diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | go.mod | 3 | ||||
-rw-r--r-- | go.sum | 2 | ||||
-rw-r--r-- | main.go | 100 |
4 files changed, 108 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8567dfb --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.swp +*.rss +radiorus-rss @@ -0,0 +1,3 @@ +module github.com/nekr0z/radiorus-rss + +require github.com/gorilla/feeds v1.1.1 @@ -0,0 +1,2 @@ +github.com/gorilla/feeds v1.1.1 h1:HwKXxqzcRNg9to+BbvJog4+f3s/xzvtZXICcQGutYfY= +github.com/gorilla/feeds v1.1.1/go.mod h1:Nk0jZrvPFZX1OBe5NPiddPw7CfwF6Q9eqzaBbaightA= @@ -0,0 +1,100 @@ +package main + +import ( + "flag" + "github.com/gorilla/feeds" + "io/ioutil" + "log" + "net/http" + "regexp" + "strconv" + "time" +) + +type subst struct { + from string + to string +} + +var ( + substitutes = []subst{ + {from: `"`, to: `"`}, + {from: `–`, to: `–`}, + } + + programNameRe = regexp.MustCompile(`<h2>(.+?)?</h2>`) + episodeRe = regexp.MustCompile(`(?s)<div class="brand__list\-\-wrap\-\-item">(.+?)?<div class="add\-to\-list">`) + episodeAudioRe = regexp.MustCompile(`data\-id="(.+?)?">`) + episodeDateRe = regexp.MustCompile(`brand\-time brand\-menu\-link">(.+?)?\.(.+?)?\.(.+?)? в (.+?)?:(.+?)?</a>`) + episodeTitleRe = regexp.MustCompile(`title brand\-menu\-link">(.+?)?</a>`) + episodeUrlRe = regexp.MustCompile(`<a href="/brand/(.+?)?" class="title`) + + feed = &feeds.Feed{ + Created: time.Now(), + } + outputPath, programNumber string +) + +func main() { + flag.StringVar(&outputPath, "path", "./", "path to put resulting RSS file in") + flag.StringVar(&programNumber, "brand", "57083", "brand number (defaults to Aerostat)") + flag.Parse() + + programUrl := "http://www.radiorus.ru/brand/" + programNumber + "/episodes" + res, err := http.Get(programUrl) + if err != nil { + log.Fatal(err) + } + defer res.Body.Close() + programPage, err := ioutil.ReadAll(res.Body) + if err != nil { + log.Fatal(err) + } + + for _, sub := range substitutes { + re := regexp.MustCompile(sub.from) + programPage = re.ReplaceAll(programPage, []byte(sub.to)) + } + + feed.Title = string(programNameRe.FindSubmatch(programPage)[1]) + feed.Link = &feeds.Link{Href: programUrl} + episodes := episodeRe.FindAll(programPage, -1) + + for _, episode := range episodes { + episodeUrl := "http://www.radiorus.ru/brand/" + string(episodeUrlRe.FindSubmatch(episode)[1]) + episodeTitle := string(episodeTitleRe.FindSubmatch(episode)[1]) + episodeAudioUrl := "https://audio.vgtrk.com/download?id=" + string(episodeAudioRe.FindSubmatch(episode)[1]) + dateBytes := episodeDateRe.FindSubmatch(episode) + var date [5]int + for i, b := range dateBytes[1:] { + date[i], err = strconv.Atoi(string(b)) + if err != nil { + log.Fatal(err) + } + } + moscow := time.FixedZone("Moscow Time", int((3 * time.Hour).Seconds())) + episodeDate := time.Date(date[2], time.Month(date[1]), date[0], date[3], date[4], 0, 0, moscow) + + feed.Add(&feeds.Item{ + Id: episodeUrl, + Link: &feeds.Link{Href: episodeUrl}, + Title: episodeTitle, + Enclosure: &feeds.Enclosure{ + Url: episodeAudioUrl, + Length: "1024", + Type: "audio/mpeg", + }, + Created: episodeDate, + }) + } + + rss, err := feed.ToRss() + if err != nil { + log.Fatal(err) + } + outputFile := outputPath + "radiorus-" + programNumber + ".rss" + output := []byte(rss) + if err := ioutil.WriteFile(outputFile, output, 0644); err != nil { + log.Fatal(err) + } +} |