init
This commit is contained in:
commit
6e2e3460d6
8 changed files with 230 additions and 0 deletions
162
main.go
Normal file
162
main.go
Normal file
|
|
@ -0,0 +1,162 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"runtime"
|
||||
"strconv"
|
||||
|
||||
"github.com/SevereCloud/vksdk/v2/api"
|
||||
"github.com/SevereCloud/vksdk/v2/events"
|
||||
"github.com/SevereCloud/vksdk/v2/longpoll-bot"
|
||||
|
||||
"github.com/s32x/httpclient"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
)
|
||||
|
||||
var video_links chan (string)
|
||||
|
||||
// const workers_count int = 5
|
||||
// const tg_chat_id int64 = -1002135430788
|
||||
// const tg_bot_key string = "6548829570:AAFi_K-YhJwP7asF2-bwSCxrNAkkI7c7Xd0"
|
||||
// const vk_api_key string = "vk1.a.iQX883p6Es-adfHxxEBd1KSEodg79PqVNatiWAadchBPzhYJz2UGE5pgPmXNFlygj3RqTJQ7gB3juUayHFm4yYgV9fE6LPx_z3EkD3L57zXmNrLGjMdSASud4dXe7yfQNQP4OoPmmSAH4MZubPGK1JWWxzRe4wgYaooAA5ehRtcQOYjzser3kfJemt-uvT93ccikgR66QSjq0bGDsmeUyw"
|
||||
|
||||
func main() {
|
||||
// get config
|
||||
mustConfigLoad()
|
||||
// create chan
|
||||
video_links = make(chan string, Conf.Workers*2)
|
||||
// run vk
|
||||
go RunVK()
|
||||
// run tg
|
||||
go RunTG()
|
||||
//
|
||||
select {}
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Workers int
|
||||
TgAPIKey string
|
||||
TgChatID int64
|
||||
VkAPIKey string
|
||||
}
|
||||
|
||||
var Conf Config
|
||||
|
||||
func mustConfigLoad() {
|
||||
var err error
|
||||
Conf.Workers, err = strconv.Atoi(os.Getenv("Workers"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
Conf.TgAPIKey = os.Getenv("TgAPIKey")
|
||||
Conf.VkAPIKey = os.Getenv("VkAPIKey")
|
||||
Conf.TgChatID, err = strconv.ParseInt(os.Getenv("TgChatID"), 10, 64)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func RunVK() {
|
||||
vk := api.NewVK(Conf.VkAPIKey)
|
||||
|
||||
// get information about the group
|
||||
group, err := vk.GroupsGetByID(nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Initializing Long Poll
|
||||
lp, err := longpoll.NewLongPoll(vk, group[0].ID)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// New message event
|
||||
lp.MessageNew(func(_ context.Context, obj events.MessageNewObject) {
|
||||
log.Printf("%d: %s", obj.Message.PeerID, obj.Message.Text)
|
||||
|
||||
for _, obj := range obj.Message.Attachments {
|
||||
objectsw:
|
||||
switch obj.Type {
|
||||
case "story":
|
||||
// fmt.Printf("Story %+v\n", obj.Story.Video.Files)
|
||||
// get video link from struct
|
||||
var videolink string
|
||||
switch {
|
||||
case obj.Story.Video.Files.Mp4_720 != "":
|
||||
videolink = obj.Story.Video.Files.Mp4_720
|
||||
case obj.Story.Video.Files.Mp4_480 != "":
|
||||
videolink = obj.Story.Video.Files.Mp4_480
|
||||
case obj.Story.Video.Files.Mp4_360 != "":
|
||||
videolink = obj.Story.Video.Files.Mp4_360
|
||||
case obj.Story.Video.Files.Mp4_240 != "":
|
||||
videolink = obj.Story.Video.Files.Mp4_240
|
||||
default:
|
||||
error_logging(errors.New("cannot find video link"))
|
||||
break objectsw
|
||||
}
|
||||
// fmt.Println("Story:", videolink)
|
||||
error_logging(err)
|
||||
// send video to tg
|
||||
video_links <- videolink
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Run Bots Long Poll
|
||||
log.Println("[VK] Start Long Poll")
|
||||
if err := lp.Run(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func RunTG() {
|
||||
bot, err := tgbotapi.NewBotAPI(Conf.TgAPIKey)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
bot.Debug = true
|
||||
|
||||
log.Printf("[TG] Authorized on account %s", bot.Self.UserName)
|
||||
|
||||
u := tgbotapi.NewUpdate(0)
|
||||
u.Timeout = 60
|
||||
|
||||
for i := 0; i < Conf.Workers; i++ {
|
||||
go tg_worker(i+1, bot)
|
||||
}
|
||||
}
|
||||
|
||||
func tg_worker(id int, bot *tgbotapi.BotAPI) {
|
||||
for link := range video_links {
|
||||
log.Println(fmt.Sprintf("[%d]", id), "Получена ссылка, загружаю:", link)
|
||||
// get video by link
|
||||
client := httpclient.New().WithHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36")
|
||||
resp, err := client.Get(link).Do()
|
||||
error_logging(err)
|
||||
body, err := resp.Bytes()
|
||||
error_logging(err)
|
||||
msg := tgbotapi.NewVideo(Conf.TgChatID, tgbotapi.FileBytes{Name: "story", Bytes: body})
|
||||
log.Println(fmt.Sprintf("[%d]", id), "Отправляю видео в телеграм...")
|
||||
_, err = bot.Send(msg)
|
||||
error_logging(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Log errors
|
||||
func error_logging(err error) {
|
||||
if err != nil {
|
||||
pc := make([]uintptr, 10)
|
||||
n := runtime.Callers(2, pc)
|
||||
frames := runtime.CallersFrames(pc[:n])
|
||||
frame, _ := frames.Next()
|
||||
// fmt.Printf("%s:%d %s\n", frame.File, frame.Line, frame.Function)
|
||||
log.Printf("[Parser] error on %s line %d: %s", frame.Function, frame.Line, err)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue