-
Notifications
You must be signed in to change notification settings - Fork 0
/
grabber.py
303 lines (250 loc) · 10.6 KB
/
grabber.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
'''
iTunes Feed Grabber: transforms iTunes podcasts to RSS XML feeds.
Copyright (C) 2015 Yan Foto
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 <http://www.gnu.org/licenses/>.
'''
from urlparse import urlparse
from urllib import urlencode
from urllib2 import Request, urlopen, HTTPError
from lxml import html, etree
from time import mktime
from datetime import datetime
from email.Utils import formatdate
from grabexceptions import *
import re
__author__ = "Yan Foto"
__version__ = "0.3.0"
class Grabber:
"""Given an iTunes URL, grabber provides various methods to fetch contents
of that URL as if it was opened in iTunes.
"""
# Request headers to act as-if we were iTunes
HEADERS = {
'X-Apple-Tz': 3600,
'User-Agent': 'iTunes/9.2.1 (Macintosh; Intel Mac OS X 10.5.8) AppleWebKit/533.16'
}
# Valid addresses
WHITE_ADDRS = [
'itunes.apple.com'
]
# Podcast hosts for web objects
# We used to use 'https://itunes.apple.com/WebObjects/DZR.woa/wa/viewPodcast'
# URL, but it didn't support some of older podcasts ('Not available in market'
# error), so we use this one and instead look for possible redirects
POD_HOST = 'https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewPodcast'
# Host for courses as they are not enlisted as podcasts and trying to fetch
# them as podcasts returns the usual and misleading 'Not available ...' error
COURSE_HOST = 'https://itunes.apple.com/course'
# iTunes' XML DTD
ITUNES_XML_DTD = 'http://www.itunes.com/dtds/podcast-1.0.dtd'
def __init__(self, url_or_id, course = False):
"""Initializes.
@param url_or_id: URL or ID of iTunes podcast
"""
if isinstance(url_or_id, (int, long)):
self.id = url_or_id
elif isinstance(url_or_id, (str, unicode)):
id = re.search('id=?(\d+)', url_or_id)
if id is None:
raise InvalidTarget("Couldn't find ID of requested item")
self.id = id.group(1) if id else query['id']
else:
raise InvalidTarget(
"Target must be an ID (number) or string (URL)! (given: {})".format(
type(url_or_id)))
self.course = course
if course:
self.url = "{}/id{}".format(Grabber.COURSE_HOST, self.id)
else:
self.url = "{}?{}".format(Grabber.POD_HOST, urlencode({'id': self.id}))
def raw_grab(self):
"""Returns the content of given URL as if it was opened in iTunes.
@return response object if grabbing succeeds
"""
# Check if it is an itunes URL
urlInfo = urlparse(self.url)
if urlInfo.netloc not in Grabber.WHITE_ADDRS:
# TODO: raise error
return False
# Follow the redirects to get to actual page with links
finished = False
while not finished:
try:
request = Request(self.url, None, Grabber.HEADERS)
response = urlopen(request)
content = response.read()
response.close()
redirect_url = self._redirect_url(content)
if redirect_url is None:
finished = True
return content
else:
self.url = redirect_url
except HTTPError as e:
# TODO: raise error
pass
def grab_audio_items(self):
"""@return an array of all audio items w/ artist, album, title, and url info"""
return self._audio_items_from(self._as_html())
def grab_meta_info(self):
"""@return dictionary of meta information"""
return self._meta_info_from(self._as_html())
def grab_rss_feed(self):
"""Creates an RSS 2.0 XML feed from the object's URL.
@return an RSS string"""
content = self._as_html()
ns = Grabber.ITUNES_XML_DTD
root = etree.Element(
'rss',
version='2.0',
nsmap={'itunes': ns})
channel = etree.SubElement(root, "channel")
# Podcast info
meta = self._meta_info_from(content)
for key in ['title', 'description']:
item = etree.SubElement(channel, key)
item.text = meta[key]
image = etree.SubElement(channel, 'image')
image_url = etree.SubElement(image, 'url')
image_url.text = meta['image']
image_title = etree.SubElement(image, 'title')
image_title.text = meta['title']
for linkable in [image, channel]:
linkable_link = etree.SubElement(linkable, 'link')
linkable_link.text = self.url
for date in ['pubDate', 'lastBuildDate']:
date_field = etree.SubElement(channel, date)
date_field.text = formatdate(mktime(datetime.now().timetuple()))
# iTunes specific items
etree.SubElement(channel, '{%s}image' % ns, href=meta['image'])
itunes_author = etree.SubElement(channel, '{%s}author' % ns)
itunes_author.text = meta['author']
# Items
for item in self._audio_items_from(content):
item_el = etree.SubElement(channel, 'item')
# Title
sub_item = etree.SubElement(item_el, 'title')
sub_item.text = item['name']
# Description
sub_item = etree.SubElement(item_el, 'description')
sub_item.text = etree.CDATA(item['description'])
# 'link' and 'guid' both as URL
for key in ['link', 'guid']:
sub_item = etree.SubElement(item_el, key)
sub_item.text = item['url']
# Publication date
# TODO: this looks really messy!
# If no release-date is given (e.g. courses) use now!
if 'release-date' in item:
dt = datetime.strptime(item['release-date'], '%Y/%m/%d')
else:
dt = datetime.now()
sub_item = etree.SubElement(item_el, 'pubDate')
sub_item.text = formatdate(mktime(dt.timetuple()))
# iTunes specific tags
itunes_author = etree.SubElement(item_el, '{%s}author' % ns)
itunes_author.text = meta['author']
# If duration is provided
try:
# Duration is provided in miliseconds
duration = int(item['time']) / 1000
m, s = divmod(duration, 60)
h, m = divmod(m, 60)
itunes_duration = etree.SubElement(
item_el, '{%s}duration' % ns)
if h > 0:
itunes_duration.text = '{:02d}:{:02d}:{:02d}'.format(
h, m, s)
else:
itunes_duration.text = '{:02d}:{:02d}'.format(m, s)
except ValueError:
# No duration provided
pass
# Enclosure: url must have http scheme and not https!
# TODO: Figure out length :/
url = re.sub(r'^https://', 'http://', item['url'])
enc = etree.SubElement(
item_el,
'enclosure',
url=url,
type='audio/mpeg',
length=item['size'])
return etree.tostring(
root,
xml_declaration=True,
encoding='utf-8',
pretty_print=True)
def _redirect_url(self, response):
"""Checks if server response denotes a redirection.
@param response: response from server in string
@return redirect URL or None if response is already valid
"""
try:
root = etree.fromstring(response)
candidates = root.xpath(
'.//key[text()="url"]/following-sibling::string')
if len(candidates) > 0:
return candidates[0].text
except etree.XMLSyntaxError:
# Response is HTML and not XML: we're good to parse further
pass
def _as_html(self):
"""@return lxml.html object of object's URL"""
return html.fromstring(self.raw_grab())
def _audio_items_from(self, content):
"""@return an array of audio items from given podcast page (HTML content)"""
result = []
rows = content.xpath(
"//table[contains(@class, 'tracklist-table')]/tbody//tr")
if self.course:
columns = 'index,name,description,time,price'
else:
columns = 'index,name,time,release-date,description,popularity,price'
columns = columns.split(',')
for e in rows:
track = {}
for i in range(0, len(columns)):
col = columns[i]
track[col] = e.find('.//td[%d]' % (i + 1)).get('sort-value')
audio_url = e.get('audio-preview-url')
if audio_url is None:
# TODO: append also video links!
continue
track['url'] = audio_url
# Fetch size in octets using a HEAD request
request = Request(audio_url, None, Grabber.HEADERS)
request.get_method = lambda: 'HEAD'
response = urlopen(request)
track['size'] = response.headers['content-length']
result.append(track)
return result
def _meta_info_from(self, content):
"""Parses podcasts meta information out of given HTML content.
@return dictionary containing title, author, description, and image"""
result = {}
# Title and Author
result["title"] = content.find(
".//button[@podcast-name]").get('podcast-name')
result["author"] = content.find(
".//button[@artist-name]").get('artist-name')
# Description
# TODO: this is not working for courses
product_info = content.find(".//div[@class='product-info']")
description = product_info.xpath(
".//div[contains(@class, 'product-review')]/p")
result["description"] = description[0].text if description else ''
# Image
image = content.find(".//div[@class='artwork']/img")
result["image"] = image.get('src-swap')
# URL (original URL provided to Grabber)
result["url"] = self.url
return result