Compare commits

...

2 Commits

Author SHA1 Message Date
Astra 2035967c0e Update log levels, add safe search toggle 2022-10-14 23:33:34 +00:00
Astra a3060fe3ad Update go.{mod,sum} 2022-10-14 23:33:12 +00:00
3 changed files with 15 additions and 19 deletions

13
go.mod
View File

@ -1,14 +1,11 @@
module git.zio.sh/astra/ddgimagebot module git.zio.sh/astravexton/ddgimagebot
go 1.19 go 1.18
require (
git.zio.sh/astra/telegram-bot-api v1.0.1-0.20220824135549-c84603be8b0f
github.com/astravexton/logger v0.2.2-0.20211216142523-c1e08a465709
github.com/dghubble/sling v1.4.0
)
require ( require (
git.zio.sh/astra/telegram-bot-api v1.0.1-0.20220824135549-c84603be8b0f // indirect
github.com/astravexton/logger v0.2.1 // indirect
github.com/dghubble/sling v1.4.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.3.0 // indirect github.com/google/uuid v1.3.0 // indirect
github.com/technoweenie/multipartstreamer v1.0.1 // indirect github.com/technoweenie/multipartstreamer v1.0.1 // indirect

8
go.sum
View File

@ -4,21 +4,13 @@ git.zio.sh/astra/telegram-bot-api v3.0.0+incompatible h1:OirhOPiMKlH9M8WCILFX69S
git.zio.sh/astra/telegram-bot-api v3.0.0+incompatible/go.mod h1:kTm5G2y4tw4tlnUps6VTc5QwNEhdgTD+qErZhd+2W0o= git.zio.sh/astra/telegram-bot-api v3.0.0+incompatible/go.mod h1:kTm5G2y4tw4tlnUps6VTc5QwNEhdgTD+qErZhd+2W0o=
github.com/astravexton/logger v0.2.1 h1:nYxhV6oCHIJzUzaPhawbAc51EEvJowIViVsB5EbdeJQ= github.com/astravexton/logger v0.2.1 h1:nYxhV6oCHIJzUzaPhawbAc51EEvJowIViVsB5EbdeJQ=
github.com/astravexton/logger v0.2.1/go.mod h1:6uxVhLlWlvZn48W8F52dsMXt4yztlrInpRhdWiJ9RXc= github.com/astravexton/logger v0.2.1/go.mod h1:6uxVhLlWlvZn48W8F52dsMXt4yztlrInpRhdWiJ9RXc=
github.com/astravexton/logger v0.2.2-0.20211216142523-c1e08a465709 h1:S+dBCax7wdg3ikWx5t6cClPKQIerrmD404qAzWhO2V4=
github.com/astravexton/logger v0.2.2-0.20211216142523-c1e08a465709/go.mod h1:6BlG5E0U3CImXLmDtGpEwdoc8PHiiDv0kbvJBCbfiMY=
github.com/dghubble/sling v1.4.0 h1:/n8MRosVTthvMbwlNZgLx579OGVjUOy3GNEv5BIqAWY= github.com/dghubble/sling v1.4.0 h1:/n8MRosVTthvMbwlNZgLx579OGVjUOy3GNEv5BIqAWY=
github.com/dghubble/sling v1.4.0/go.mod h1:0r40aNsU9EdDUVBNhfCstAtFgutjgJGYbO1oNzkMoM8= github.com/dghubble/sling v1.4.0/go.mod h1:0r40aNsU9EdDUVBNhfCstAtFgutjgJGYbO1oNzkMoM8=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/kris-nova/lolgopher v0.0.0-20210112022122-73f0047e8b65/go.mod h1:V0HF/ZBlN86HqewcDC/cVxMmYDiRukWjSrgKLUAn9Js=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM= github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM=
github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog= github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

13
main.go
View File

@ -53,7 +53,7 @@ type Params struct {
func main() { func main() {
logger.Layout = time.Stamp logger.Layout = time.Stamp
logger.BitwiseLevel = logger.LogEverything logger.BitwiseLevel = logger.LogInfo | logger.LogWarning | logger.LogCritical
bot, err := tgbotapi.NewBotAPI(os.Getenv("BOT_TOKEN")) bot, err := tgbotapi.NewBotAPI(os.Getenv("BOT_TOKEN"))
if err != nil { if err != nil {
logger.Critical(err.Error()) logger.Critical(err.Error())
@ -71,7 +71,7 @@ func main() {
for update := range updates { for update := range updates {
if update.InlineQuery != nil { if update.InlineQuery != nil {
if update.InlineQuery.Query != "" { if update.InlineQuery.Query != "" {
logger.Info("Inline query %q from %s", update.InlineQuery.Query, update.InlineQuery.From) logger.Debug("Inline query %q from %s", update.InlineQuery.Query, update.InlineQuery.From)
var results []interface{} var results []interface{}
images := getImageList(update.InlineQuery.Query) images := getImageList(update.InlineQuery.Query)
@ -100,6 +100,13 @@ func main() {
} }
func getImageList(query string) []Results { func getImageList(query string) []Results {
safeSearch := "-1" // -1 = safe off, 1 = safe on
if len(query) >= 8 {
if strings.HasPrefix(query, "safe:on") {
safeSearch = "1"
query = query[8:]
}
}
params := &Params{Query: query} params := &Params{Query: query}
s := sling.New().Get(ddgURL) 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.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")
@ -132,7 +139,7 @@ func getImageList(query string) []Results {
Q: query, Q: query,
VQD: token, VQD: token,
F: ",,,,,", F: ",,,,,",
P: "-1", P: safeSearch,
S: "0", S: "0",
U: "bing", U: "bing",
} }