morss/morss/feedify.py

29 lines
712 B
Python
Raw Normal View History

import re
import json
2015-02-25 03:43:04 +00:00
from . import crawler
2013-09-25 10:36:21 +00:00
2015-02-25 16:50:23 +00:00
try:
basestring
except NameError:
basestring = str
2013-11-09 17:48:06 +00:00
def pre_worker(url):
2017-03-19 09:41:37 +00:00
if url.startswith('http://itunes.apple.com/') or url.startswith('https://itunes.apple.com/'):
match = re.search('/id([0-9]+)(\?.*)?$', url)
if match:
iid = match.groups()[0]
2017-03-19 09:41:37 +00:00
redirect = 'https://itunes.apple.com/lookup?id=%s' % iid
try:
con = crawler.custom_handler(basic=True).open(redirect, timeout=4)
data = con.read()
except (IOError, HTTPException):
raise
return json.loads(data.decode('utf-8', 'replace'))['results'][0]['feedUrl']
return None