Add SunXdcc search provider (#1)

main
Totto16 2022-10-21 18:35:11 +02:00 committed by GitHub
parent b773fb330e
commit 3c554922c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 112 additions and 8 deletions

View File

@ -16,6 +16,7 @@ var registry *XdccProviderRegistry = nil
func init() { func init() {
registry = NewProviderRegistry() registry = NewProviderRegistry()
registry.AddProvider(&XdccEuProvider{}) registry.AddProvider(&XdccEuProvider{})
registry.AddProvider(&SunXdccProvider{})
} }
var defaultColWidths []int = []int{100, 10, -1} var defaultColWidths []int = []int{100, 10, -1}

119
search.go
View File

@ -1,7 +1,9 @@
package main package main
import ( import (
"encoding/json"
"errors" "errors"
"fmt"
"log" "log"
"net/http" "net/http"
"strconv" "strconv"
@ -65,10 +67,6 @@ func (registry *XdccProviderRegistry) Search(keywords []string) ([]XdccFileInfo,
return allResults, nil return allResults, nil
} }
type XdccEuProvider struct{}
const XdccEuURL = "https://www.xdcc.eu/search.php"
func parseFileSize(sizeStr string) (int64, error) { func parseFileSize(sizeStr string) (int64, error) {
if len(sizeStr) == 0 { if len(sizeStr) == 0 {
return -1, errors.New("empty string") return -1, errors.New("empty string")
@ -92,11 +90,15 @@ func parseFileSize(sizeStr string) (int64, error) {
return -1, errors.New("unable to parse: " + sizeStr) return -1, errors.New("unable to parse: " + sizeStr)
} }
type XdccEuProvider struct{}
const XdccEuURL = "https://www.xdcc.eu/search.php"
const xdccEuNumberOfEntries = 7 const xdccEuNumberOfEntries = 7
func (p *XdccEuProvider) parseFields(fields []string) (*XdccFileInfo, error) { func (p *XdccEuProvider) parseFields(fields []string) (*XdccFileInfo, error) {
if len(fields) != xdccEuNumberOfEntries { if len(fields) != xdccEuNumberOfEntries {
return nil, errors.New("unespected number of search entry fields") return nil, errors.New("unexpected number of search entry fields")
} }
fInfo := &XdccFileInfo{} fInfo := &XdccFileInfo{}
@ -132,9 +134,8 @@ func (p *XdccEuProvider) Search(keywords []string) ([]XdccFileInfo, error) {
} }
defer res.Body.Close() defer res.Body.Close()
if res.StatusCode != 200 { if res.StatusCode != http.StatusOK {
log.Fatalf("status code error: %d %s", res.StatusCode, res.Status) return nil, errors.New(fmt.Sprintf("status code error: %d %s", res.StatusCode, res.Status))
return nil, err
} }
// Load the HTML document // Load the HTML document
@ -167,3 +168,105 @@ func (p *XdccEuProvider) Search(keywords []string) ([]XdccFileInfo, error) {
}) })
return fileInfos, nil return fileInfos, nil
} }
type SunXdccProvider struct{}
const SunXdccURL = "http://sunxdcc.com/deliver.php"
const SunXdccNumberOfEntries = 8
func (p *SunXdccProvider) parseFields(entry SunXdccEntry, index int) (*XdccFileInfo, error) {
fInfo := &XdccFileInfo{}
fInfo.Network = entry.Network[index]
fInfo.BotName = entry.Bot[index]
fInfo.Channel = entry.Channel[index]
slot, err := strconv.Atoi(entry.Packnum[index][1:])
if err != nil {
return nil, err
}
sizeString := strings.TrimLeft(strings.TrimRight(entry.Fsize[index], "]"), "[")
fInfo.Size, _ = parseFileSize(sizeString) // ignoring error
fInfo.Name = entry.Fname[index]
if err != nil {
return nil, err
}
fInfo.Slot = slot
fInfo.Url = "irc://" + fInfo.Network + "/" + strings.TrimLeft(fInfo.Channel, "#") + "/" + fInfo.BotName + "/" + strconv.Itoa(fInfo.Slot)
return fInfo, nil
}
type SunXdccEntry struct {
Botrec []string
Network []string
Bot []string
Channel []string
Packnum []string
Gets []string
Fsize []string
Fname []string
}
func (p *SunXdccProvider) Search(keywords []string) ([]XdccFileInfo, error) {
keywordString := strings.Join(keywords, " ")
searchkey := strings.Join(strings.Fields(keywordString), "+")
// for API definition use https://sunxdcc.com/#api
res, err := http.Get(SunXdccURL + "?sterm=" + searchkey)
if err != nil {
log.Fatal(err)
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errors.New(fmt.Sprintf("status code error: %d %s", res.StatusCode, res.Status))
}
// Load the JSON Response document
var entry SunXdccEntry
decoder := json.NewDecoder(res.Body)
if err := decoder.Decode(&entry); err != nil {
return nil, err
}
var sizes = [8]int{
len(entry.Botrec),
len(entry.Network),
len(entry.Bot),
len(entry.Channel),
len(entry.Packnum),
len(entry.Gets),
len(entry.Fsize),
len(entry.Fname)}
var length = sizes[0]
for _, l := range sizes {
if length != l {
log.Fatalf("Parse Error, not all fields have the same size")
return nil, errors.New("Parse Error, not all fields have the same size")
}
}
fileInfos := make([]XdccFileInfo, 0)
for i := 0; i < len(entry.Botrec); i++ {
info, err := p.parseFields(entry, i)
if err == nil {
fileInfos = append(fileInfos, *info)
}
}
return fileInfos, nil
}