telegram bot added

This commit is contained in:
milarin 2023-01-15 20:59:54 +01:00
parent 1026be5bd0
commit 53288c56a3
9 changed files with 75 additions and 24 deletions

View File

@ -57,6 +57,7 @@ func CheckTorrents() {
collectionPreferred := props.Priority >= torrentPrio.Priority collectionPreferred := props.Priority >= torrentPrio.Priority
collectionPreferredTorrents[torrentPrio.ParsedTorrent] = collectionPreferred // debug output collectionPreferredTorrents[torrentPrio.ParsedTorrent] = collectionPreferred // debug output
if collectionPreferred { if collectionPreferred {
SendTelegramAnimeEpMessage(TelegramDownloadMessagePattern, animeEp)
continue continue
} }
} }

View File

@ -11,6 +11,8 @@ import (
) )
func DownloadTorrent(animeEp model.AnimeEpisode, torrent *model.ParsedTorrent) error { func DownloadTorrent(animeEp model.AnimeEpisode, torrent *model.ParsedTorrent) error {
SendTelegramAnimeEpMessage(TelegramDownloadMessagePattern, animeEp)
if err := SetCurrentlyDownloading(animeEp); err != nil { if err := SetCurrentlyDownloading(animeEp); err != nil {
return ErrLockFileCreationFailed.Wrap(err, animeEp.Anime.Title.Romaji, animeEp.Episode) return ErrLockFileCreationFailed.Wrap(err, animeEp.Anime.Title.Romaji, animeEp.Episode)
} }

View File

@ -1,10 +1,11 @@
package main package main
import ( import (
"html/template" "text/template"
"time" "time"
"git.milar.in/milarin/envvars/v2" "git.milar.in/milarin/envvars/v2"
"git.milar.in/nyaanime/logic"
) )
var ( var (
@ -13,10 +14,16 @@ var (
TorrentPath = envvars.String("TORRENT_PATH", "") TorrentPath = envvars.String("TORRENT_PATH", "")
DebugAnimeEpisodePattern = envvars.Object( DebugAnimeEpisodePattern = envvars.Object(
"DEBUG_ANIME_LANGUAGE_PATTERN", "DEBUG_ANIME_EPISODE_PATTERN",
template.Must(template.New("anime-episode-filepath-pattern").Parse(`{{.Title.UserPreferred}} episode {{.Episode}}`)), template.Must(template.New("anime-episode-filepath-pattern").Parse(`{{.Title.UserPreferred}} episode {{.Episode}}`)),
template.New("anime-episode-pattern").Parse, template.New("anime-episode-filepath-pattern").Parse,
) )
TelegramDownloadMessagePatternStr = logic.EscSeqReplacer.Replace(envvars.String("TELEGRAM_DOWNLOAD_MESSAGE_PATTERN", `Download started\n{{.Anime.Title.UserPreferred}}{{.Episode}}`))
TelegramDownloadMessagePattern = template.Must(template.New("telegram-download-message-pattern").Parse(TelegramDownloadMessagePatternStr))
DownloadAll = envvars.Bool("DOWNLOAD_ALL_ANIMES", false) DownloadAll = envvars.Bool("DOWNLOAD_ALL_ANIMES", false)
TelegramBotToken = envvars.String("TELEGRAM_API_TOKEN", "")
TelegramChatID = envvars.Int64("TELEGRAM_CHAT_ID", 0)
) )

6
go.mod
View File

@ -8,11 +8,12 @@ require (
git.milar.in/milarin/envvars/v2 v2.0.0 git.milar.in/milarin/envvars/v2 v2.0.0
git.milar.in/milarin/slices v0.0.6 git.milar.in/milarin/slices v0.0.6
git.milar.in/milarin/tprint v0.0.7 git.milar.in/milarin/tprint v0.0.7
git.milar.in/nyaanime/logic v0.0.0-20230114105336-6bfb7dce349f git.milar.in/nyaanime/logic v0.0.0-20230115195351-8ed3e407ee72
git.milar.in/nyaanime/model v0.0.0-20230113095840-5eb2822653c3 git.milar.in/nyaanime/model v0.0.0-20230113095840-5eb2822653c3
git.milar.in/nyaanime/parsers v0.0.0-20230113101942-2c9bc6925201 git.milar.in/nyaanime/parsers v0.0.0-20230115135225-d80026a240a2
github.com/PuerkitoBio/goquery v1.8.0 github.com/PuerkitoBio/goquery v1.8.0
github.com/fatih/color v1.13.0 github.com/fatih/color v1.13.0
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
) )
require ( require (
@ -23,6 +24,7 @@ require (
github.com/mattn/go-isatty v0.0.16 // indirect github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/rivo/uniseg v0.4.3 // indirect github.com/rivo/uniseg v0.4.3 // indirect
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
golang.org/x/net v0.0.0-20220812174116-3211cb980234 // indirect golang.org/x/net v0.0.0-20220812174116-3211cb980234 // indirect
golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24 // indirect golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24 // indirect
gopkg.in/vansante/go-ffprobe.v2 v2.1.1 // indirect gopkg.in/vansante/go-ffprobe.v2 v2.1.1 // indirect

12
go.sum
View File

@ -12,18 +12,20 @@ git.milar.in/milarin/slices v0.0.6 h1:AQoSarZ58WHYol9c6woWJSe8wFpPC2RC4cvIlZpfg9
git.milar.in/milarin/slices v0.0.6/go.mod h1:NOr53AOeur/qscu/FBj3lsFR262PNYBccLYSTCAXRk4= git.milar.in/milarin/slices v0.0.6/go.mod h1:NOr53AOeur/qscu/FBj3lsFR262PNYBccLYSTCAXRk4=
git.milar.in/milarin/tprint v0.0.7 h1:dvm4l4BhXOie4vtnRMZii0WTLTz2wju8wyUPB/lNeVo= git.milar.in/milarin/tprint v0.0.7 h1:dvm4l4BhXOie4vtnRMZii0WTLTz2wju8wyUPB/lNeVo=
git.milar.in/milarin/tprint v0.0.7/go.mod h1:UwW/B+0cTCbN5hi0bQBE9rIRgLkq+x4V751rrS2KVoI= git.milar.in/milarin/tprint v0.0.7/go.mod h1:UwW/B+0cTCbN5hi0bQBE9rIRgLkq+x4V751rrS2KVoI=
git.milar.in/nyaanime/logic v0.0.0-20230114105336-6bfb7dce349f h1:tc8hQpZprt8/yedVSPmkftKOI2+P7twFc7BEov5RTBc= git.milar.in/nyaanime/logic v0.0.0-20230115195351-8ed3e407ee72 h1:/kSY7P/NNaUmkxCJMKmB5YbhIA9owX9pErPxREuBAog=
git.milar.in/nyaanime/logic v0.0.0-20230114105336-6bfb7dce349f/go.mod h1:cLuIBDTtpXdzprOIsz73r6ZCE5s0k+o6HA7fH9OuW/M= git.milar.in/nyaanime/logic v0.0.0-20230115195351-8ed3e407ee72/go.mod h1:cLuIBDTtpXdzprOIsz73r6ZCE5s0k+o6HA7fH9OuW/M=
git.milar.in/nyaanime/model v0.0.0-20230113095840-5eb2822653c3 h1:mXcEA47FQQzeSDXE3UvhNfIt4fBfpDSq1/f0r+jbHpY= git.milar.in/nyaanime/model v0.0.0-20230113095840-5eb2822653c3 h1:mXcEA47FQQzeSDXE3UvhNfIt4fBfpDSq1/f0r+jbHpY=
git.milar.in/nyaanime/model v0.0.0-20230113095840-5eb2822653c3/go.mod h1:kPWLDvFrhc1Uf77gxsBOxNeJ5JTVF2HhVs1IdVcw0tg= git.milar.in/nyaanime/model v0.0.0-20230113095840-5eb2822653c3/go.mod h1:kPWLDvFrhc1Uf77gxsBOxNeJ5JTVF2HhVs1IdVcw0tg=
git.milar.in/nyaanime/parsers v0.0.0-20230113101942-2c9bc6925201 h1:glTG4IeuIvD4mVwJyCF5SYMawCRcZZ01pz4AUyWTEP8= git.milar.in/nyaanime/parsers v0.0.0-20230115135225-d80026a240a2 h1:Q95JBR9mXENAjRhvzPAsFjPfxY0ljUiLVlhfAO4q6UY=
git.milar.in/nyaanime/parsers v0.0.0-20230113101942-2c9bc6925201/go.mod h1:GG4vtUIfxopZc/+Y8OAa//vWJw/m6aeoGN7fw6SLiEM= git.milar.in/nyaanime/parsers v0.0.0-20230115135225-d80026a240a2/go.mod h1:GG4vtUIfxopZc/+Y8OAa//vWJw/m6aeoGN7fw6SLiEM=
github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U= github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U=
github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI= github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI=
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c= github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA= github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
@ -36,6 +38,8 @@ github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw= github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM=
github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog=
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220812174116-3211cb980234 h1:RDqmgfe7SvlMWoqC3xwQ2blLO3fcWcxMa3eBLRdRW7E= golang.org/x/net v0.0.0-20220812174116-3211cb980234 h1:RDqmgfe7SvlMWoqC3xwQ2blLO3fcWcxMa3eBLRdRW7E=
golang.org/x/net v0.0.0-20220812174116-3211cb980234/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= golang.org/x/net v0.0.0-20220812174116-3211cb980234/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=

View File

@ -5,17 +5,10 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"git.milar.in/milarin/anilist"
"git.milar.in/nyaanime/logic" "git.milar.in/nyaanime/logic"
"git.milar.in/nyaanime/model" "git.milar.in/nyaanime/model"
) )
type AnimePathPatternData struct {
Title anilist.MediaTitle
Episode int
Ext string
}
func AnimeEpExistsLocally(animeEp model.AnimeEpisode) bool { func AnimeEpExistsLocally(animeEp model.AnimeEpisode) bool {
animeEpPath := logic.GetAnimeEpFilepath(animeEp, "*") animeEpPath := logic.GetAnimeEpFilepath(animeEp, "*")

13
main.go
View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"os/exec" "os/exec"
"time" "time"
@ -16,14 +15,14 @@ func main() {
// get access token once at startup to be sure that an access token is obtainable at all // get access token once at startup to be sure that an access token is obtainable at all
if _, err := logic.GetAnilistAccessToken(); err != nil { if _, err := logic.GetAnilistAccessToken(); err != nil {
panic(err) panic(err) // TODO error handling
} }
fmt.Println("generated priority values:") if err := InitTelegramBot(); err != nil {
fmt.Print(Map2Table("language", logic.PreferredLanguages)) panic(err) // TODO error handling
fmt.Print(Map2Table("subtitle", logic.PreferredSubtitles)) }
fmt.Print(Map2Table("resolution", logic.PreferredResolutions))
fmt.Println() logic.PrintPriorityTables()
ticker := time.NewTicker(PollRate) ticker := time.NewTicker(PollRate)
defer ticker.Stop() defer ticker.Stop()

View File

@ -63,7 +63,7 @@ func ShowDebugInfo(
} }
b := new(strings.Builder) b := new(strings.Builder)
DebugAnimeEpisodePattern.Execute(b, AnimePathPatternData{Title: animeEp.Anime.Title, Episode: animeEp.Episode}) DebugAnimeEpisodePattern.Execute(b, animeEp)
header := BoldText.Sprintf("%s (%s)", color.MagentaString(b.String()), epState) header := BoldText.Sprintf("%s (%s)", color.MagentaString(b.String()), epState)
fmt.Println(tprint.FormatHeaderTable(header, table)) fmt.Println(tprint.FormatHeaderTable(header, table))
} }

43
telegram.go Normal file
View File

@ -0,0 +1,43 @@
package main
import (
"strings"
"text/template"
"git.milar.in/milarin/adverr"
"git.milar.in/nyaanime/model"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
var TelegramBot *tgbotapi.BotAPI
func InitTelegramBot() error {
if TelegramBotToken != "" && TelegramChatID != 0 {
bot, err := tgbotapi.NewBotAPI(TelegramBotToken)
if err != nil {
return err
}
TelegramBot = bot
}
return nil
}
func SendTelegramMessage(text string) {
if TelegramBot == nil {
return
}
msg := tgbotapi.NewMessage(TelegramChatID, text)
_, err := TelegramBot.Send(msg)
if err != nil {
adverr.Println(adverr.Wrap("could not send telegram message", err))
}
}
func SendTelegramAnimeEpMessage(messagePattern *template.Template, animeEp model.AnimeEpisode) {
b := new(strings.Builder)
if err := messagePattern.Execute(b, animeEp); err != nil {
adverr.Println(adverr.Wrap("could not send telegram message", err))
}
SendTelegramMessage(b.String())
}