Compare commits

..

4 commits

Author SHA1 Message Date
ee56414b0e Merge pull request 'work' (#2) from work into main
Reviewed-on: #2
2024-06-12 13:00:42 +00:00
partisan
1d17841048 retry anouther search engine when 0 results 2024-06-12 14:51:45 +02:00
partisan
810f57dd77 opensearch.xml generator 2024-06-12 14:26:50 +02:00
admin
d91c275aed added "next" button for videos 2024-06-10 13:12:09 +02:00
16 changed files with 278 additions and 49 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
config.json
opensearch.xml

View file

@ -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

116
init.go Normal file
View file

@ -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
}

34
main.go
View file

@ -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))
}

50
open-search.go Normal file
View file

@ -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.")
}

2
run.sh
View file

@ -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
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

View file

@ -5,6 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{.Query}} - Ocásek</title>
<link rel="stylesheet" type="text/css" href="/static/css/style.css">
<link rel="search" type="application/opensearchdescription+xml" title="Ocásek" href="/opensearch.xml">
</head>
<body>
<form action="/search" id="prev-next-form" class="results-search-container" method="GET" autocomplete="off">

View file

@ -5,6 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{.Query}} - Ocásek</title>
<link rel="stylesheet" type="text/css" href="/static/css/style.css">
<link rel="search" type="application/opensearchdescription+xml" title="Ocásek" href="/opensearch.xml">
</head>
<body>
<form action="/search" id="prev-next-form" class="results-search-container" method="GET" autocomplete="off">

View file

@ -5,6 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{.Query}} - Ocásek</title>
<link rel="stylesheet" type="text/css" href="/static/css/style.css">
<link rel="search" type="application/opensearchdescription+xml" title="Ocásek" href="/opensearch.xml">
</head>
<body>
<form action="/search" id="prev-next-form" class="results-search-container" method="GET" autocomplete="off">

View file

@ -5,6 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ .Query }} - Ocásek</title>
<link rel="stylesheet" href="/static/css/style.css">
<link rel="search" type="application/opensearchdescription+xml" title="Ocásek" href="/opensearch.xml">
<script src="https://cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/leaflet.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/leaflet.css" />
<style>

View file

@ -5,6 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Search with Ocásek</title>
<link rel="stylesheet" href="/static/css/style.css">
<link rel="search" type="application/opensearchdescription+xml" title="Ocásek" href="/opensearch.xml">
</head>
<body>
<div class="settings-search-div settings-search-div-search">

View file

@ -5,6 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Settings - Ocásek</title>
<link rel="stylesheet" type="text/css" href="static/css/style.css">
<link rel="search" type="application/opensearchdescription+xml" title="Ocásek" href="/opensearch.xml">
</head>
<body>
<form action="/search" id="prev-next-form" class="results-search-container" method="GET" autocomplete="off">

View file

@ -5,6 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{.Query}} - Ocásek</title>
<link rel="stylesheet" type="text/css" href="/static/css/style.css">
<link rel="search" type="application/opensearchdescription+xml" title="Ocásek" href="/opensearch.xml">
</head>
<body>
<form action="/search" id="prev-next-form" class="results-search-container" method="GET" autocomplete="off">

View file

