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
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

View file

@ -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
}

View file

@ -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

13
main.go
View file

@ -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,
},
}

View file

@ -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

View file

@ -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}},
)