From 8f44886d0114de8a4a432e28de930eae0928f709 Mon Sep 17 00:00:00 2001 From: Evgeny Kuznetsov Date: Tue, 4 Feb 2020 17:19:36 +0300 Subject: decouple enclosure finding --- main.go | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 5d7115b..a105576 100644 --- a/main.go +++ b/main.go @@ -44,7 +44,6 @@ var ( programNameRe = regexp.MustCompile(`

(.+?)?

`) programAboutRe = regexp.MustCompile(`(?s)
(.+?)?
`) programImageRe = regexp.MustCompile(`(?s)
(.+?)?`) - episodeAudioRe = regexp.MustCompile(`data\-id="(.+?)?">`) episodeDateRe = regexp.MustCompile(`brand\-time brand\-menu\-link">(.+?)?\.(.+?)?\.(.+?)? в (.+?)?:(.+?)?`) episodeDescRe = regexp.MustCompile(`

(.+?)?

`) episodeTitleRe = regexp.MustCompile(`title brand\-menu\-link">(.+?)?`) @@ -141,7 +140,7 @@ func populateFeed(feed *feeds.Feed, page []byte) (err error) { } episodeUrl := urlPrefix + string(episodeUrlRe.FindSubmatch(episode)[1]) episodeTitle := string(episodeTitleRe.FindSubmatch(episode)[1]) - episodeAudioUrl := "https://audio.vgtrk.com/download?id=" + string(episodeAudioRe.FindSubmatch(episode)[1]) + enclosure := findEnclosure(episode) dateBytes := episodeDateRe.FindSubmatch(episode) var date [5]int for i, b := range dateBytes[1:] { @@ -155,20 +154,26 @@ func populateFeed(feed *feeds.Feed, page []byte) (err error) { episodeDate := time.Date(date[2], time.Month(date[1]), date[0], date[3], date[4], 0, 0, moscow) feed.Add(&feeds.Item{ - Id: episodeID(episodeUrl), - Link: &feeds.Link{Href: episodeUrl}, - Title: episodeTitle, - Enclosure: &feeds.Enclosure{ - Url: episodeAudioUrl, - Length: "1024", - Type: "audio/mpeg", - }, - Created: episodeDate, + Id: episodeID(episodeUrl), + Link: &feeds.Link{Href: episodeUrl}, + Title: episodeTitle, + Enclosure: enclosure, + Created: episodeDate, }) } return nil } +func findEnclosure(ep []byte) *feeds.Enclosure { + episodeAudioRe := regexp.MustCompile(`data\-id="(.+?)?">`) + episodeAudioUrl := "https://audio.vgtrk.com/download?id=" + string(episodeAudioRe.FindSubmatch(ep)[1]) + return &feeds.Enclosure{ + Url: episodeAudioUrl, + Length: "1024", + Type: "audio/mpeg", + } +} + func findEpisodes(page []byte) [][]byte { episodeRe := regexp.MustCompile(`(?s)
(.+?)?data-id="(.+?)">
`) episodes := episodeRe.FindAll(page, -1) -- cgit v1.2.3