Implement reading xml rss feed and extracting videos

This commit is contained in:
2025-04-13 17:21:06 +02:00
parent 3a74d63963
commit 91106bd391
2 changed files with 104 additions and 13 deletions

View File

@@ -1,6 +1,7 @@
package main
import (
"encoding/xml"
"fmt"
"io"
"log"
@@ -30,19 +31,18 @@ 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)
log.Printf("[%s]: Watcher started, checking every minute.", w.Feed.Id)
w.CheckFeed(videoUrls)
for {
select {
case <-ticker.C:
log.Printf("Checking feed: %s", w.Feed.Url)
log.Printf("[%s]: Checking feed", w.Feed.Id)
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)
log.Printf("[%s]: Successfully checked feed", w.Feed.Id)
}
}
}
@@ -51,28 +51,33 @@ 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)
// return fmt.Errorf("[%s]: failed to create request: %w", w.Feed.Id, 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)
// return fmt.Errorf("[%s]: failed to read response body: %w", w.Feed.Id, 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)
return fmt.Errorf("[%s]: failed to read cache file: %w", w.Feed.Id, err)
}
matches := videoRegex.FindAllStringSubmatch(string(body), -1)
for _, match := range matches {
log.Println(match[1])
var feed Feed
err = xml.Unmarshal(body, &feed)
if err != nil {
return fmt.Errorf("[%s]: failed to unmarshal feed: %w", w.Feed.Id, err)
}
for _, entry := range feed.Entry {
log.Printf("[%s]: Found new video titled %q with url %q", w.Feed.Id, entry.Title, entry.Link.Href)
videoUrls <- entry.Link.Href
}
return nil
}
@@ -124,7 +129,7 @@ func main() {
go func() {
for videoUrl := range videoUrls {
log.Println(videoUrl)
log.Printf("Got new video with url %q", videoUrl)
}
}()