// Copyright (C) 2019-2020 Evgeny Kuznetsov (evgeny@kuznetsov.md)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see
(.+?)?
`) episodeTitleRe = regexp.MustCompile(`title brand\-menu\-link">(.+?)?`) episodeUrlRe = regexp.MustCompile(` 1 { return errBadEpisode } url, err := parseSingle(episode, episodeUrlRe) if err != nil { return errBadEpisode } episodeUrl := urlPrefix + string(url) title, _ := parseSingle(episode, episodeTitleRe) episodeTitle := string(title) enclosure := findEnclosure(episode) date := findDate(episode) feed.Add(&feeds.Item{ Id: episodeID(episodeUrl), Link: &feeds.Link{Href: episodeUrl}, Title: episodeTitle, Enclosure: enclosure, Created: date, }) } return } func addFeedImage(page []byte, feed *feeds.Feed) { programImage, err := parse(page, programImageRe, 4) if err == nil { feed.Image = &feeds.Image{ Link: feed.Link.Href, Url: string(programImage[1]), Title: string(programImage[3]), } } } func parse(src []byte, re *regexp.Regexp, n int) (out [][]byte, err error) { match := re.FindSubmatch(src) if len(match) != n+1 { for i := 0; i < n; i++ { out = append(out, []byte{}) } return out, errCantParse } return match[1:], nil } func parseSingle(src []byte, re *regexp.Regexp) (out []byte, err error) { got, err := parse(src, re, 1) out = got[0] return } func findDate(ep []byte) time.Time { episodeDateRe := regexp.MustCompile(`brand\-time brand\-menu\-link">(.+?)?\.(.+?)?\.(.+?)? в (.+?)?:(.+?)?`) dateBytes := episodeDateRe.FindSubmatch(ep) return parseDate(dateBytes) } func parseDate(bytes [][]byte) time.Time { if len(bytes) < 4 { return time.Date(1970, time.January, 1, 0, 0, 0, 0, moscow) } var date [5]int for i, b := range bytes[1:] { d, err := strconv.Atoi(string(b)) if err != nil { return time.Date(1970, time.January, 1, 0, 0, 0, 0, moscow) } date[i] = d } return time.Date(date[2], time.Month(date[1]), date[0], date[3], date[4], 0, 0, moscow) } func findEnclosure(ep []byte) *feeds.Enclosure { re := regexp.MustCompile(`data\-type="audio"\s+data\-id="(.+?)?">`) res, err := parseSingle(ep, re) if err != nil { return &feeds.Enclosure{} } url := "https://audio.vgtrk.com/download?id=" + string(res) return &feeds.Enclosure{ Url: url, Length: "1024", Type: "audio/mpeg", } } func findEpisodes(page []byte) [][]byte { episodeRe := regexp.MustCompile(`(?s)