Implement basic rss reader structure

This commit is contained in:
2025-04-13 17:17:00 +02:00
parent 586c49558e
commit 3a74d63963
4 changed files with 234 additions and 0 deletions

2
.gitignore vendored
View File

@@ -9,3 +9,5 @@ downloader/vendor/github.com/*
downloader/vendor/modules.txt
downloader/ytdl.exe
downloader/ytdl.log
*.log
*.xml

3
youtubeWatcher/go.mod Normal file
View File

@@ -0,0 +1,3 @@
module ywatcher
go 1.24.2

132
youtubeWatcher/main.go Normal file
View File

@@ -0,0 +1,132 @@
package main
import (
"fmt"
"io"
"log"
"os"
"regexp"
"sync"
"time"
)
type DownloadRequest struct {
Link string `json:"link"`
}
type RssWatcher struct {
Feed *RssFeed
}
var videoRegex = regexp.MustCompile(`yt:video:(?<videoid>[^ ]+) (?:[^ ]+ ){2}(?<videotitle>.+?)https(?:[^ ]+ ){2}(?<date>[^ ]+)`)
var feeds []*RssFeed = []*RssFeed{
{Url: "https://www.youtube.com/feeds/videos.xml?channel_id=UCMwJJL5FJFuTRT55ksbQ4GQ", Id: "@AsmongoldClips"},
// {Url: "https://www.youtube.com/feeds/videos.xml?channel_id=UC8nZUXCwCTffxthKLtOp6ng", Id: "@Splattercatgaming"},
// {Url: "https://www.youtube.com/feeds/videos.xml?channel_id=UC2THf0jmDDeBujMzG1sD2-Q", Id: "@thesingleplayersquad"},
// {Url: "https://www.youtube.com/feeds/videos.xml?channel_id=UCmtyQOKKmrMVaKuRXz02jbQ", Id: "@SebastianLague"},
// {Url: "https://www.youtube.com/feeds/videos.xml?channel_id=UCywBfpGBYhsczNuyyh6Cf6w", Id: "@WorthABuyreviews"},
}
func (w *RssWatcher) Watch(videoUrls chan string) error {
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
log.Printf("Watcher for feed %s started, checking every minute.", w.Feed.Id)
w.CheckFeed(videoUrls)
for {
select {
case <-ticker.C:
log.Printf("Checking feed: %s", w.Feed.Url)
err := w.CheckFeed(videoUrls)
if err != nil {
log.Printf("Error checking feed %s: %v", w.Feed.Id, err)
return fmt.Errorf("watcher %s failed to check feed: %w", w.Feed.Id, err)
}
log.Printf("Successfully checked feed: %s", w.Feed.Url)
}
}
}
func (w *RssWatcher) CheckFeed(videoUrls chan string) error {
// log.Printf("Checking feed URL: %s", w.Feed.Url)
// resp, err := http.Get(w.Feed.Url)
// if err != nil {
// log.Printf("Error creating request for feed %s: %v", w.Feed.Id, err)
// return fmt.Errorf("failed to create request: %w", err)
// }
// defer resp.Body.Close()
// log.Printf("Received response with status code: %d", resp.StatusCode)
// body, err := io.ReadAll(resp.Body)
// if err != nil {
// log.Printf("Error reading response body for feed %s: %v", w.Feed.Id, err)
// return fmt.Errorf("failed to read response body: %w", err)
// }
// os.WriteFile("cache.xml", body, 0644)
body, err := os.ReadFile("cache.xml")
if err != nil {
return fmt.Errorf("failed to read cache file: %w", err)
}
matches := videoRegex.FindAllStringSubmatch(string(body), -1)
for _, match := range matches {
log.Println(match[1])
}
return nil
}
var Error *log.Logger
var Warning *log.Logger
func init() {
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
logFile, err := os.Create("ywatcher.log")
if err != nil {
log.Printf("Error creating log file: %v", err)
os.Exit(1)
}
logger := io.MultiWriter(os.Stdout, logFile)
log.SetOutput(logger)
Error = log.New(io.MultiWriter(logFile, os.Stderr, os.Stdout),
fmt.Sprintf("%sERROR:%s ", "\033[0;101m", "\033[0m"),
log.Lmicroseconds|log.Lshortfile)
Warning = log.New(io.MultiWriter(logFile, os.Stdout),
fmt.Sprintf("%sWarning:%s ", "\033[0;93m", "\033[0m"),
log.Lmicroseconds|log.Lshortfile)
}
func main() {
videoUrls := make(chan string, 12)
wg := &sync.WaitGroup{}
for _, feed := range feeds {
wg.Add(1)
go func(feed *RssFeed) {
err := feed.UpdateLastSeen()
if err != nil {
Error.Printf("failed to update lastseen for feed %s: %v", feed.Id, err)
panic(err)
}
defer wg.Done()
watcher := RssWatcher{
Feed: feed,
}
err = watcher.Watch(videoUrls)
if err != nil {
Error.Printf("watcher %s failed to watch feed: %v", feed.Id, err)
panic(err)
}
}(feed)
}
go func() {
for videoUrl := range videoUrls {
log.Println(videoUrl)
}
}()
wg.Wait()
}

97
youtubeWatcher/rssfeed.go Normal file
View File

@@ -0,0 +1,97 @@
package main
import (
"fmt"
"io"
"log"
"os"
"sync"
"time"
)
type RssFeed struct {
Url string
Id string
LastSeen time.Time
fileMutex sync.Mutex
}
func (f *RssFeed) UpdateLastSeen() error {
log.Printf("[%s]: Updating last seen time", f.Id)
lastSeen, err := f.ReadLastSeen()
if err != nil {
return fmt.Errorf("failed to read lastseen file for feed %s: %w", f.Id, err)
}
f.LastSeen = lastSeen
log.Printf("[%s]: Last seen time updated to: %s", f.Id, f.LastSeen)
return nil
}
func (f *RssFeed) ReadLastSeen() (time.Time, error) {
f.fileMutex.Lock()
defer f.fileMutex.Unlock()
lastSeen := time.Now()
log.Printf("[%s]: Attempting to open lastseen file...", f.Id)
lastSeenFile, err := os.Open("lastseen")
if err != nil {
if os.IsNotExist(err) {
log.Printf("[%s]: lastseen file does not exist, creating a new one...", f.Id)
lastSeenFile, err = os.Create("lastseen" + f.Id)
if err != nil {
return lastSeen, fmt.Errorf("failed to create lastseen file: %w", err)
}
log.Printf("[%s]: Writing current time to lastseen file...", f.Id)
lastSeenFile.Write([]byte(lastSeen.Format(time.RFC3339)))
lastSeenFile.Sync()
log.Printf("[%s]: Successfully created lastseen file with current time.", f.Id)
return lastSeen, nil
} else {
log.Printf("[%s]: Error opening lastseen file: %v", f.Id, err)
return lastSeen, fmt.Errorf("failed to read lastseen file: %w", err)
}
}
log.Printf("[%s]: Reading contents of lastseen file...", f.Id)
lastSeenBytes, err := io.ReadAll(lastSeenFile)
if err != nil {
log.Printf("[%s]: Error reading lastseen file: %v", f.Id, err)
return lastSeen, fmt.Errorf("failed to read lastseen file: %w", err)
}
log.Printf("[%s]: Parsing lastseen time...", f.Id)
lastSeen, err = time.Parse(time.RFC3339, string(lastSeenBytes))
if err != nil {
log.Printf("[%s]: Error parsing lastseen file: %v", f.Id, err)
return lastSeen, fmt.Errorf("failed to parse lastseen file: %w", err)
}
lastSeenFile.Close()
log.Printf("[%s]: Last seen time read: %s", f.Id, lastSeen)
return lastSeen, nil
}
func (f *RssFeed) WriteLastSeen(when time.Time) error {
f.fileMutex.Lock()
defer f.fileMutex.Unlock()
log.Printf("[%s]: Attempting to create lastseen file...", f.Id)
lastSeenFile, err := os.Create("lastseen" + f.Id)
if err != nil {
log.Printf("[%s]: Error creating lastseen file: %v", f.Id, err)
return fmt.Errorf("failed to create lastseen file: %w", err)
}
log.Printf("[%s]: Successfully created lastseen file: lastseen%s", f.Id, f.Id)
log.Printf("[%s]: Writing last seen time to file: %s", f.Id, when.Format(time.RFC3339))
_, err = lastSeenFile.Write([]byte(when.Format(time.RFC3339)))
if err != nil {
log.Printf("[%s]: Error writing to lastseen file: %v", f.Id, err)
return fmt.Errorf("failed to write to lastseen file: %w", err)
}
log.Printf("[%s]: Successfully wrote last seen time to file.", f.Id)
err = lastSeenFile.Close()
if err != nil {
log.Printf("[%s]: Error closing lastseen file: %v", f.Id, err)
return fmt.Errorf("failed to close lastseen file: %w", err)
}
log.Printf("[%s]: Lastseen file closed successfully.", f.Id)
return nil
}