2022-04-26 09:55:59 -04:00
|
|
|
package app
|
|
|
|
|
2022-05-01 15:32:41 -04:00
|
|
|
import (
|
|
|
|
"io"
|
2022-05-01 23:28:54 -04:00
|
|
|
"net/http"
|
|
|
|
"time"
|
2022-05-01 15:32:41 -04:00
|
|
|
|
|
|
|
"go.balki.me/tss/log"
|
2022-05-03 17:14:37 -04:00
|
|
|
"go.balki.me/tss/parser"
|
2022-05-01 15:32:41 -04:00
|
|
|
"go.balki.me/tss/proxy"
|
2022-05-01 23:28:54 -04:00
|
|
|
"go.balki.me/tss/telegram"
|
2022-05-01 15:32:41 -04:00
|
|
|
)
|
2022-04-28 19:24:21 -04:00
|
|
|
|
2022-04-27 00:19:05 -04:00
|
|
|
func Run(configPath string) {
|
2022-04-28 19:24:21 -04:00
|
|
|
cfg, err := ParseConfig(configPath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Panic("failed to parse config", "path", configPath, "err", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
scheduler, err := NewScheduler(cfg.LastSuccessPath)
|
2022-05-01 23:28:54 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Panic("failed to create scheduler", "path", cfg.LastSuccessPath, "error", err)
|
|
|
|
}
|
2022-04-28 19:24:21 -04:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := scheduler.Save(); err != nil {
|
|
|
|
log.Panic("failed to save last success info", "path", cfg.LastSuccessPath, "err", err)
|
2022-04-27 00:19:05 -04:00
|
|
|
}
|
2022-04-28 19:24:21 -04:00
|
|
|
}()
|
2022-04-26 09:55:59 -04:00
|
|
|
|
2022-05-01 23:28:54 -04:00
|
|
|
tgramProxy, err := proxy.GetTransport(cfg.TelegramProxy)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic("failed to get proxy transport", "proxyURL", cfg.TelegramProxy, "error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tgram := telegram.NewTelegramSender(tgramProxy, cfg.TelegramAuthToken)
|
2022-05-01 19:13:27 -04:00
|
|
|
|
2022-04-28 19:24:21 -04:00
|
|
|
for _, feed := range cfg.Feeds {
|
|
|
|
log.Info("processing feed", "feed", feed.Name)
|
2022-05-01 23:28:54 -04:00
|
|
|
ProcessFeed(feed, scheduler, cfg.DbDir, tgram)
|
2022-04-28 19:24:21 -04:00
|
|
|
}
|
2022-04-26 09:55:59 -04:00
|
|
|
}
|
2022-04-28 19:24:21 -04:00
|
|
|
|
2022-05-02 20:20:48 -04:00
|
|
|
func ProcessFeed(feed FeedCfg, scheduler Scheduler, dbDir string, tgram telegram.TelegramSender) {
|
2022-04-28 19:24:21 -04:00
|
|
|
sd, err := scheduler.ShouldDownload(feed.Name, feed.Cron)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("shouldDownload failed", "feed", feed.Name, "err", err)
|
|
|
|
return
|
|
|
|
}
|
2022-05-01 15:32:41 -04:00
|
|
|
|
2022-04-28 19:24:21 -04:00
|
|
|
if !sd {
|
|
|
|
log.Info("skipping feed due to schedule", "feed", feed.Name)
|
|
|
|
return
|
|
|
|
}
|
2022-05-01 15:32:41 -04:00
|
|
|
|
|
|
|
db, err := NewDB(dbDir, feed.Name)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to get db", "feed", feed.Name, "db_dir", dbDir, "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := Download(feed.Url, feed.Proxy)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("download failed", "feed", feed.Name, "url", feed.Url, "proxy", feed.Proxy, "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-03 17:14:37 -04:00
|
|
|
entries, err := parser.ParseFeed(feed.Type, data)
|
2022-04-28 19:24:21 -04:00
|
|
|
if err != nil {
|
2022-05-01 15:32:41 -04:00
|
|
|
log.Error("feed parsing failed", "feed", feed.Name, "data", data, "error", err)
|
2022-04-28 19:24:21 -04:00
|
|
|
return
|
|
|
|
}
|
2022-05-01 15:32:41 -04:00
|
|
|
|
2022-05-02 00:36:27 -04:00
|
|
|
scheduler.Good(feed.Name)
|
|
|
|
|
2022-05-02 23:23:56 -04:00
|
|
|
var records []Record
|
2022-05-03 17:14:37 -04:00
|
|
|
var newEntries []parser.FeedEntry
|
2022-05-02 23:23:56 -04:00
|
|
|
if db.IsNewFeed() {
|
|
|
|
ftl := int(feed.FirstTimeLimit)
|
|
|
|
if feed.FirstTimeLimit == NoLimit || len(entries) <= ftl {
|
|
|
|
newEntries = entries
|
|
|
|
} else {
|
2022-05-03 17:14:37 -04:00
|
|
|
var filteredEntries []parser.FeedEntry
|
2022-05-02 23:23:56 -04:00
|
|
|
newEntries, filteredEntries = entries[:ftl], entries[ftl:]
|
|
|
|
for _, entry := range filteredEntries {
|
|
|
|
records = append(records, Record{
|
|
|
|
Time: time.Now(),
|
|
|
|
Status: Filtered,
|
|
|
|
Filter: "FirstTime",
|
|
|
|
FeedEntry: entry,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newEntries, err = db.Filter(entries)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to filter entries", "feed", feed.Name, "error", err)
|
|
|
|
}
|
2022-05-01 15:32:41 -04:00
|
|
|
}
|
2022-05-01 23:28:54 -04:00
|
|
|
|
2022-05-02 23:23:56 -04:00
|
|
|
for _, entry := range newEntries {
|
2022-05-01 23:28:54 -04:00
|
|
|
r := Record{
|
2022-05-02 23:23:56 -04:00
|
|
|
Time: time.Now(),
|
2022-05-01 23:28:54 -04:00
|
|
|
FeedEntry: entry,
|
|
|
|
}
|
2022-05-07 21:01:14 -04:00
|
|
|
err := tgram.SendLink(entry.Link, feed.Channel, feed.Rhash, feed.Title)
|
2022-05-01 23:28:54 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to send to telegram", "feed", feed.Name, "link", entry.Link, "channel", feed.Channel, "rhash", feed.Rhash, "error", err)
|
|
|
|
r.Status = Error
|
|
|
|
} else {
|
|
|
|
r.Status = Sent
|
|
|
|
}
|
|
|
|
records = append(records, r)
|
|
|
|
}
|
|
|
|
err = db.Save(records)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to save sent records", "feed", feed.Name, "num_records", len(records), "error", err)
|
|
|
|
}
|
2022-05-01 15:32:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func Download(url string, proxyUrl string) ([]byte, error) {
|
2022-05-01 23:28:54 -04:00
|
|
|
transport, err := proxy.GetTransport(proxyUrl)
|
2022-05-01 15:32:41 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-01 23:28:54 -04:00
|
|
|
client := &http.Client{Transport: transport}
|
2022-05-01 15:32:41 -04:00
|
|
|
res, err := client.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
return io.ReadAll(res.Body)
|
2022-04-28 19:24:21 -04:00
|
|
|
}
|