-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcExtractResults.go
48 lines (44 loc) · 1.28 KB
/
funcExtractResults.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package ihcph
import (
"encoding/json"
"github.com/sp0x/ihcph/funcExtractResults"
"github.com/sp0x/ihcph/telegram"
"github.com/sp0x/torrentd/indexer"
"github.com/sp0x/torrentd/indexer/search"
"net/http"
)
type ExtractionRequest struct {
BotToken string `json:"bot_id"`
}
type ExtractionResponse struct {
Items []string `json:"items"`
}
func ExtractResults(w http.ResponseWriter, r *http.Request) {
extractionRequest := ExtractionRequest{}
err := json.NewDecoder(r.Body).Decode(&extractionRequest)
if err != nil {
http.Error(w, "bad request", 400)
return
}
fnContext := funcExtractResults.Initialize()
botInterface := telegram.NewBotInterface()
err = botInterface.Initialize(extractionRequest.BotToken)
if err != nil {
http.Error(w, "error fetching bot", 500)
return
}
resultsChan := indexer.GetAllPagesFromIndex(fnContext.IndexFacade, nil)
proxyChan := make(chan *search.ExternalResultItem)
go botInterface.BroadcastResults(proxyChan)
response := ExtractionResponse{Items: []string{}}
for result := range resultsChan {
proxyChan <- result
if result == nil {
break
}
response.Items = append(response.Items, result.Title)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(response)
}