@ -5,15 +5,16 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{.Query}} - Ocásek</title>
<link rel="stylesheet" href="/static/css/style.css">
<link rel="search" type="application/opensearchdescription+xml" title="Ocásek" href="/opensearch.xml">
</head>
<body>
<form action="/search" id="prev-next-form" class="results-search-container" method="GET" autocomplete="off">
<h1 class="logomobile"><a class="no-decoration" href="./">Ocásek</a></h1>
<div class="wrapper-results">
<input type="text" name="q" value="{{ .Query }}" id="search-input" placeholder="Type to search..." />
<button id="search-wrapper-ico" class="material-icons-round" name="t" value="video">search</button>
<input type="submit" class="hide" name="t" value="video" />
</div>
<input type="text" name="q" value="{{ .Query }}" id="search-input" placeholder="Type to search..." />
<button id="search-wrapper-ico" class="material-icons-round" name="t" value="video">search</button>
<input type="submit" class="hide" name="t" value="video" />
</div>
<div class="sub-search-button-wrapper">
<div class="search-container-results-btn">
<button id="sub-search-wrapper-ico" class="material-icons-round clickable" name="t" value="text">search</button>
@ -32,17 +33,16 @@
<button name="t" value="forum" class="clickable">Forums</button>
</div>
<div id="content" class="js-enabled">
<div class="search-container-results-btn">
<button id="sub-search-wrapper-ico" class="material-icons-round clickable" name="t" value="map">map</button>
<button name="t" value="map" class="clickable">Maps</button>
<div class="search-container-results-btn">
<button id="sub-search-wrapper-ico" class="material-icons-round clickable" name="t" value="map">map</button>
<button name="t" value="map" class="clickable">Maps</button>
</div>
</div>
</div>
<div class="search-container-results-btn">
<button id="sub-search-wrapper-ico" class="material-icons-round clickable" name="t" value="file">share</button>
<button name="t" value="file" class="clickable">Torrents</button>
</div>
</div>
</div>
</form>
<!-- Results go here -->
<p class="fetched fetched_dif fetched_vid"><!-- { fetched } --></p>
@ -51,11 +51,11 @@
<div>
<div class="video__results">
<div class="video__img__results">
<a href="{{ .Href }}"> <img src="{{ .Image }}">
<div class="duration">{{ .Duration }}</div>
</img></a>
<a href="{{ .Href }}"> <img src="{{ .Image }}">
<div class="duration">{{ .Duration }}</div>
</img></a>
</div>
<div class="results video-results-margin">
<div class="results video-results-margin">
<h3 class="video_title" href="{{ .Href }}">{{ .Title }}</h3></a>
<p class="stats">{{ .Views }} <span class="pipe">|</span> {{ .Date }}</p>
<p class="publish__info">YouTube <span class="pipe">|</span> {{ .Creator }}</p>
@ -64,9 +64,20 @@
</div>
{{ end }}
{{ else }}
<div class="no-results">No results found for '{{ .Query }}'. Try different keywords.</div>>
<div class="no-results">No results found for '{{ .Query }}'. Try different keywords.</div>
{{ end }}
<div class="prev-next prev-img" id="prev-next">
<form action="/search" method="get">
<input type="hidden" name="q" value="{{ .Query }}">
<input type="hidden" name="t" value="video">
{{ if .HasPrevPage }}
<button type="submit" name="p" value="{{ sub .Page 1 }}">Previous</button>
{{ end }}
{{ if .HasNextPage }}
<button type="submit" name="p" value="{{ add .Page 1 }}">Next</button>
{{ end }}
</form>
</div>
<script>
// Check if JavaScript is enabled and modify the DOM accordingly
document.getElementById('content').classList.remove('js-enabled');

33
text.go
View file

@ -69,7 +69,9 @@ func getTextResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string,
case results := <-cacheChan:
if results == nil {
combinedResults = fetchTextResults(query, safe, lang, page)
resultsCache.Set(cacheKey, convertToSearchResults(combinedResults))
if len(combinedResults) > 0 {
resultsCache.Set(cacheKey, convertToSearchResults(combinedResults))
}
} else {
textResults, _, _ := convertToSpecificResults(results)
combinedResults = textResults
@ -77,7 +79,9 @@ func getTextResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string,
case <-time.After(2 * time.Second):
log.Println("Cache check timeout")
combinedResults = fetchTextResults(query, safe, lang, page)
resultsCache.Set(cacheKey, convertToSearchResults(combinedResults))
if len(combinedResults) > 0 {
resultsCache.Set(cacheKey, convertToSearchResults(combinedResults))
}
}
return combinedResults
@ -88,20 +92,31 @@ func prefetchPage(query, safe, lang string, page int) {
if _, exists := resultsCache.Get(cacheKey); !exists {
log.Printf("Page %d not cached, caching now...", page)
pageResults := fetchTextResults(query, safe, lang, page)
resultsCache.Set(cacheKey, convertToSearchResults(pageResults))
if len(pageResults) > 0 {
resultsCache.Set(cacheKey, convertToSearchResults(pageResults))
}
} else {
log.Printf("Page %d already cached", page)
}
}
func fetchTextResults(query, safe, lang string, page int) []TextSearchResult {
engine := selectSearchEngine()
log.Printf("Using search engine: %s", engine.Name)
var results []TextSearchResult
var err error
results, err := engine.Func(query, safe, lang, page)
if err != nil {
log.Printf("Error performing search with %s: %v", engine.Name, err)
return nil
for attempts := 0; attempts < len(searchEngines); attempts++ {
engine := selectSearchEngine()
log.Printf("Using search engine: %s", engine.Name)
results, err = engine.Func(query, safe, lang, page)
if err != nil {
log.Printf("Error performing search with %s: %v", engine.Name, err)
continue
}
if len(results) > 0 {
break
}
}
return results

View file

@ -180,16 +180,23 @@ func handleVideoSearch(w http.ResponseWriter, query, safe, lang string, page int
}
elapsed := time.Since(start)
tmpl, err := template.ParseFiles("templates/videos.html")
tmpl, err := template.New("videos.html").Funcs(funcs).ParseFiles("templates/videos.html")
if err != nil {
log.Printf("Error parsing template: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
tmpl.Execute(w, map[string]interface{}{
err = tmpl.Execute(w, map[string]interface{}{
"Results": results,
"Query": query,
"Fetched": fmt.Sprintf("%.2f seconds", elapsed.Seconds()),
"Page": page,
"HasPrevPage": page > 1,
"HasNextPage": len(results) > 0, // assuming you have a way to determine if there are more pages
})
if err != nil {
log.Printf("Error executing template: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
}