Update main.go
This commit is contained in:
parent
869938b061
commit
51550386d2
1 changed files with 82 additions and 81 deletions
159
main.go
159
main.go
|
|
@ -1,54 +1,47 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
tgbotapi "git.zio.sh/astra/telegram-bot-api"
|
tgbotapi "git.zio.sh/astra/telegram-bot-api"
|
||||||
"github.com/astravexton/logger"
|
"github.com/astravexton/logger"
|
||||||
"github.com/dghubble/sling"
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
const ddgURL = "https://duckduckgo.com"
|
const openverseURL = "https://api.openverse.org/v1/images/"
|
||||||
|
|
||||||
// ImgResult ...
|
var httpClient = &http.Client{Timeout: 5 * time.Second}
|
||||||
type ImgResult struct {
|
|
||||||
Ads interface{} `json:"ads"`
|
type openverseResponse struct {
|
||||||
Next string `json:"next"`
|
ResultCount int `json:"result_count"`
|
||||||
Query string `json:"query"`
|
Results []openverseImage `json:"results"`
|
||||||
QueryEncoded string `json:"queryEncoded"`
|
}
|
||||||
ResponseType string `json:"response_type"`
|
|
||||||
Results []Results `json:"results"`
|
type openverseImage struct {
|
||||||
Vqd struct {
|
URL string `json:"url"`
|
||||||
Testing string `json:"testing"`
|
Thumbnail string `json:"thumbnail"`
|
||||||
} `json:"vqd"`
|
Title string `json:"title"`
|
||||||
|
Width int `json:"width"`
|
||||||
|
Height int `json:"height"`
|
||||||
|
ForeignLandingURL string `json:"foreign_landing_url"`
|
||||||
|
Source string `json:"source"`
|
||||||
|
Filetype string `json:"filetype"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Results struct {
|
type Results struct {
|
||||||
Height int `json:"height"`
|
Height int
|
||||||
Image string `json:"image"`
|
Image string
|
||||||
Source string `json:"source"`
|
Source string
|
||||||
Thumbnail string `json:"thumbnail"`
|
Thumbnail string
|
||||||
Title string `json:"title"`
|
Title string
|
||||||
URL string `json:"url"`
|
URL string
|
||||||
Width int `json:"width"`
|
Width int
|
||||||
}
|
Filetype string
|
||||||
|
|
||||||
type Params struct {
|
|
||||||
Query string `url:"q,omitempty"`
|
|
||||||
L string `url:"l,omitempty"`
|
|
||||||
O string `url:"o,omitempty"`
|
|
||||||
Q string `url:"q,omitempty"`
|
|
||||||
VQD string `url:"vqd,omitempty"`
|
|
||||||
F string `url:"f,omitempty"`
|
|
||||||
P string `url:"p,omitempty"`
|
|
||||||
S string `url:"s,omitempty"`
|
|
||||||
U string `url:"u,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
@ -80,7 +73,7 @@ func main() {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
var result interface{}
|
var result interface{}
|
||||||
if strings.HasSuffix(image.Image, ".gif") {
|
if image.Filetype == "gif" || strings.HasSuffix(strings.ToLower(image.Image), ".gif") {
|
||||||
result = &tgbotapi.InlineQueryResultGIF{
|
result = &tgbotapi.InlineQueryResultGIF{
|
||||||
Type: "gif",
|
Type: "gif",
|
||||||
ID: uuid.New().String(),
|
ID: uuid.New().String(),
|
||||||
|
|
@ -100,7 +93,7 @@ func main() {
|
||||||
inlineConf := tgbotapi.InlineConfig{
|
inlineConf := tgbotapi.InlineConfig{
|
||||||
InlineQueryID: update.InlineQuery.ID,
|
InlineQueryID: update.InlineQuery.ID,
|
||||||
IsPersonal: false,
|
IsPersonal: false,
|
||||||
CacheTime: 1,
|
CacheTime: 300,
|
||||||
Results: results,
|
Results: results,
|
||||||
}
|
}
|
||||||
bot.Request(inlineConf)
|
bot.Request(inlineConf)
|
||||||
|
|
@ -109,61 +102,69 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getImageList(query string) []Results {
|
func getImageList(query string) []Results {
|
||||||
safeSearch := "-1" // -1 = safe off, 1 = safe on
|
mature := "true"
|
||||||
if len(query) >= 8 {
|
|
||||||
if strings.HasPrefix(query, "safe:on") {
|
if strings.HasPrefix(query, "safe:on") {
|
||||||
safeSearch = "1"
|
mature = "false"
|
||||||
query = query[8:]
|
query = strings.TrimSpace(strings.TrimPrefix(query, "safe:on"))
|
||||||
}
|
}
|
||||||
|
if query == "" {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
params := &Params{Query: query}
|
|
||||||
s := sling.New().Get(ddgURL)
|
|
||||||
s.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36")
|
|
||||||
s.QueryStruct(params)
|
|
||||||
|
|
||||||
req, err := s.Request()
|
// Anonymous requests are capped at 20; a registered token allows more.
|
||||||
|
token := os.Getenv("OPENVERSE_TOKEN")
|
||||||
|
pageSize := "20"
|
||||||
|
if token != "" {
|
||||||
|
pageSize = "50"
|
||||||
|
}
|
||||||
|
|
||||||
|
params := url.Values{}
|
||||||
|
params.Set("q", query)
|
||||||
|
params.Set("page_size", pageSize)
|
||||||
|
params.Set("mature", mature)
|
||||||
|
|
||||||
|
req, err := http.NewRequest(http.MethodGet, openverseURL+"?"+params.Encode(), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Critical(err.Error())
|
logger.Warning("building request: %s", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
req.Header.Set("User-Agent", "telegram-imagesearch-bot/1.0")
|
||||||
|
if token != "" {
|
||||||
|
req.Header.Set("Authorization", "Bearer "+token)
|
||||||
}
|
}
|
||||||
|
|
||||||
client := &http.Client{}
|
res, err := httpClient.Do(req)
|
||||||
client.Timeout = time.Second * 3
|
|
||||||
|
|
||||||
res, err := client.Do(req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Critical(err.Error())
|
logger.Warning("openverse request failed: %s", err)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
|
|
||||||
body, _ := io.ReadAll(res.Body)
|
if res.StatusCode != http.StatusOK {
|
||||||
bodyContents := string(body)
|
logger.Warning("openverse returned %s for %q", res.Status, query)
|
||||||
|
return nil
|
||||||
r := regexp.MustCompile(`vqd=([\d-]+)`)
|
|
||||||
token := strings.ReplaceAll(r.FindString(bodyContents), "vqd=", "")
|
|
||||||
|
|
||||||
params = &Params{
|
|
||||||
L: "us-en",
|
|
||||||
O: "json",
|
|
||||||
Q: query,
|
|
||||||
VQD: token,
|
|
||||||
F: ",,,,,",
|
|
||||||
P: safeSearch,
|
|
||||||
S: "0",
|
|
||||||
U: "bing",
|
|
||||||
}
|
|
||||||
s = s.Get(ddgURL).Path("/i.js").QueryStruct(params)
|
|
||||||
s.Set("Authority", "duckduckgo.com")
|
|
||||||
s.Set("Accept", "application/json, text/javascript, */*; q=0.01")
|
|
||||||
s.Set("Cookie", "bc=1; at=-1; au=-1; av=1; ax=-1; ak=-1; ao=-1; ap=-1; 1=-1; 5=1; aq=-1; ay=b; ah=uk-en; l=wt-wt; p=-2")
|
|
||||||
s.Set("Referer", "https://duckduckgo.com/")
|
|
||||||
|
|
||||||
imageList := &ImgResult{}
|
|
||||||
_, err = s.ReceiveSuccess(imageList)
|
|
||||||
if err != nil {
|
|
||||||
logger.Critical(err.Error())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return imageList.Results
|
var data openverseResponse
|
||||||
|
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
||||||
|
logger.Warning("decoding openverse response: %s", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
results := make([]Results, 0, len(data.Results))
|
||||||
|
for _, img := range data.Results {
|
||||||
|
results = append(results, Results{
|
||||||
|
Height: img.Height,
|
||||||
|
Image: img.URL,
|
||||||
|
Source: img.Source,
|
||||||
|
Thumbnail: img.Thumbnail,
|
||||||
|
Title: img.Title,
|
||||||
|
URL: img.ForeignLandingURL,
|
||||||
|
Width: img.Width,
|
||||||
|
Filetype: img.Filetype,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return results
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue