-
Notifications
You must be signed in to change notification settings - Fork 1
/
radio_update
executable file
·176 lines (140 loc) · 6.58 KB
/
radio_update
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python
import os
import os.path
import shutil
import urllib.request, urllib.error
import contextlib
from bs4 import BeautifulSoup
class Radio(object):
def __init__(self):
# might add the object to the list or something
pass
def update(self):
raise NotImplementedError('update')
def cleanup(self):
# unused now but will be used to handle both station removal and network errors:
# cwd('tmp')
# station.update() #exception is thrown on error
# cwd(orig_wd)
# station.cleanup()
# move_files_from_tmp_to_cwd
raise NotImplementedError('cleanup')
@staticmethod
def download_playlist(url, dest):
if not os.path.isdir(os.path.dirname(dest)):
os.makedirs(os.path.dirname(dest))
print('{0} -> {1}'.format(url, dest))
try:
with contextlib.closing(urllib.request.urlopen(url, timeout=5)) as i, open(dest, 'w') as o:
while True:
chunk = i.read(8192)
if not chunk:
break
o.write(chunk.decode())
except Exception as e:
print('Unable to download: {0} - {1}'.format(url, e))
class PlsRadio(Radio):
def __init__(self, url, name):
self.url = url
self.dest = os.path.join('by-name', name)
super(PlsRadio, self).__init__()
def cleanup(self):
os.unlink(self.dest)
def update(self):
self.download_playlist(self.url, self.dest)
class ScrapRadio(Radio):
def __init__(self, name, url, pls_ext='.pls'):
self.namedir = os.path.join('by-name', name)
self.url = url
self.html = ''
self.pls_ext = pls_ext
super(Radio, self).__init__()
def cleanup(self):
shutil.rmtree(self.namedir)
def update(self):
print('Updating {0} from {1}'.format(self.__class__.__name__, self.url))
try:
self.html = urllib.request.urlopen(self.url, timeout=5).read()
except Exception as e:
print('Unable to download: {0} - {1}'.format(self.url, e))
soup = BeautifulSoup(self.html, features="html.parser")
for (url, name) in self.scrape(soup):
dest = os.path.join(self.namedir, name + self.pls_ext)
self.download_playlist(url, dest)
def scrape(self, soup):
# needs to return iterable of tuples (url, stationname)
raise NotImplementedError('scrape')
class SomaFm(ScrapRadio):
def __init__(self):
super(SomaFm, self).__init__('somafm.com', 'http://somafm.com/')
def scrape(self, soup):
div_stations = soup.find('div', id='stations')
for anchor in div_stations.find_all('a'):
name = anchor['href'].strip('/')
src = 'http://somafm.com/{0}.pls'.format(name)
yield (src, name)
# approx curl -s http://live.slovakradio.sk:8000/ | grep -oP '/.+256\.mp3.m3u'
class RTVS(ScrapRadio):
def __init__(self):
super(RTVS, self).__init__('rtvs.sk', 'http://live.slovakradio.sk:8000/')
def scrape(self, soup):
for anchor in soup.find_all('a'):
name = anchor['href'].strip('/')
if "_256.mp3.m3u" in name:
sname = name.strip("_256.mp3.m3u").lower()
if sname == "fm": sname = "radio_fm"
yield (self.url + name, sname)
# approx curl -s http://live.slovakradio.sk:8000/ | grep -oP '/.+256\.mp3.m3u'
class R23(ScrapRadio):
def __init__(self):
super(R23, self).__init__('radio23.cz', 'http://radio23.cz')
def scrape(self, soup):
for anchor in soup.find_all('a'):
name = anchor['href']
if ".m3u" in name:
sname = name.split('/')[-1].strip(".m3u")
yield (name, sname)
stations = \
[
# somafm.com
SomaFm(),
# rtvs.sk
RTVS(),
# radio23.cz
R23(),
# rozhlas.cz
PlsRadio('http://api.play.cz/radio/cro1-128.mp3.m3u', 'rozhlas.cz/1.m3u'),
PlsRadio('http://api.play.cz/radio/cro2-128.mp3.m3u', 'rozhlas.cz/2.m3u'),
PlsRadio('http://api.play.cz/radio/cro3-128.mp3.m3u', 'rozhlas.cz/3.m3u'),
PlsRadio('http://api.play.cz/radio/cro7-128.mp3.m3u', 'rozhlas.cz/7.m3u'),
PlsRadio('http://api.play.cz/radio/crobrno128.mp3.m3u', 'rozhlas.cz/brno.m3u'),
PlsRadio('http://api.play.cz/radio/crocb128.mp3.m3u', 'rozhlas.cz/cb.m3u'),
PlsRadio('http://api.play.cz/radio/croddur-128.mp3.m3u', 'rozhlas.cz/ddur.m3u'),
PlsRadio('http://api.play.cz/radio/crohk128.mp3.m3u', 'rozhlas.cz/hk.m3u'),
PlsRadio('http://api.play.cz/radio/crojuniormaxi128.mp3.m3u', 'rozhlas.cz/juniormaxi.m3u'),
PlsRadio('http://api.play.cz/radio/crojuniormini128.mp3.m3u', 'rozhlas.cz/juniormini.m3u'),
PlsRadio('http://api.play.cz/radio/crokv128.mp3.m3u', 'rozhlas.cz/kv.m3u'),
PlsRadio('http://api.play.cz/radio/croliberec128.mp3.m3u', 'rozhlas.cz/liberec.m3u'),
PlsRadio('http://api.play.cz/radio/crool128.mp3.m3u', 'rozhlas.cz/ol.m3u'),
PlsRadio('http://api.play.cz/radio/croov128.mp3.m3u', 'rozhlas.cz/ov.m3u'),
PlsRadio('http://api.play.cz/radio/cropardubice128.mp3.m3u', 'rozhlas.cz/pardubice.m3u'),
PlsRadio('http://api.play.cz/radio/croplus128.mp3.m3u', 'rozhlas.cz/plus.m3u'),
PlsRadio('http://api.play.cz/radio/croplzen128.mp3.m3u', 'rozhlas.cz/plzen.m3u'),
PlsRadio('http://api.play.cz/radio/croregina128.mp3.m3u', 'rozhlas.cz/regina.m3u'),
PlsRadio('http://api.play.cz/radio/croregion128.mp3.m3u', 'rozhlas.cz/region.m3u'),
PlsRadio('http://api.play.cz/radio/crosever128.mp3.m3u', 'rozhlas.cz/sever.m3u'),
PlsRadio('http://api.play.cz/radio/crovysocina128.mp3.m3u', 'rozhlas.cz/vysocina.m3u'),
PlsRadio('http://api.play.cz/radio/crowave-128.mp3.m3u', 'rozhlas.cz/wave.m3u'),
PlsRadio('http://www.bassdrive.com/v2/streams/BassDrive.pls', 'bassdrive.pls'),
PlsRadio('http://www.dirtlabaudio.com/listen.m3u', 'dirt-lab-audio.m3u'),
PlsRadio('http://radior.video.muni.cz:8000/FSS_mp3-128.mp3.m3u', 'radio-R.m3u'),
PlsRadio('http://api.play.cz/radio/ethno128.asx', 'ethno.asx'),
PlsRadio('http://stream.laut.fm/bitfunker.m3u', 'bitfunker.m3u'),
PlsRadio('http://app1.enation.fm/pls', 'enationFM.pls'),
# radiotunguska.com
PlsRadio('http://stream1.radiostyle.ru/play.php?pltype=m3u&media=http://stream1.radiostyle.ru:8001/tunguska', 'tunguska.m3u'),
# nsbradio
PlsRadio('https://nsbradio.co.uk/listen.pls', 'nsbradio.pls')
]
for radio in stations:
radio.update()