Implement reading xml rss feed and extracting videos
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}()
|
||||
|
||||
|
86
youtubeWatcher/rssfeedResType.go
Normal file
86
youtubeWatcher/rssfeedResType.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package main
|
||||
|
||||
import "encoding/xml"
|
||||
|
||||
type Feed struct {
|
||||
XMLName xml.Name `xml:"feed"`
|
||||
Text string `xml:",chardata"`
|
||||
Yt string `xml:"yt,attr"`
|
||||
Media string `xml:"media,attr"`
|
||||
Xmlns string `xml:"xmlns,attr"`
|
||||
Link []Link `xml:"link"`
|
||||
ID string `xml:"id"`
|
||||
ChannelId string `xml:"channelId"`
|
||||
Title string `xml:"title"`
|
||||
Author Author `xml:"author"`
|
||||
Published string `xml:"published"`
|
||||
Entry []Entry `xml:"entry"`
|
||||
}
|
||||
|
||||
type Link struct {
|
||||
Text string `xml:",chardata"`
|
||||
Rel string `xml:"rel,attr"`
|
||||
Href string `xml:"href,attr"`
|
||||
}
|
||||
|
||||
type Author struct {
|
||||
Text string `xml:",chardata"`
|
||||
Name string `xml:"name"`
|
||||
URI string `xml:"uri"`
|
||||
}
|
||||
|
||||
type Entry struct {
|
||||
Text string `xml:",chardata"`
|
||||
ID string `xml:"id"`
|
||||
VideoId string `xml:"videoId"`
|
||||
ChannelId string `xml:"channelId"`
|
||||
Title string `xml:"title"`
|
||||
Link Link `xml:"link"`
|
||||
Author Author `xml:"author"`
|
||||
Published string `xml:"published"`
|
||||
Updated string `xml:"updated"`
|
||||
Group Group `xml:"group"`
|
||||
}
|
||||
|
||||
type Group struct {
|
||||
Text string `xml:",chardata"`
|
||||
Title string `xml:"title"`
|
||||
Content Content `xml:"content"`
|
||||
Thumbnail Thumbnail `xml:"thumbnail"`
|
||||
Description string `xml:"description"`
|
||||
Community Community `xml:"community"`
|
||||
}
|
||||
|
||||
type Content struct {
|
||||
Text string `xml:",chardata"`
|
||||
URL string `xml:"url,attr"`
|
||||
Type string `xml:"type,attr"`
|
||||
Width string `xml:"width,attr"`
|
||||
Height string `xml:"height,attr"`
|
||||
}
|
||||
|
||||
type Thumbnail struct {
|
||||
Text string `xml:",chardata"`
|
||||
URL string `xml:"url,attr"`
|
||||
Width string `xml:"width,attr"`
|
||||
Height string `xml:"height,attr"`
|
||||
}
|
||||
|
||||
type Community struct {
|
||||
Text string `xml:",chardata"`
|
||||
StarRating StarRating `xml:"starRating"`
|
||||
Statistics Statistics `xml:"statistics"`
|
||||
}
|
||||
|
||||
type StarRating struct {
|
||||
Text string `xml:",chardata"`
|
||||
Count string `xml:"count,attr"`
|
||||
Average string `xml:"average,attr"`
|
||||
Min string `xml:"min,attr"`
|
||||
Max string `xml:"max,attr"`
|
||||
}
|
||||
|
||||
type Statistics struct {
|
||||
Text string `xml:",chardata"`
|
||||
Views string `xml:"views,attr"`
|
||||
}
|
Reference in New Issue
Block a user