diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0034634 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +config.json +opensearch.xml \ No newline at end of file diff --git a/README.md b/README.md index 90fd75f..e0df49c 100644 --- a/README.md +++ b/README.md @@ -20,15 +20,28 @@ - [ ] Better name - [ ] LXC container - [ ] Docker container +- [ ] Automatic updates +- [ ] Scalable crawlers and webservers + load balacing -# Go Search Engine +# Ocásek (Warp) Search Engine -A self-hosted [metasearch engine](https://en.wikipedia.org/wiki/Metasearch_engine) that respects privacy, contains no ads, and serves as a proxy/alternative to Google website. +A self-hosted private and anonymous [metasearch engine](https://en.wikipedia.org/wiki/Metasearch_engine), that aims to be more resource effichent and scalable. Decentralized services are nice, but juming between instances when one just stops working for some reason is just inconvenient. So thats why this engine can do both, you can self-hoste it or use [officiall instance](https://search.spitfirebrowser.com/). + +## Comparison to other search engines + +| Name | Works without JS | Privacy frontend redirect | Torrent results | API | No 3rd party libs | Scalable | Not Resource Hungry | Dynamic Page Loading | +|------------|----------------------|---------------------------|-----------------|-----|-------------------|----------|---------------------------------------------|----------------------| +| Whoogle | ✅ | ❓ Only host can set it | ❌ | ❌ | ❌ | ❌ | ❓ Moderate | ❓ Not specified | +| Araa-Search| ✅ | ✅ | ✅ | ✅ | ❓ | ❌ | ❌ Very resource hungry | ❌ | +| LibreY | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❓ Moderate | ❌ | +| Ocásek | ✅ | ✅ | ✅ | ❌ | ✅ [1] | ✅ | ✅ about 20MiB at idle, 21MiB when searching| ✅ | + +[1]: It does not rely on 3rd-party libs for webscraping like [Selenium](https://www.javatpoint.com/selenium-webdriver), but it uses other search instalces like LibreX as fallback. ## Features -- Text search using Google search results. -- Image search using the Qwant API. +- Text search using Google, Brave, DuckDuckGo and LibreX/Y search results. +- Image search using the Qwant/Imgur. - Video search using Piped API. - Image viewing using proxy and direct links to image source pages for image searches. - Maps using OpenStreetMap @@ -39,7 +52,8 @@ A self-hosted [metasearch engine](https://en.wikipedia.org/wiki/Metasearch_engin ### Prerequisites - Go (version 1.18 or higher recommended) -- Access to the internet for fetching results from the Qwant API and Google +- Git (unexpected) +- Access to the internet for fetching results (even more unexpected) ### Running the Application @@ -48,4 +62,6 @@ git clone https://weforgecode.xyz/Spitfire/Search.git cd Search chmod +x ./run.sh ./run.sh -``` \ No newline at end of file +``` + +*Its that easy!* \ No newline at end of file diff --git a/cache.go b/cache.go index fa2912c..db982a8 100644 --- a/cache.go +++ b/cache.go @@ -1,13 +1,18 @@ -// common_cache.go package main import ( "fmt" + "log" "sync" "time" + + "github.com/shirou/gopsutil/mem" ) -var resultsCache = NewResultsCache(6 * time.Hour) // Cache with 6-hour expiration +var ( + resultsCache = NewResultsCache(6 * time.Hour) // Cache with 6-hour expiration + maxMemoryUsage = 90.0 // Maximum memory usage in % +) // SearchResult is a generic interface for all types of search results. type SearchResult interface{} @@ -114,9 +119,13 @@ func (rc *ResultsCache) Get(key CacheKey) ([]SearchResult, bool) { func (rc *ResultsCache) Set(key CacheKey, results []SearchResult) { rc.mu.Lock() defer rc.mu.Unlock() - rc.results[rc.keyToString(key)] = CachedItem{ - Results: results, - StoredTime: time.Now(), + + if _, exists := rc.results[rc.keyToString(key)]; !exists { + rc.results[rc.keyToString(key)] = CachedItem{ + Results: results, + StoredTime: time.Now(), + } + go rc.checkAndCleanCache() } } @@ -125,7 +134,46 @@ func (rc *ResultsCache) keyToString(key CacheKey) string { return fmt.Sprintf("%s|%d|%t|%s|%s", key.Query, key.Page, key.Safe, key.Lang, key.Type) } -// Helper functions to convert between generic SearchResult and specific ImageSearchResult +func (rc *ResultsCache) checkAndCleanCache() { + if rc.memoryUsage() > maxMemoryUsage { + rc.cleanOldestItems() + } +} + +func (rc *ResultsCache) memoryUsage() float64 { + v, err := mem.VirtualMemory() + if err != nil { + log.Printf("Failed to get memory info: %v", err) + return 0 + } + + return v.UsedPercent +} + +func (rc *ResultsCache) cleanOldestItems() { + rc.mu.Lock() + defer rc.mu.Unlock() + + for rc.memoryUsage() > maxMemoryUsage { + var oldestKey string + var oldestTime time.Time = time.Now() + + for key, item := range rc.results { + if item.StoredTime.Before(oldestTime) { + oldestTime = item.StoredTime + oldestKey = key + } + } + + if oldestKey != "" { + delete(rc.results, oldestKey) + log.Printf("Removed oldest cache item: %s", oldestKey) + } else { + break + } + } +} + func convertToSearchResults(results interface{}) []SearchResult { switch res := results.(type) { case []TextSearchResult: diff --git a/common.go b/common.go new file mode 100644 index 0000000..7d30861 --- /dev/null +++ b/common.go @@ -0,0 +1,17 @@ +package main + +import ( + "html/template" +) + +var ( + debugMode bool = true + funcs = template.FuncMap{ + "sub": func(a, b int) int { + return a - b + }, + "add": func(a, b int) int { + return a + b + }, + } +) diff --git a/get-searchxng.go b/get-searchxng.go index cb88261..b6da71a 100644 --- a/get-searchxng.go +++ b/get-searchxng.go @@ -128,10 +128,10 @@ func isInstanceValid(instance SearXInstance) bool { } } -func main() { - instance, err := getRandomSearXInstance() - if err != nil { - log.Fatalf("Failed to get a SearX instance: %v", err) - } - fmt.Printf("Selected SearX instance: %s\n", instance.URL) -} +// func main() { +// instance, err := getRandomSearXInstance() +// if err != nil { +// log.Fatalf("Failed to get a SearX instance: %v", err) +// } +// fmt.Printf("Selected SearX instance: %s\n", instance.URL) +// } diff --git a/go.mod b/go.mod index 4f6d6fa..1b9bbf4 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,22 @@ module searchengine go 1.18 +require github.com/PuerkitoBio/goquery v1.9.1 // direct + require ( - github.com/PuerkitoBio/goquery v1.9.1 // direct github.com/andybalholm/cascadia v1.3.2 // indirect + github.com/chromedp/cdproto v0.0.0-20240202021202-6d0b6a386732 // indirect + github.com/chromedp/chromedp v0.9.5 // indirect + github.com/chromedp/sysutil v1.0.0 // indirect + github.com/gobwas/httphead v0.1.0 // indirect + github.com/gobwas/pool v0.2.1 // indirect + github.com/gobwas/ws v1.3.2 // indirect + github.com/josharian/intern v1.0.0 // indirect + github.com/mailru/easyjson v0.7.7 // indirect + github.com/shirou/gopsutil v3.21.11+incompatible golang.org/x/net v0.21.0 // indirect -) + golang.org/x/sys v0.17.0 // indirect + golang.org/x/time v0.5.0 // indirect + github.com/go-ole/go-ole v1.2.6 // indirect + github.com/yusufpapurcu/wmi v1.2.4 // indirect +) \ No newline at end of file diff --git a/go.sum b/go.sum index f988942..cef914e 100644 --- a/go.sum +++ b/go.sum @@ -2,7 +2,31 @@ github.com/PuerkitoBio/goquery v1.9.1 h1:mTL6XjbJTZdpfL+Gwl5U2h1l9yEkJjhmlTeV9VP github.com/PuerkitoBio/goquery v1.9.1/go.mod h1:cW1n6TmIMDoORQU5IU/P1T3tGFunOeXEpGP2WHRwkbY= github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss= github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU= +github.com/chromedp/cdproto v0.0.0-20240202021202-6d0b6a386732 h1:XYUCaZrW8ckGWlCRJKCSoh/iFwlpX316a8yY9IFEzv8= +github.com/chromedp/cdproto v0.0.0-20240202021202-6d0b6a386732/go.mod h1:GKljq0VrfU4D5yc+2qA6OVr8pmO/MBbPEWqWQ/oqGEs= +github.com/chromedp/chromedp v0.9.5 h1:viASzruPJOiThk7c5bueOUY91jGLJVximoEMGoH93rg= +github.com/chromedp/chromedp v0.9.5/go.mod h1:D4I2qONslauw/C7INoCir1BJkSwBYMyZgx8X276z3+Y= +github.com/chromedp/sysutil v1.0.0 h1:+ZxhTpfpZlmchB58ih/LBHX52ky7w2VhQVKQMucy3Ic= +github.com/chromedp/sysutil v1.0.0/go.mod h1:kgWmDdq8fTzXYcKIBqIYvRRTnYb9aNS9moAV0xufSww= +github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= +github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= +github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU= +github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM= +github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og= +github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= +github.com/gobwas/ws v1.3.2 h1:zlnbNHxumkRvfPWgfXu8RBwyNR1x8wh9cf5PTOCqs9Q= +github.com/gobwas/ws v1.3.2/go.mod h1:hRKAFb8wOxFROYNsT1bqfWnhX+b5MFeJM9r2ZSwg/KY= +github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= +github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= +github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80/go.mod h1:imJHygn/1yfhB7XSJJKlFZKl/J+dCPAknuiaGOshXAs= +github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= +github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= +github.com/orisano/pixelmatch v0.0.0-20220722002657-fb0b55479cde/go.mod h1:nZgzbfBr3hhjoZnS66nKrHmduYNpc34ny7RK4z5/HM0= +github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI= +github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= +github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= @@ -18,12 +42,17 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= +golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= @@ -33,6 +62,8 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= +golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= diff --git a/images-imgur.go b/images-imgur.go new file mode 100644 index 0000000..ede8d10 --- /dev/null +++ b/images-imgur.go @@ -0,0 +1,149 @@ +package main + +import ( + "fmt" + "net/http" + "net/url" + "strconv" + "strings" + "time" + + "github.com/PuerkitoBio/goquery" +) + +// PerformImgurImageSearch performs an image search on Imgur and returns the results +func PerformImgurImageSearch(query, safe, lang string, page int) ([]ImageSearchResult, time.Duration, error) { + startTime := time.Now() // Start the timer + + var results []ImageSearchResult + searchURL := buildImgurSearchURL(query, page) + + resp, err := http.Get(searchURL) + if err != nil { + return nil, 0, fmt.Errorf("making request: %v", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return nil, 0, fmt.Errorf("unexpected status code: %d", resp.StatusCode) + } + + doc, err := goquery.NewDocumentFromReader(resp.Body) + if err != nil { + return nil, 0, fmt.Errorf("loading HTML document: %v", err) + } + + doc.Find("div.cards div.post").Each(func(i int, s *goquery.Selection) { + thumbnailSrc, exists := s.Find("a img").Attr("src") + if !exists || len(thumbnailSrc) < 25 { + return + } + imgSrc := strings.Replace(thumbnailSrc, "b.", ".", 1) + + // Ensure the URLs have the correct protocol + if !strings.HasPrefix(thumbnailSrc, "http") { + thumbnailSrc = "https:" + thumbnailSrc + } + if !strings.HasPrefix(imgSrc, "http") { + imgSrc = "https:" + imgSrc + } + + urlPath, exists := s.Find("a").Attr("href") + if !exists { + return + } + + // Scrape the image directly from the Imgur page + imgSrc = scrapeImageFromImgurPage("https://imgur.com" + urlPath) + + // Remove any query parameters from the URL + imgSrc = removeQueryParameters(imgSrc) + + title, _ := s.Find("a img").Attr("alt") + + width, _ := strconv.Atoi(s.Find("a img").AttrOr("width", "0")) + height, _ := strconv.Atoi(s.Find("a img").AttrOr("height", "0")) + + results = append(results, ImageSearchResult{ + Thumbnail: thumbnailSrc, + Title: strings.TrimSpace(title), + Media: imgSrc, + Width: width, + Height: height, + Source: "https://imgur.com" + urlPath, + ThumbProxy: imgSrc, //"/img_proxy?url=" + url.QueryEscape(imgSrc) + }) + }) + + duration := time.Since(startTime) // Calculate the duration + + return results, duration, nil +} + +// scrapeImageFromImgurPage scrapes the image source from the Imgur page +func scrapeImageFromImgurPage(pageURL string) string { + resp, err := http.Get(pageURL) + if err != nil { + fmt.Printf("Error fetching page: %v\n", err) + return "" + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + fmt.Printf("Unexpected status code: %d\n", resp.StatusCode) + return "" + } + + doc, err := goquery.NewDocumentFromReader(resp.Body) + if err != nil { + fmt.Printf("Error loading HTML document: %v\n", err) + return "" + } + + imgSrc, exists := doc.Find("meta[property='og:image']").Attr("content") + if !exists { + fmt.Printf("Image not found on page: %s\n", pageURL) + return "" + } + + // Ensure the URL has the correct protocol + if !strings.HasPrefix(imgSrc, "http") { + imgSrc = "https:" + imgSrc + } + + return imgSrc +} + +// removeQueryParameters removes query parameters from a URL +func removeQueryParameters(rawURL string) string { + parsedURL, err := url.Parse(rawURL) + if err != nil { + fmt.Printf("Error parsing URL: %v\n", err) + return rawURL + } + parsedURL.RawQuery = "" + return parsedURL.String() +} + +func buildImgurSearchURL(query string, page int) string { + baseURL := "https://imgur.com/search/score/all" + params := url.Values{} + params.Add("q", query) + params.Add("qs", "thumbs") + params.Add("p", fmt.Sprintf("%d", page-1)) + return fmt.Sprintf("%s?%s", baseURL, params.Encode()) +} + +// func main() { +// results, duration, err := PerformImgurImageSearch("cats", "true", "en", 1) +// if err != nil { +// fmt.Println("Error:", err) +// return +// } + +// fmt.Printf("Search took: %v\n", duration) +// for _, result := range results { +// fmt.Printf("Title: %s\nSource: %s\nMedia: %s\nThumbnail: %s\nThumbProxy: %s\nWidth: %d\nHeight: %d\n\n", +// result.Title, result.Source, result.Media, result.Thumbnail, result.ThumbProxy, result.Width, result.Height) +// } +// } diff --git a/images-quant.go b/images-quant.go new file mode 100644 index 0000000..d9a9770 --- /dev/null +++ b/images-quant.go @@ -0,0 +1,99 @@ +package main + +import ( + "encoding/json" + "fmt" + "net/http" + "net/url" + "time" +) + +// QwantAPIResponse represents the JSON response structure from Qwant API +type QwantAPIResponse struct { + Data struct { + Result struct { + Items []struct { + Media string `json:"media"` + Thumbnail string `json:"thumbnail"` + Title string `json:"title"` + Url string `json:"url"` + Width int `json:"width"` + Height int `json:"height"` + } `json:"items"` + } `json:"result"` + } `json:"data"` +} + +// PerformQwantImageSearch performs an image search on Qwant and returns the results. +func PerformQwantImageSearch(query, safe, lang string, page int) ([]ImageSearchResult, time.Duration, error) { + startTime := time.Now() // Start the timer + + const resultsPerPage = 50 + var offset int + if page <= 1 { + offset = 0 + } else { + offset = (page - 1) * resultsPerPage + } + + if safe == "" { + safe = "0" + } + + if lang == "" { + lang = "en_CA" + } + + apiURL := fmt.Sprintf("https://api.qwant.com/v3/search/images?t=images&q=%s&count=%d&locale=%s&offset=%d&device=desktop&tgp=2&safesearch=%s", + url.QueryEscape(query), + resultsPerPage, + lang, + offset, + safe) + + client := &http.Client{Timeout: 10 * time.Second} + + req, err := http.NewRequest("GET", apiURL, nil) + if err != nil { + return nil, 0, fmt.Errorf("creating request: %v", err) + } + + ImageUserAgent, err := GetUserAgent("Image-Search") + if err != nil { + return nil, 0, err + } + + req.Header.Set("User-Agent", ImageUserAgent) + + resp, err := client.Do(req) + if err != nil { + return nil, 0, fmt.Errorf("making request: %v", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return nil, 0, fmt.Errorf("unexpected status code: %d", resp.StatusCode) + } + + var apiResp QwantAPIResponse + if err := json.NewDecoder(resp.Body).Decode(&apiResp); err != nil { + return nil, 0, fmt.Errorf("decoding response: %v", err) + } + + var results []ImageSearchResult + for _, item := range apiResp.Data.Result.Items { + results = append(results, ImageSearchResult{ + Thumbnail: item.Thumbnail, + Title: item.Title, + Media: item.Media, + Source: item.Url, + ThumbProxy: "/img_proxy?url=" + url.QueryEscape(item.Media), + Width: item.Width, + Height: item.Height, + }) + } + + duration := time.Since(startTime) // Calculate the duration + + return results, duration, nil +} diff --git a/images.go b/images.go index 16e8581..84c1366 100644 --- a/images.go +++ b/images.go @@ -1,120 +1,22 @@ package main import ( - "encoding/json" "fmt" "html/template" "log" "net/http" - "net/url" "time" ) -// QwantAPIResponse represents the JSON response structure from Qwant API -type QwantAPIResponse struct { - Data struct { - Result struct { - Items []struct { - Media string `json:"media"` - Thumbnail string `json:"thumbnail"` - Title string `json:"title"` - Url string `json:"url"` - Width int `json:"width"` - Height int `json:"height"` - } `json:"items"` - } `json:"result"` - } `json:"data"` +var imageSearchEngines []SearchEngine + +func init() { + imageSearchEngines = []SearchEngine{ + {Name: "Qwant", Func: wrapImageSearchFunc(PerformQwantImageSearch), Weight: 1}, + {Name: "Imgur", Func: wrapImageSearchFunc(PerformImgurImageSearch), Weight: 2}, + } } -var funcs = template.FuncMap{ - "sub": func(a, b int) int { - return a - b - }, - "add": func(a, b int) int { - return a + b - }, -} - -// FetchImageResults contacts the image search API and returns a slice of ImageSearchResult -func fetchImageResults(query string, safe, lang string, page int) ([]ImageSearchResult, error) { - const resultsPerPage = 50 - var offset int - if page <= 1 { - offset = 0 - } else { - offset = (page - 1) * resultsPerPage - } - - // Ensuring safe search is disabled by default if not specified - if safe == "" { - safe = "0" - } - - // Defaulting to English Canada locale if not specified - if lang == "" { - lang = "en_CA" - } - - // Format &lang=lang_de is incorrect, implement fix ! - apiURL := fmt.Sprintf("https://api.qwant.com/v3/search/images?t=images&q=%s&count=%d&locale=%s&offset=%d&device=desktop&tgp=2&safesearch=%s", - url.QueryEscape(query), - resultsPerPage, - lang, - offset, - safe) - - client := &http.Client{Timeout: 10 * time.Second} - - req, err := http.NewRequest("GET", apiURL, nil) - if err != nil { - return nil, fmt.Errorf("creating request: %v", err) - } - - // User Agent generation - ImageUserAgent, err := GetUserAgent("Image-Search") - if err != nil { - fmt.Println("Error:", err) - return nil, err - } - - if debugMode { - fmt.Println("Generated User Agent (images):", ImageUserAgent) - } - - req.Header.Set("User-Agent", ImageUserAgent) - - resp, err := client.Do(req) - if err != nil { - return nil, fmt.Errorf("making request: %v", err) - } - defer resp.Body.Close() - - if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) - } - - var apiResp QwantAPIResponse - if err := json.NewDecoder(resp.Body).Decode(&apiResp); err != nil { - return nil, fmt.Errorf("decoding response: %v", err) - } - - var results []ImageSearchResult - for _, item := range apiResp.Data.Result.Items { - results = append(results, ImageSearchResult{ - Thumbnail: item.Thumbnail, // Thumbnail URL - Title: item.Title, // Image title - Media: item.Media, // Direct link to the image - Source: item.Url, - ThumbProxy: "/img_proxy?url=" + url.QueryEscape(item.Media), - Width: item.Width, - Height: item.Height, - }) - } - - return results, nil -} - -// HandleImageSearch is the HTTP handler for image search requests func handleImageSearch(w http.ResponseWriter, query, safe, lang string, page int) { startTime := time.Now() @@ -174,31 +76,66 @@ func getImageResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string select { case results := <-cacheChan: if results == nil { - combinedResults = fetchAndCacheImageResults(query, safe, lang, page) + combinedResults = fetchImageResults(query, safe, lang, page) + if len(combinedResults) > 0 { + resultsCache.Set(cacheKey, convertToSearchResults(combinedResults)) + } } else { _, _, imageResults := convertToSpecificResults(results) combinedResults = imageResults } case <-time.After(2 * time.Second): log.Println("Cache check timeout") - combinedResults = fetchAndCacheImageResults(query, safe, lang, page) + combinedResults = fetchImageResults(query, safe, lang, page) + if len(combinedResults) > 0 { + resultsCache.Set(cacheKey, convertToSearchResults(combinedResults)) + } } return combinedResults } -func fetchAndCacheImageResults(query, safe, lang string, page int) []ImageSearchResult { - results, err := fetchImageResults(query, safe, lang, page) - if err != nil || len(results) == 0 { - log.Printf("Error fetching image results: %v", err) - return []ImageSearchResult{ - {Title: "Results are currently unavailable, sorry. Please try again later."}, +func fetchImageResults(query, safe, lang string, page int) []ImageSearchResult { + var results []ImageSearchResult + + for _, engine := range imageSearchEngines { + log.Printf("Using image search engine: %s", engine.Name) + + searchResults, duration, err := engine.Func(query, safe, lang, page) + updateEngineMetrics(&engine, duration, err == nil) + if err != nil { + log.Printf("Error performing image search with %s: %v", engine.Name, err) + continue + } + + for _, result := range searchResults { + results = append(results, result.(ImageSearchResult)) + } + + // If results are found, break out of the loop + if len(results) > 0 { + break } } - // Cache the valid results - cacheKey := CacheKey{Query: query, Page: page, Safe: safe == "true", Lang: lang, Type: "image"} - resultsCache.Set(cacheKey, convertToSearchResults(results)) + // If no results found after trying all engines + if len(results) == 0 { + log.Printf("No image results found for query: %s", query) + } return results } + +func wrapImageSearchFunc(f func(string, string, string, int) ([]ImageSearchResult, time.Duration, error)) func(string, string, string, int) ([]SearchResult, time.Duration, error) { + return func(query, safe, lang string, page int) ([]SearchResult, time.Duration, error) { + imageResults, duration, err := f(query, safe, lang, page) + if err != nil { + return nil, duration, err + } + searchResults := make([]SearchResult, len(imageResults)) + for i, result := range imageResults { + searchResults[i] = result + } + return searchResults, duration, nil + } +} diff --git a/init.go b/init.go new file mode 100644 index 0000000..b3129a4 --- /dev/null +++ b/init.go @@ -0,0 +1,116 @@ +package main + +import ( + "bufio" + "encoding/json" + "fmt" + "log" + "os" + "strconv" +) + +// Configuration structure +type Config struct { + Port int + OpenSearch OpenSearchConfig +} + +type OpenSearchConfig struct { + Domain string +} + +// Default configuration values +var defaultConfig = Config{ + Port: 5000, + OpenSearch: OpenSearchConfig{ + Domain: "localhost", + }, +} + +const configFilePath = "config.json" + +func main() { + // Run the initialization process + err := initConfig() + if err != nil { + fmt.Println("Error during initialization:", err) + return + } + + // Start the main application + runServer() +} + +func initConfig() error { + if _, err := os.Stat(configFilePath); os.IsNotExist(err) { + return createConfig() + } + + fmt.Println("Configuration file already exists.") + return nil +} + +func createConfig() error { + reader := bufio.NewReader(os.Stdin) + + fmt.Println("Configuration file not found.") + fmt.Print("Do you want to use default values? (yes/no): ") + useDefaults, _ := reader.ReadString('\n') + + config := defaultConfig + if useDefaults != "yes\n" { + fmt.Print("Enter port (default 5000): ") + portStr, _ := reader.ReadString('\n') + if portStr != "\n" { + port, err := strconv.Atoi(portStr[:len(portStr)-1]) + if err != nil { + return err + } + config.Port = port + } + + fmt.Print("Enter your domain address (e.g., domain.com): ") + domain, _ := reader.ReadString('\n') + if domain != "\n" { + config.OpenSearch.Domain = domain[:len(domain)-1] + } + } + + saveConfig(config) + return nil +} + +func saveConfig(config Config) { + file, err := os.Create(configFilePath) + if err != nil { + fmt.Println("Error creating config file:", err) + return + } + defer file.Close() + + configData, err := json.MarshalIndent(config, "", " ") + if err != nil { + fmt.Println("Error marshalling config data:", err) + return + } + + _, err = file.Write(configData) + if err != nil { + fmt.Println("Error writing to config file:", err) + } +} + +func loadConfig() Config { + configFile, err := os.Open(configFilePath) + if err != nil { + log.Fatalf("Error opening config file: %v", err) + } + defer configFile.Close() + + var config Config + if err := json.NewDecoder(configFile).Decode(&config); err != nil { + log.Fatalf("Error decoding config file: %v", err) + } + + return config +} diff --git a/main.go b/main.go index a10f225..a463ba6 100644 --- a/main.go +++ b/main.go @@ -63,19 +63,6 @@ var languageOptions = []LanguageOption{ {Code: "lang_vi", Name: "Tiếng Việt (Vietnamese)"}, } -func main() { - http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) - http.HandleFunc("/", handleSearch) - http.HandleFunc("/search", handleSearch) - http.HandleFunc("/img_proxy", handleImageProxy) - http.HandleFunc("/settings", func(w http.ResponseWriter, r *http.Request) { - http.ServeFile(w, r, "templates/settings.html") - }) - initializeTorrentSites() - fmt.Println("Server is listening on http://localhost:5000") - log.Fatal(http.ListenAndServe(":5000", nil)) -} - func handleSearch(w http.ResponseWriter, r *http.Request) { query, safe, lang, searchType, page := parseSearchParams(r) @@ -133,3 +120,24 @@ func parsePageParameter(pageStr string) int { } return page } + +func runServer() { + http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) + http.HandleFunc("/", handleSearch) + http.HandleFunc("/search", handleSearch) + http.HandleFunc("/img_proxy", handleImageProxy) + http.HandleFunc("/settings", func(w http.ResponseWriter, r *http.Request) { + http.ServeFile(w, r, "templates/settings.html") + }) + http.HandleFunc("/opensearch.xml", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/opensearchdescription+xml") + http.ServeFile(w, r, "static/opensearch.xml") + }) + initializeTorrentSites() + + config := loadConfig() + generateOpenSearchXML(config) + + fmt.Printf("Server is listening on http://localhost:%d\n", config.Port) + log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil)) +} diff --git a/open-search.go b/open-search.go new file mode 100644 index 0000000..b685e42 --- /dev/null +++ b/open-search.go @@ -0,0 +1,50 @@ +package main + +import ( + "encoding/xml" + "fmt" + "os" +) + +type OpenSearchDescription struct { + XMLName xml.Name `xml:"OpenSearchDescription"` + Xmlns string `xml:"xmlns,attr"` + ShortName string `xml:"ShortName"` + Description string `xml:"Description"` + Tags string `xml:"Tags"` + URL URL `xml:"Url"` +} + +type URL struct { + Type string `xml:"type,attr"` + Template string `xml:"template,attr"` +} + +func generateOpenSearchXML(config Config) { + opensearch := OpenSearchDescription{ + Xmlns: "http://a9.com/-/spec/opensearch/1.1/", + ShortName: "Ocásek", + Description: "Search engine", + Tags: "search, engine", + URL: URL{ + Type: "text/html", + Template: fmt.Sprintf("https://%s/search?q={searchTerms}", config.OpenSearch.Domain), + }, + } + + file, err := os.Create("static/opensearch.xml") + if err != nil { + fmt.Println("Error creating OpenSearch file:", err) + return + } + defer file.Close() + + enc := xml.NewEncoder(file) + enc.Indent(" ", " ") + if err := enc.Encode(opensearch); err != nil { + fmt.Println("Error encoding OpenSearch XML:", err) + return + } + + fmt.Println("OpenSearch description file generated successfully.") +} diff --git a/run.sh b/run.sh index 9b6d4d8..da5bcae 100755 --- a/run.sh +++ b/run.sh @@ -1,3 +1,7 @@ -#!/bin/bash +#!/bin/sh -go run main.go images.go imageproxy.go video.go map.go text.go text-searchxng.go text-librex.go text-google.go cache.go forums.go files.go files-torrentgalaxy.go files-thepiratebay.go agent.go --debug \ No newline at end of file +# Find all .go files in the current directory +GO_FILES=$(find . -name '*.go' -print) + +# Run the Go program +go run $GO_FILES diff --git a/search-engine.go b/search-engine.go new file mode 100644 index 0000000..36dde9a --- /dev/null +++ b/search-engine.go @@ -0,0 +1,90 @@ +package main + +import ( + "math/rand" + "sync" + "time" +) + +var ( + searchEngineLock sync.Mutex +) + +// SearchEngine struct now includes metrics for calculating reputation. +type SearchEngine struct { + Name string + Func func(string, string, string, int) ([]SearchResult, time.Duration, error) + Weight int + TotalRequests int + TotalTime time.Duration + SuccessfulSearches int + FailedSearches int +} + +// init function seeds the random number generator. +func init() { + rand.Seed(time.Now().UnixNano()) +} + +// Selects a search engine based on weighted random selection with dynamic weighting. +func selectSearchEngine(engines []SearchEngine) SearchEngine { + searchEngineLock.Lock() + defer searchEngineLock.Unlock() + + // Recalculate weights based on average response time and success rate. + for i := range engines { + engines[i].Weight = calculateReputation(engines[i]) + } + + totalWeight := 0 + for _, engine := range engines { + totalWeight += engine.Weight + } + + randValue := rand.Intn(totalWeight) + for _, engine := range engines { + if randValue < engine.Weight { + return engine + } + randValue -= engine.Weight + } + + return engines[0] // fallback to the first engine +} + +// Updates the engine's performance metrics. +func updateEngineMetrics(engine *SearchEngine, responseTime time.Duration, success bool) { + searchEngineLock.Lock() + defer searchEngineLock.Unlock() + + engine.TotalRequests++ + engine.TotalTime += responseTime + if success { + engine.SuccessfulSearches++ + } else { + engine.FailedSearches++ + } + engine.Weight = calculateReputation(*engine) +} + +// Calculates the reputation of the search engine based on average response time and success rate. +func calculateReputation(engine SearchEngine) int { + const referenceTime = time.Second // 1 second reference time in nanoseconds (1000 ms) + + if engine.TotalRequests == 0 { + return 10 // Default weight for new engines + } + + // Calculate average response time in seconds. + avgResponseTime := engine.TotalTime.Seconds() / float64(engine.TotalRequests) + + // Calculate success rate. + successRate := float64(engine.SuccessfulSearches) / float64(engine.TotalRequests) + + // Combine response time and success rate into a single reputation score. + // The formula can be adjusted to weigh response time and success rate differently. + reputation := (referenceTime.Seconds() / avgResponseTime) * successRate + + // Scale reputation for better interpretability (e.g., multiply by 10) + return int(reputation * 10) +} diff --git a/templates/files.html b/templates/files.html index d3e8701..67ed60d 100644 --- a/templates/files.html +++ b/templates/files.html @@ -5,6 +5,7 @@ {{.Query}} - Ocásek +
diff --git a/templates/forums.html b/templates/forums.html index 4501a9b..c7752aa 100644 --- a/templates/forums.html +++ b/templates/forums.html @@ -5,6 +5,7 @@ {{.Query}} - Ocásek + diff --git a/templates/images.html b/templates/images.html index a49c217..3ae4a86 100644 --- a/templates/images.html +++ b/templates/images.html @@ -5,6 +5,7 @@ {{.Query}} - Ocásek + diff --git a/templates/map.html b/templates/map.html index 747d568..f698229 100644 --- a/templates/map.html +++ b/templates/map.html @@ -5,6 +5,7 @@ {{ .Query }} - Ocásek +