Instagram bot

This commit is contained in:
Cat /dev/Nulo 2023-01-02 23:30:01 -03:00
parent fb2d827e97
commit 22a472ad1d
6 changed files with 109 additions and 0 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
tiktok/tiktok
instagram/instagram

View file

@ -22,3 +22,6 @@ tasks:
- task: upload
vars:
WHICH: tiktok
- task: upload
vars:
WHICH: instagram

10
instagram/go.mod Normal file
View file

@ -0,0 +1,10 @@
module nulo.in/dlbot/instagram
go 1.19
replace nulo.in/dlbot/common => ../common
require (
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
nulo.in/dlbot/common v0.0.0-00010101000000-000000000000
)

2
instagram/go.sum Normal file
View file

@ -0,0 +1,2 @@
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=

60
instagram/instagram.go Normal file
View file

@ -0,0 +1,60 @@
package main
import (
"encoding/json"
"io"
"net/http"
"net/url"
"path"
)
type QueryResponse struct {
Data struct {
ShortcodeMedia struct {
VideoUrl string `json:"video_url"`
Owner struct {
Username string `json:"username"`
} `json:"owner"`
EdgeMediaToCaption struct {
Edges []struct {
Node struct {
Text string `json:"text`
} `json:"node"`
} `json:"edges"`
} `json:"edge_media_to_caption"`
} `json:"shortcode_media"`
} `json:"data"`
}
type Response struct {
VideoUrl string
Author string
Text string
}
func Lookup(urlSrc string) (Response, error) {
url, _ := url.Parse("https://www.instagram.com/graphql/query/?query_hash=b3055c01b4b222b8a47dc12b090e4e64")
query := url.Query()
query.Add("variables", "{\"shortcode\":\""+path.Base(urlSrc)+"\",\"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())
if err != nil {
return Response{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
var response QueryResponse
err = json.Unmarshal(body, &response)
if err != nil {
return Response{}, err
}
return Response{
VideoUrl: response.Data.ShortcodeMedia.VideoUrl,
Author: response.Data.ShortcodeMedia.Owner.Username,
Text: response.Data.ShortcodeMedia.EdgeMediaToCaption.Edges[0].Node.Text,
}, nil
}

33
instagram/main.go Normal file
View file

@ -0,0 +1,33 @@
package main
import (
"log"
"net/url"
"nulo.in/dlbot/common"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
func 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
}
log.Printf("Downloading %s", url.String())
lookup, err := Lookup(url.String())
if err != nil {
return common.HadError
}
log.Println(lookup)
res := tgbotapi.NewVideo(update.Message.Chat.ID, tgbotapi.FileURL(lookup.VideoUrl))
res.ReplyToMessageID = update.Message.MessageID
res.Caption = "@" + lookup.Author + ": " + lookup.Text
bot.Send(res)
return common.Uploaded
}
func main() {
common.Main(common.Config{Respond: respond})
}