Reuse http.Client

This commit is contained in:
Cat /dev/Nulo 2023-01-04 13:36:25 -03:00
parent 7a638ae6b3
commit 8f5ecef858
6 changed files with 32 additions and 17 deletions

View file

@ -1,12 +1,16 @@
package common package common
import ( import (
"net/url"
"strings" "strings"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
) )
type Result uint8 type Result uint8
type Responder interface {
Respond(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) Result
}
const ( const (
NotValid Result = iota NotValid Result = iota

View file

@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"io" "io"
"net/http"
"net/url" "net/url"
"path" "path"
) )
@ -35,7 +34,7 @@ type lookupResponse struct {
Text string Text string
} }
func lookup(urlSrc string) (lookupResponse, error) { func (r *Instagram) lookup(urlSrc string) (lookupResponse, error) {
urlSrcParsed, err := url.Parse(urlSrc) urlSrcParsed, err := url.Parse(urlSrc)
if err != nil { if err != nil {
return lookupResponse{}, err 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}") 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() url.RawQuery = query.Encode()
resp, err := http.Get(url.String()) resp, err := r.Client.Get(url.String())
if err != nil { if err != nil {
return lookupResponse{}, err return lookupResponse{}, err
} }

View file

@ -2,6 +2,7 @@ package instagram
import ( import (
"log" "log"
"net/http"
"net/url" "net/url"
"strings" "strings"
@ -10,7 +11,13 @@ import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" 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" { if url.Hostname() != "instagram.com" && url.Hostname() != "www.instagram.com" {
return common.NotValid 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()) log.Printf("Downloading %s", url.String())
lookup, err := lookup(url.String()) lookup, err := r.lookup(url.String())
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return common.HadError return common.HadError

13
main.go
View file

@ -12,9 +12,8 @@ import (
"nulo.in/dlbot/tiktok" "nulo.in/dlbot/tiktok"
) )
type Responder func(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) common.Result
type Config struct { type Config struct {
Responders []Responder Responders []common.Responder
} }
func (config Config) handleMessage(bot *tgbotapi.BotAPI, update tgbotapi.Update) { 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 var result common.Result
for _, respond := range config.Responders { for _, responder := range config.Responders {
result = respond(bot, update, url) result = responder.Respond(bot, update, url)
if result != common.NotValid { if result != common.NotValid {
break break
} }
@ -76,9 +75,9 @@ func (config Config) handleMessage(bot *tgbotapi.BotAPI, update tgbotapi.Update)
func main() { func main() {
config := Config{ config := Config{
Responders: []Responder{ Responders: []common.Responder{
instagram.Respond, instagram.Responder,
tiktok.Respond, tiktok.Responder,
}, },
} }

View file

@ -2,6 +2,7 @@ package tiktok
import ( import (
"log" "log"
"net/http"
"net/url" "net/url"
"nulo.in/dlbot/common" "nulo.in/dlbot/common"
@ -12,7 +13,13 @@ import (
// Gracias a https://github.com/Xenzi-XN1/Tiktok-Download // Gracias a https://github.com/Xenzi-XN1/Tiktok-Download
// por enseñarme tikmate.app // 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" { if url.Hostname() != "vm.tiktok.com" && url.Hostname() != "tiktok.com" {
return common.NotValid return common.NotValid
} }
@ -22,7 +29,7 @@ func Respond(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) common.
url.Host = "vm.tiktok.com" url.Host = "vm.tiktok.com"
log.Printf("Downloading %s", urlString) log.Printf("Downloading %s", urlString)
lookup, err := lookup(urlString) lookup, err := r.lookup(urlString)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return common.HadError return common.HadError

View file

@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"io" "io"
"net/http"
"net/url" "net/url"
) )
@ -22,8 +21,8 @@ type lookupResponse struct {
Token string `json:"token"` Token string `json:"token"`
} }
func lookup(urlS string) (string, error) { func (r *TikTok) lookup(urlS string) (string, error) {
resp, err := http.PostForm( resp, err := r.Client.PostForm(
"https://api.tikmate.app/api/lookup", "https://api.tikmate.app/api/lookup",
url.Values{"url": {urlS}}, url.Values{"url": {urlS}},
) )