From 8f5ecef858fca81edae0d3dfc3fd3e11cba13363 Mon Sep 17 00:00:00 2001 From: Nulo Date: Wed, 4 Jan 2023 13:36:25 -0300 Subject: [PATCH] Reuse http.Client --- common/main.go | 4 ++++ instagram/instagram.go | 5 ++--- instagram/main.go | 11 +++++++++-- main.go | 13 ++++++------- tiktok/main.go | 11 +++++++++-- tiktok/tikmate.go | 5 ++--- 6 files changed, 32 insertions(+), 17 deletions(-) diff --git a/common/main.go b/common/main.go index adee096..21c473f 100644 --- a/common/main.go +++ b/common/main.go @@ -1,12 +1,16 @@ package common import ( + "net/url" "strings" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" ) type Result uint8 +type Responder interface { + Respond(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) Result +} const ( NotValid Result = iota diff --git a/instagram/instagram.go b/instagram/instagram.go index 7770845..dd28eeb 100644 --- a/instagram/instagram.go +++ b/instagram/instagram.go @@ -4,7 +4,6 @@ import ( "encoding/json" "errors" "io" - "net/http" "net/url" "path" ) @@ -35,7 +34,7 @@ type lookupResponse struct { Text string } -func lookup(urlSrc string) (lookupResponse, error) { +func (r *Instagram) lookup(urlSrc string) (lookupResponse, error) { urlSrcParsed, err := url.Parse(urlSrc) if err != nil { return lookupResponse{}, err @@ -46,7 +45,7 @@ func lookup(urlSrc string) (lookupResponse, error) { query.Add("variables", "{\"shortcode\":\""+path.Base(urlSrcParsed.Path)+"\",\"child_comment_count\":3,\"fetch_comment_count\":40,\"parent_comment_count\":24,\"has_threaded_comments\":true}") url.RawQuery = query.Encode() - resp, err := http.Get(url.String()) + resp, err := r.Client.Get(url.String()) if err != nil { return lookupResponse{}, err } diff --git a/instagram/main.go b/instagram/main.go index 03aec8c..c37b71f 100644 --- a/instagram/main.go +++ b/instagram/main.go @@ -2,6 +2,7 @@ package instagram import ( "log" + "net/http" "net/url" "strings" @@ -10,7 +11,13 @@ import ( tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" ) -func Respond(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) common.Result { +type Instagram struct { + http.Client +} + +var Responder *Instagram = &Instagram{} + +func (r *Instagram) Respond(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) common.Result { if url.Hostname() != "instagram.com" && url.Hostname() != "www.instagram.com" { return common.NotValid } @@ -19,7 +26,7 @@ func Respond(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) common. } log.Printf("Downloading %s", url.String()) - lookup, err := lookup(url.String()) + lookup, err := r.lookup(url.String()) if err != nil { log.Println(err) return common.HadError diff --git a/main.go b/main.go index b91b601..644442f 100644 --- a/main.go +++ b/main.go @@ -12,9 +12,8 @@ import ( "nulo.in/dlbot/tiktok" ) -type Responder func(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) common.Result type Config struct { - Responders []Responder + Responders []common.Responder } func (config Config) handleMessage(bot *tgbotapi.BotAPI, update tgbotapi.Update) { @@ -48,8 +47,8 @@ func (config Config) handleMessage(bot *tgbotapi.BotAPI, update tgbotapi.Update) } var result common.Result - for _, respond := range config.Responders { - result = respond(bot, update, url) + for _, responder := range config.Responders { + result = responder.Respond(bot, update, url) if result != common.NotValid { break } @@ -76,9 +75,9 @@ func (config Config) handleMessage(bot *tgbotapi.BotAPI, update tgbotapi.Update) func main() { config := Config{ - Responders: []Responder{ - instagram.Respond, - tiktok.Respond, + Responders: []common.Responder{ + instagram.Responder, + tiktok.Responder, }, } diff --git a/tiktok/main.go b/tiktok/main.go index 13c51d9..381c7a5 100644 --- a/tiktok/main.go +++ b/tiktok/main.go @@ -2,6 +2,7 @@ package tiktok import ( "log" + "net/http" "net/url" "nulo.in/dlbot/common" @@ -12,7 +13,13 @@ import ( // Gracias a https://github.com/Xenzi-XN1/Tiktok-Download // por enseƱarme tikmate.app -func Respond(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) common.Result { +type TikTok struct { + http.Client +} + +var Responder *TikTok = &TikTok{} + +func (r *TikTok) Respond(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) common.Result { if url.Hostname() != "vm.tiktok.com" && url.Hostname() != "tiktok.com" { return common.NotValid } @@ -22,7 +29,7 @@ func Respond(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) common. url.Host = "vm.tiktok.com" log.Printf("Downloading %s", urlString) - lookup, err := lookup(urlString) + lookup, err := r.lookup(urlString) if err != nil { log.Println(err) return common.HadError diff --git a/tiktok/tikmate.go b/tiktok/tikmate.go index 5bc328f..41be301 100644 --- a/tiktok/tikmate.go +++ b/tiktok/tikmate.go @@ -4,7 +4,6 @@ import ( "encoding/json" "errors" "io" - "net/http" "net/url" ) @@ -22,8 +21,8 @@ type lookupResponse struct { Token string `json:"token"` } -func lookup(urlS string) (string, error) { - resp, err := http.PostForm( +func (r *TikTok) lookup(urlS string) (string, error) { + resp, err := r.Client.PostForm( "https://api.tikmate.app/api/lookup", url.Values{"url": {urlS}}, )