-
Notifications
You must be signed in to change notification settings - Fork 13
/
feed.py
241 lines (199 loc) · 7.23 KB
/
feed.py
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# This file is part of Radio-Browser-Plugin for Rhythmbox.
# Copyright (C) 2012 <[email protected]>
# This is a derivative of software originally created by <[email protected]> 2009
#
# Radio-Browser-Plugin 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.
#
# Radio-Browser-Plugin 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 Radio-Browser-Plugin. If not, see <http://www.gnu.org/licenses/>.
import os
import urllib.request, urllib.error, urllib.parse
import http.client
from urllib.parse import urlparse
import datetime
import locale
import xml.sax.handler
from radio_station import RadioStation
from constants import _Const
CONST = _Const()
class FeedAction:
def __init__(self, feed, name, func):
self.feed = feed
self.name = name
self.func = func
def call(self, source):
self.func(source)
class FeedStationAction:
def __init__(self, feed, name, func):
self.feed = feed
self.name = name
self.func = func
def call(self, source, station):
self.func(source, station)
class Feed:
def __init__(self):
self.loaded = False
self.AutoDownload = True
self.UpdateChecking = True
self.FileSize = 0
self.remote_mod = datetime.datetime.now()
def getSource(self):
return self.uri
def getDescription(self):
return ""
def getHomepage(self):
return ""
def setAutoDownload(self, autodownload):
self.AutoDownload = autodownload
def setUpdateChecking(self, updatechecking):
self.UpdateChecking = updatechecking
def copy_callback(self, current, total):
self.status_change_handler(self.uri, current, total)
def download(self):
print("downloading " + self.uri)
try:
os.remove(self.filename)
except:
pass
try:
remotefile = urllib.request.urlopen(urllib.request.Request(self.uri, headers={'User-Agent': CONST.USER_AGENT}))
chunksize = 100
data = ""
current = 0
while True:
chunk = remotefile.read(chunksize)
chunk = chunk.decode('latin-1')
current += chunksize
self.copy_callback(current, self.FileSize)
if chunk == "":
break
if chunk == None:
break
data += chunk
localfile = open(self.filename, 'at', encoding='utf8')
localfile.write(data + '\n')
localfile.close()
except Exception as e:
print("download failed exception")
print(e)
return False
return True
def getRemoteFileInfo(self):
try:
urlparts = urlparse(self.uri)
conn = http.client.HTTPConnection(urlparts.netloc)
conn.request("HEAD", urlparts.path, headers={'User-Agent': CONST.USER_AGENT})
res = conn.getresponse()
for key, value in res.getheaders():
if key == "last-modified":
print(key + ":" + value)
oldlocale = locale.setlocale(locale.LC_ALL)
locale.setlocale(locale.LC_ALL, "C")
self.remote_mod = datetime.datetime.strptime(value, '%a, %d %b %Y %H:%M:%S %Z')
locale.setlocale(locale.LC_ALL, oldlocale)
if key == "content-length":
print(key + ":" + value)
self.FileSize = int(value)
except Exception as e:
print("could not check remote file for modification time:" + self.uri)
print(e)
return
# only download if necessary
def update(self):
download = False
local_mod = datetime.datetime.min
try:
local_mod = datetime.datetime.fromtimestamp(os.path.getmtime(self.filename))
local_mod += datetime.timedelta(days=7)
except:
print("could not load local file:" + self.filename)
download = True
self.getRemoteFileInfo()
if self.remote_mod > local_mod:
print("Local file older than 7 days: remote(" + str(self.remote_mod) + ") local(" + str(local_mod) + ")")
# change date is different -> download
download = True
else:
print("Local file newer than 7 days: remote(" + str(self.remote_mod) + ") local(" + str(local_mod) + ")")
if download:
self.download()
def load(self):
print("loading " + self.filename)
try:
xml.sax.parse(self.filename, self.handler)
except:
print("parse failed of " + self.filename)
def genres(self):
if not os.path.isfile(self.filename) and not self.AutoDownload:
return []
if not self.loaded:
if self.UpdateChecking:
self.update()
if not os.path.isfile(self.filename):
self.download() #was just download()
self.load()
self.loaded = True
list = []
for station in self.handler.entries:
if station.genre is not None:
for genre in station.genre.split(","):
tmp = genre.strip().lower()
if tmp not in list:
list.append(tmp)
return list
def entries(self):
if not os.path.isfile(self.filename) and not self.AutoDownload:
return []
if not self.loaded:
if self.UpdateChecking:
self.update()
if not os.path.isfile(self.filename):
self.download() #was just download()
self.load()
self.loaded = True
return self.handler.entries
def force_redownload(self):
self.handler.entries = []
self.loaded = False
try:
os.remove(self.filename)
except:
pass
pass
def get_feed_actions(self):
actions = []
return actions
def get_station_actions(self):
actions = []
return actions
#def search(self,term,queue):
# print "not implemented in this feed"
# return None
def downloadFile(self, url):
try:
remotefile = urllib.request.urlopen(url)
chunksize = 100
data = ""
current = 0
while True:
chunk = remotefile.read(chunksize).decode('utf-8')
current += chunksize
if chunk == "":
break
if chunk == None:
break
data += chunk
remotefile.close()
return data
except Exception as e:
print("download failed exception")
print(e)
return None