1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
// Copyright (C) 2019 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 tihe this program. If not, see <https://www.gnu.org/licenses/>.
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>`)
programAboutRe = regexp.MustCompile(`(?s)<div class="brand__content_text__anons">(.+?)?</div>`)
programImageRe = regexp.MustCompile(`(?s)<div class="brand\-promo__header">(.+?)?<img src="(.+?)?"(.+?)?alt='(.+?)?'>`)
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>`)
episodeDescRe = regexp.MustCompile(`<p class="anons">(.+?)?</p>`)
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
err error
)
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"
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)
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(``)))
badFeed := false
for _, episode := range episodes {
if len(episodeUrlRe.FindAllSubmatch(episode, -1)) > 1 {
badFeed = true
break
}
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,
})
}
if badFeed {
log.Println("Page looks strange. Episode in progress? Will wait for 15 minutes and try again...")
time.Sleep(15 * 60 * time.Second)
continue
}
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
}
}
func getPage(pageUrl string) []byte {
res, err := http.Get(pageUrl)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
page, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
for _, sub := range substitutes {
re := regexp.MustCompile(sub.from)
page = re.ReplaceAll(page, []byte(sub.to))
}
return page
}
|