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/images.go b/images.go index f235a85..9d9ef55 100644 --- a/images.go +++ b/images.go @@ -90,7 +90,9 @@ func getImageResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string case results := <-cacheChan: if results == nil { combinedResults = fetchImageResults(query, safe, lang, page) - resultsCache.Set(cacheKey, convertToSearchResults(combinedResults)) + if len(combinedResults) > 0 { + resultsCache.Set(cacheKey, convertToSearchResults(combinedResults)) + } } else { _, _, imageResults := convertToSpecificResults(results) combinedResults = imageResults @@ -98,20 +100,31 @@ func getImageResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string case <-time.After(2 * time.Second): log.Println("Cache check timeout") combinedResults = fetchImageResults(query, safe, lang, page) - resultsCache.Set(cacheKey, convertToSearchResults(combinedResults)) + if len(combinedResults) > 0 { + resultsCache.Set(cacheKey, convertToSearchResults(combinedResults)) + } } return combinedResults } func fetchImageResults(query, safe, lang string, page int) []ImageSearchResult { - engine := selectImageEngine() - log.Printf("Using image search engine: %s", engine.Name) + var results []ImageSearchResult + var err error - results, err := engine.Func(query, safe, lang, page) - if err != nil { - log.Printf("Error performing image search with %s: %v", engine.Name, err) - return nil + for attempts := 0; attempts < len(imageEngines); attempts++ { + engine := selectImageEngine() + log.Printf("Using image search engine: %s", engine.Name) + + results, err = engine.Func(query, safe, lang, page) + if err != nil { + log.Printf("Error performing image search with %s: %v", engine.Name, err) + continue + } + + if len(results) > 0 { + break + } } return results 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 9fa41ee..a845d9f 100755 --- a/run.sh +++ b/run.sh @@ -1,3 +1,3 @@ #!/bin/bash -go run main.go common.go images.go imageproxy.go images-quant.go images-imgur.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 \ No newline at end of file +go run main.go common.go init.go open-search.go images.go imageproxy.go images-quant.go images-imgur.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 \ No newline at end of file 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 +