82 lines
1.9 KiB
Go
82 lines
1.9 KiB
Go
package ytsclient
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
|
|
r "github.com/levigross/grequests"
|
|
)
|
|
|
|
|
|
var ErrNotFound = errors.New("not found")
|
|
|
|
type SearchResponse struct {
|
|
Status string `json:"status"`
|
|
Data struct {
|
|
Query struct {
|
|
Locale string `json:"locale"`
|
|
Query string `json:"query"`
|
|
Offset int `json:"offset"`
|
|
} `json:"query"`
|
|
Cache struct {
|
|
Key string `json:"key"`
|
|
Created int `json:"created"`
|
|
Expiration int `json:"expiration"`
|
|
Status string `json:"status"`
|
|
Age int `json:"age"`
|
|
Version int `json:"version"`
|
|
System string `json:"system"`
|
|
Mode int `json:"mode"`
|
|
} `json:"cache"`
|
|
Result struct {
|
|
Total int `json:"total"`
|
|
Items []struct {
|
|
Title string `json:"title"`
|
|
Favicon string `json:"favicon"`
|
|
URL string `json:"url"`
|
|
Source string `json:"source"`
|
|
Desc string `json:"desc"`
|
|
ID string `json:"_id"`
|
|
Score int `json:"score"`
|
|
Position int `json:"position"`
|
|
} `json:"items"`
|
|
Filters struct {
|
|
Freshness struct {
|
|
Label string `json:"label"`
|
|
Name string `json:"name"`
|
|
Type string `json:"type"`
|
|
Selected string `json:"selected"`
|
|
Values []struct {
|
|
Value string `json:"value"`
|
|
Label string `json:"label"`
|
|
Translate bool `json:"translate"`
|
|
} `json:"values"`
|
|
} `json:"freshness"`
|
|
} `json:"filters"`
|
|
Ads []interface{} `json:"ads"`
|
|
} `json:"result"`
|
|
} `json:"data"`
|
|
}
|
|
|
|
|
|
func ResolveYts() (url string, err error) {
|
|
res, err := r.Get("https://api.qwant.com/api/search/web?count=10&q=yts&t=web&uiv=4", nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
sr := &SearchResponse{}
|
|
if err := res.JSON(sr); err != nil{
|
|
return "", err
|
|
}
|
|
if len(sr.Data.Result.Items) == 0 {
|
|
return "", ErrNotFound
|
|
}
|
|
for _, v := range sr.Data.Result.Items {
|
|
if !strings.HasPrefix(v.URL, "https://yts.") {
|
|
continue
|
|
}
|
|
return v.URL, nil
|
|
}
|
|
return "", ErrNotFound
|
|
}
|