diff options
author | Evgeny Kuznetsov <evgeny@kuznetsov.md> | 2019-06-23 17:55:55 +0300 |
---|---|---|
committer | Evgeny Kuznetsov <evgeny@kuznetsov.md> | 2019-06-23 17:55:55 +0300 |
commit | 3843274200d64ed1fa8a36625457ee351dd78a27 (patch) | |
tree | 5e65fd88259190e7c19738813fd8519e29b1b2cd /main.go | |
parent | 6692e82ad3326e6179f286fc625aa4793b1c55c8 (diff) | |
download | radiorus-rss-3843274200d64ed1fa8a36625457ee351dd78a27.tar.gz radiorus-rss-3843274200d64ed1fa8a36625457ee351dd78a27.zip |
wait for 15 minutes and retry until page makes sense
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 117 |
1 files changed, 61 insertions, 56 deletions
@@ -61,68 +61,73 @@ func main() { programUrl := "http://www.radiorus.ru/brand/" + programNumber + "/episodes" - programPage := getPage(programUrl) - - feed.Title = string(programNameRe.FindSubmatch(programPage)[1]) - feed.Link = &feeds.Link{Href: programUrl} - programImage := programImageRe.FindSubmatch(programPage) - feed.Image = &feeds.Image{ - Link: programUrl, - Url: string(programImage[2]), - Title: string(programImage[4]), - } + for { + programPage := getPage(programUrl) + + feed.Title = string(programNameRe.FindSubmatch(programPage)[1]) + feed.Link = &feeds.Link{Href: programUrl} + programImage := programImageRe.FindSubmatch(programPage) + feed.Image = &feeds.Image{ + Link: programUrl, + Url: string(programImage[2]), + Title: string(programImage[4]), + } - episodes := episodeRe.FindAll(programPage, -1) + episodes := episodeRe.FindAll(programPage, -1) - programAboutUrl := "http://www.radiorus.ru/brand/" + programNumber + "/about" - programAboutPage := getPage(programAboutUrl) - programAbout := programAboutRe.FindSubmatch(programAboutPage)[1] - re := regexp.MustCompile(`<(.+?)?>`) - feed.Description = string(re.ReplaceAll(programAbout, []byte(``))) + programAboutUrl := "http://www.radiorus.ru/brand/" + programNumber + "/about" + programAboutPage := getPage(programAboutUrl) + programAbout := programAboutRe.FindSubmatch(programAboutPage)[1] + re := regexp.MustCompile(`<(.+?)?>`) + feed.Description = string(re.ReplaceAll(programAbout, []byte(``))) - for _, episode := range episodes { - if len(episodeUrlRe.FindAllSubmatch(episode, -1)) > 1 { - log.Fatal("Page looks strange. Episode in progress?") - } - 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) + for _, episode := range episodes { + if len(episodeUrlRe.FindAllSubmatch(episode, -1)) > 1 { + log.Println("Page looks strange. Episode in progress?") + time.Sleep(15 * 60 * time.Second) + continue } + 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) + + episodePage := getPage(episodeUrl) + episodeDesc := string(episodeDescRe.FindSubmatch(episodePage)[1]) + + 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, + Description: episodeDesc, + }) } - 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) - - episodePage := getPage(episodeUrl) - episodeDesc := string(episodeDescRe.FindSubmatch(episodePage)[1]) - - 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, - Description: episodeDesc, - }) - } - 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) + 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) + } + break } } |