Changed self-crawling as experimental, cleanup unused features
Some checks failed
Run Integration Tests / test (push) Failing after 1m15s
Some checks failed
Run Integration Tests / test (push) Failing after 1m15s
This commit is contained in:
parent
ca87df5df1
commit
49cb7bb94a
27 changed files with 1731 additions and 832 deletions
|
@ -1,218 +1,203 @@
|
|||
//go:build experimental
|
||||
// +build experimental
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
)
|
||||
|
||||
func handleSearchTextMessage(msg Message) {
|
||||
var searchParams struct {
|
||||
Query string `json:"query"`
|
||||
Safe string `json:"safe"`
|
||||
Lang string `json:"lang"`
|
||||
Page int `json:"page"`
|
||||
ResponseAddr string `json:"responseAddr"`
|
||||
type searchParams struct {
|
||||
Query string `json:"query"`
|
||||
Safe string `json:"safe"`
|
||||
Lang string `json:"lang"`
|
||||
Page int `json:"page"`
|
||||
ResponseAddr string `json:"responseAddr"`
|
||||
}
|
||||
|
||||
func extractTargetFromAddress(addr string) string {
|
||||
if len(addr) > 5 && addr[len(addr)-5:] == ".sock" {
|
||||
return addr[:len(addr)-5]
|
||||
}
|
||||
err := json.Unmarshal([]byte(msg.Content), &searchParams)
|
||||
if err != nil {
|
||||
printWarn("Error parsing search parameters: %v", err)
|
||||
return addr
|
||||
}
|
||||
|
||||
// Utility to respond to any search
|
||||
func respondToSearch(req searchParams, msgType uint8, results any) {
|
||||
if req.ResponseAddr == "" {
|
||||
printErr("ResponseAddr is empty")
|
||||
return
|
||||
}
|
||||
|
||||
printDebug("Received search-text request. ResponseAddr: %s", searchParams.ResponseAddr)
|
||||
|
||||
results := fetchTextResults(searchParams.Query, searchParams.Safe, searchParams.Lang, searchParams.Page)
|
||||
resultsJSON, err := json.Marshal(results)
|
||||
respBytes, err := json.Marshal(results)
|
||||
if err != nil {
|
||||
printWarn("Error marshalling search results: %v", err)
|
||||
printWarn("Failed to marshal results for msg type %d: %v", msgType, err)
|
||||
return
|
||||
}
|
||||
|
||||
responseMsg := Message{
|
||||
ID: hostID,
|
||||
Type: "text-results",
|
||||
Content: string(resultsJSON),
|
||||
resp := Message{
|
||||
ID: generateMessageID(),
|
||||
Type: msgType,
|
||||
Content: respBytes,
|
||||
Target: req.ResponseAddr,
|
||||
}
|
||||
|
||||
// Log the address to be used for sending the response
|
||||
printDebug("Sending text search results to %s", searchParams.ResponseAddr)
|
||||
|
||||
if searchParams.ResponseAddr == "" {
|
||||
printErr("Error: Response address is empty")
|
||||
return
|
||||
}
|
||||
|
||||
err = sendMessage(searchParams.ResponseAddr, responseMsg)
|
||||
err = sendMessage(resp)
|
||||
if err != nil {
|
||||
printWarn("Error sending text search results to %s: %v", searchParams.ResponseAddr, err)
|
||||
printWarn("Failed to send search results to %s: %v", req.ResponseAddr, err)
|
||||
}
|
||||
}
|
||||
|
||||
func handleSearchImageMessage(msg Message) {
|
||||
var searchParams struct {
|
||||
Query string `json:"query"`
|
||||
Safe string `json:"safe"`
|
||||
Lang string `json:"lang"`
|
||||
Page int `json:"page"`
|
||||
ResponseAddr string `json:"responseAddr"`
|
||||
}
|
||||
err := json.Unmarshal([]byte(msg.Content), &searchParams)
|
||||
if err != nil {
|
||||
log.Printf("Error parsing search parameters: %v", err)
|
||||
func sendBinaryResponse(req searchParams, msgType uint8, payload []byte, msgID uint32) {
|
||||
if req.ResponseAddr == "" {
|
||||
printErr("ResponseAddr is empty")
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Received search-image request. ResponseAddr: %s", searchParams.ResponseAddr)
|
||||
results := fetchImageResults(searchParams.Query, searchParams.Safe, searchParams.Lang, searchParams.Page, true)
|
||||
resultsJSON, err := json.Marshal(results)
|
||||
resp := Message{
|
||||
ID: msgID,
|
||||
Type: msgType,
|
||||
Content: payload,
|
||||
Target: req.ResponseAddr,
|
||||
}
|
||||
|
||||
if err := sendMessage(resp); err != nil {
|
||||
printWarn("Failed to send binary search results: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func handleSearchTextMessage(msg Message) {
|
||||
var req searchParams
|
||||
if err := json.Unmarshal([]byte(msg.Content), &req); err != nil {
|
||||
printWarn("Invalid JSON: %v", err)
|
||||
return
|
||||
}
|
||||
printDebug("Received search-text from %s", req.ResponseAddr)
|
||||
|
||||
results := fetchTextResults(req.Query, req.Safe, req.Lang, req.Page)
|
||||
data, err := encodeTextResults(results)
|
||||
if err != nil {
|
||||
log.Printf("Error marshalling search results: %v", err)
|
||||
printWarn("Failed to encode text results: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
responseMsg := Message{
|
||||
ID: hostID,
|
||||
Type: "image-results",
|
||||
Content: string(resultsJSON),
|
||||
}
|
||||
|
||||
// Log the address to be used for sending the response
|
||||
log.Printf("Sending image search results to %s", searchParams.ResponseAddr)
|
||||
|
||||
if searchParams.ResponseAddr == "" {
|
||||
log.Printf("Error: Response address is empty")
|
||||
return
|
||||
}
|
||||
|
||||
err = sendMessage(searchParams.ResponseAddr, responseMsg)
|
||||
if err != nil {
|
||||
log.Printf("Error sending image search results to %s: %v", searchParams.ResponseAddr, err)
|
||||
}
|
||||
sendBinaryResponse(req, MsgTypeSearchTextResponse, data, msg.ID)
|
||||
}
|
||||
|
||||
func handleSearchVideoMessage(msg Message) {
|
||||
var searchParams struct {
|
||||
Query string `json:"query"`
|
||||
Safe string `json:"safe"`
|
||||
Lang string `json:"lang"`
|
||||
Page int `json:"page"`
|
||||
ResponseAddr string `json:"responseAddr"`
|
||||
var req searchParams
|
||||
if err := json.Unmarshal([]byte(msg.Content), &req); err != nil {
|
||||
printWarn("Invalid JSON: %v", err)
|
||||
return
|
||||
}
|
||||
err := json.Unmarshal([]byte(msg.Content), &searchParams)
|
||||
printDebug("Received search-video from %s", req.ResponseAddr)
|
||||
|
||||
results := fetchVideoResults(req.Query, req.Safe, req.Lang, req.Page)
|
||||
data, err := encodeVideoResults(results)
|
||||
if err != nil {
|
||||
log.Printf("Error parsing search parameters: %v", err)
|
||||
printWarn("Failed to encode video results: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Received search-video request. ResponseAddr: %s", searchParams.ResponseAddr)
|
||||
sendBinaryResponse(req, MsgTypeSearchVideoResponse, data, msg.ID)
|
||||
}
|
||||
|
||||
results := fetchVideoResults(searchParams.Query, searchParams.Safe, searchParams.Lang, searchParams.Page)
|
||||
resultsJSON, err := json.Marshal(results)
|
||||
func handleSearchMusicMessage(msg Message) {
|
||||
var req searchParams
|
||||
if err := json.Unmarshal([]byte(msg.Content), &req); err != nil {
|
||||
printWarn("Invalid JSON: %v", err)
|
||||
return
|
||||
}
|
||||
printDebug("Received search-music from %s", req.ResponseAddr)
|
||||
|
||||
results := fetchMusicResults(req.Query, req.Page)
|
||||
data, err := encodeMusicResults(results)
|
||||
if err != nil {
|
||||
log.Printf("Error marshalling search results: %v", err)
|
||||
printWarn("Failed to encode music results: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
responseMsg := Message{
|
||||
ID: hostID,
|
||||
Type: "video-results",
|
||||
Content: string(resultsJSON),
|
||||
}
|
||||
|
||||
log.Printf("Sending video search results to %s", searchParams.ResponseAddr)
|
||||
|
||||
if searchParams.ResponseAddr == "" {
|
||||
log.Printf("Error: Response address is empty")
|
||||
return
|
||||
}
|
||||
|
||||
err = sendMessage(searchParams.ResponseAddr, responseMsg)
|
||||
if err != nil {
|
||||
log.Printf("Error sending video search results to %s: %v", searchParams.ResponseAddr, err)
|
||||
}
|
||||
sendBinaryResponse(req, MsgTypeSearchMusicResponse, data, msg.ID)
|
||||
}
|
||||
|
||||
func handleSearchFileMessage(msg Message) {
|
||||
var searchParams struct {
|
||||
Query string `json:"query"`
|
||||
Safe string `json:"safe"`
|
||||
Lang string `json:"lang"`
|
||||
Page int `json:"page"`
|
||||
ResponseAddr string `json:"responseAddr"`
|
||||
var req searchParams
|
||||
if err := json.Unmarshal([]byte(msg.Content), &req); err != nil {
|
||||
printWarn("Invalid JSON: %v", err)
|
||||
return
|
||||
}
|
||||
err := json.Unmarshal([]byte(msg.Content), &searchParams)
|
||||
printDebug("Received search-file from %s", req.ResponseAddr)
|
||||
|
||||
results := fetchFileResults(req.Query, req.Safe, req.Lang, req.Page)
|
||||
data, err := encodeFileResults(results)
|
||||
if err != nil {
|
||||
log.Printf("Error parsing search parameters: %v", err)
|
||||
printWarn("Failed to encode file results: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Received search-file request. ResponseAddr: %s", searchParams.ResponseAddr)
|
||||
|
||||
results := fetchFileResults(searchParams.Query, searchParams.Safe, searchParams.Lang, searchParams.Page)
|
||||
resultsJSON, err := json.Marshal(results)
|
||||
if err != nil {
|
||||
log.Printf("Error marshalling search results: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
responseMsg := Message{
|
||||
ID: hostID,
|
||||
Type: "file-results",
|
||||
Content: string(resultsJSON),
|
||||
}
|
||||
|
||||
log.Printf("Sending file search results to %s", searchParams.ResponseAddr)
|
||||
|
||||
if searchParams.ResponseAddr == "" {
|
||||
log.Printf("Error: Response address is empty")
|
||||
return
|
||||
}
|
||||
|
||||
err = sendMessage(searchParams.ResponseAddr, responseMsg)
|
||||
if err != nil {
|
||||
log.Printf("Error sending file search results to %s: %v", searchParams.ResponseAddr, err)
|
||||
}
|
||||
sendBinaryResponse(req, MsgTypeSearchFileResponse, data, msg.ID)
|
||||
}
|
||||
|
||||
func handleSearchForumMessage(msg Message) {
|
||||
var searchParams struct {
|
||||
Query string `json:"query"`
|
||||
Safe string `json:"safe"`
|
||||
Lang string `json:"lang"`
|
||||
Page int `json:"page"`
|
||||
ResponseAddr string `json:"responseAddr"`
|
||||
var req searchParams
|
||||
if err := json.Unmarshal([]byte(msg.Content), &req); err != nil {
|
||||
printWarn("Invalid JSON: %v", err)
|
||||
return
|
||||
}
|
||||
err := json.Unmarshal([]byte(msg.Content), &searchParams)
|
||||
printDebug("Received search-forum from %s", req.ResponseAddr)
|
||||
|
||||
results := fetchForumResults(req.Query, req.Safe, req.Lang, req.Page)
|
||||
data, err := encodeForumResults(results)
|
||||
if err != nil {
|
||||
log.Printf("Error parsing search parameters: %v", err)
|
||||
printWarn("Failed to encode forum results: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Received search-forum request. ResponseAddr: %s", searchParams.ResponseAddr)
|
||||
|
||||
results := fetchForumResults(searchParams.Query, searchParams.Safe, searchParams.Lang, searchParams.Page)
|
||||
resultsJSON, err := json.Marshal(results)
|
||||
if err != nil {
|
||||
log.Printf("Error marshalling search results: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
responseMsg := Message{
|
||||
ID: hostID,
|
||||
Type: "forum-results",
|
||||
Content: string(resultsJSON),
|
||||
}
|
||||
|
||||
// Log the address to be used for sending the response
|
||||
log.Printf("Sending forum search results to %s", searchParams.ResponseAddr)
|
||||
|
||||
if searchParams.ResponseAddr == "" {
|
||||
log.Printf("Error: Response address is empty")
|
||||
return
|
||||
}
|
||||
|
||||
err = sendMessage(searchParams.ResponseAddr, responseMsg)
|
||||
if err != nil {
|
||||
log.Printf("Error sending forum search results to %s: %v", searchParams.ResponseAddr, err)
|
||||
}
|
||||
sendBinaryResponse(req, MsgTypeSearchForumResponse, data, msg.ID)
|
||||
}
|
||||
|
||||
func handleSearchImageMessage(msg Message) {
|
||||
var req searchParams
|
||||
if err := json.Unmarshal([]byte(msg.Content), &req); err != nil {
|
||||
printWarn("Invalid JSON: %v", err)
|
||||
return
|
||||
}
|
||||
printDebug("Received image search type %d from %s", msg.Type, req.ResponseAddr)
|
||||
|
||||
var (
|
||||
thumbsNeeded bool
|
||||
fullNeeded bool
|
||||
)
|
||||
|
||||
switch msg.Type {
|
||||
case MsgTypeSearchImageRawRequest:
|
||||
thumbsNeeded = false
|
||||
fullNeeded = false
|
||||
case MsgTypeSearchImageThumbRequest:
|
||||
thumbsNeeded = true
|
||||
fullNeeded = false
|
||||
case MsgTypeSearchImageFullRequest:
|
||||
thumbsNeeded = false
|
||||
fullNeeded = true
|
||||
case MsgTypeSearchImageAllRequest:
|
||||
thumbsNeeded = true
|
||||
fullNeeded = true
|
||||
default:
|
||||
printWarn("Unknown image search type: %d", msg.Type)
|
||||
return
|
||||
}
|
||||
|
||||
results := fetchImageResults(req.Query, req.Safe, req.Lang, req.Page, true, thumbsNeeded)
|
||||
|
||||
if fullNeeded || thumbsNeeded {
|
||||
results = prepareProxiedImages(results, msg.Type)
|
||||
}
|
||||
|
||||
data, err := encodeImageResults(results)
|
||||
if err != nil {
|
||||
printWarn("Failed to encode image results: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
sendBinaryResponse(req, MsgTypeSearchImageResponse, data, msg.ID)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue