81 lines
1.8 KiB
Go
81 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
)
|
|
|
|
////// FORUMS /////
|
|
|
|
var resultsChan = make(chan []ForumSearchResult)
|
|
|
|
func sendForumResults(responseAddr string, results []ForumSearchResult) {
|
|
resultsMsg := Message{
|
|
ID: hostID,
|
|
Type: "forum-results",
|
|
Content: marshalResults(results),
|
|
}
|
|
|
|
err := sendMessage(responseAddr, resultsMsg)
|
|
if err != nil {
|
|
log.Printf("Error sending forum search results to %s: %v", responseAddr, err)
|
|
} else {
|
|
log.Printf("Forum search results sent successfully to %s", responseAddr)
|
|
}
|
|
}
|
|
|
|
func marshalResults(results []ForumSearchResult) string {
|
|
content, err := json.Marshal(results)
|
|
if err != nil {
|
|
log.Printf("Error marshalling forum results: %v", err)
|
|
return ""
|
|
}
|
|
return string(content)
|
|
}
|
|
|
|
func handleForumResultsMessage(msg Message) {
|
|
var results []ForumSearchResult
|
|
err := json.Unmarshal([]byte(msg.Content), &results)
|
|
if err != nil {
|
|
log.Printf("Error unmarshalling forum results: %v", err)
|
|
return
|
|
}
|
|
|
|
log.Printf("Received forum results: %+v", results)
|
|
// Send results to resultsChan
|
|
go func() {
|
|
resultsChan <- results
|
|
}()
|
|
}
|
|
|
|
func fetchForumResults(query, safe, lang string, page int) []ForumSearchResult {
|
|
results, err := PerformRedditSearch(query, safe, page)
|
|
if err != nil {
|
|
log.Printf("Error fetching forum results: %v", err)
|
|
return nil
|
|
}
|
|
return results
|
|
}
|
|
|
|
////// FORUMS //////
|
|
|
|
////// IMAGES /////
|
|
|
|
var imageResultsChan = make(chan []ImageSearchResult)
|
|
|
|
func handleImageResultsMessage(msg Message) {
|
|
var results []ImageSearchResult
|
|
err := json.Unmarshal([]byte(msg.Content), &results)
|
|
if err != nil {
|
|
log.Printf("Error unmarshalling image results: %v", err)
|
|
return
|
|
}
|
|
|
|
log.Printf("Received image results: %+v", results)
|
|
// Send results to imageResultsChan
|
|
go func() {
|
|
imageResultsChan <- results
|
|
}()
|
|
}
|
|
|
|
////// IMAGES /////
|