6
from functools import partial
7
from flexget.utils.imdb import make_url
8
from flexget.utils.cached_input import cached
9
from flexget.utils.tools import urlopener as _urlopener, decode_html
10
from flexget.plugin import register_plugin, PluginError
11
from flexget.entry import Entry
13
log = logging.getLogger('imdb_list')
15
USER_ID_RE = r'^ur\d{7,8}$'
18
class ImdbList(object):
19
""""Creates an entry for each movie in your imdb list."""
22
from flexget import validator
23
root = validator.factory('dict')
24
root.accept('regexp_match', key='user_id').\
25
accept(USER_ID_RE, message='user_id must be in the form urXXXXXXX')
26
root.accept('text', key='username')
27
root.accept('text', key='password')
28
root.accept('text', key='list', required=True)
31
@cached('imdb_list', persist='2 hours')
32
def on_feed_input(self, feed, config):
33
urlopener = partial(_urlopener, log=log, retries=2)
34
if config.get('username') and config.get('password'):
35
# Create a cookie handler, make sure it is used in our calls to urlopener
36
cookiehandler = urllib2.HTTPCookieProcessor()
37
urlopener = partial(urlopener, handlers=[cookiehandler])
39
log.verbose('Logging in ...')
41
# Log in to imdb with our handler
42
params = urllib.urlencode({'login': config['username'], 'password': config['password']})
44
urlopener('https://secure.imdb.com/register-imdb/login', data=params)
45
except urllib2.URLError, e:
46
raise PluginError('Unable to login to imdb: %s' % e.message)
48
# try to automatically figure out user_id from watchlist redirect url
49
if not 'user_id' in config:
50
log.verbose('Getting user_id ...')
51
opener = urlopener('http://www.imdb.com/list/watchlist')
52
redirected = opener.geturl()
53
log.debug('redirected to %s' % redirected)
54
user_id = redirected.split('/')[-2]
55
if re.match(USER_ID_RE, user_id):
56
config['user_id'] = user_id
58
raise PluginError('Couldn\'t figure out user_id, please configure it manually.')
60
if not 'user_id' in config:
61
raise PluginError('Configuration option `user_id` required.')
63
log.verbose('Retrieving list %s ...' % config['list'])
65
# Get the imdb list in csv format
67
url = 'http://www.imdb.com/list/export?list_id=%s&author_id=%s' % (config['list'], config['user_id'])
68
log.debug('Requesting %s' % url)
69
opener = urlopener(url)
70
mime_type = opener.headers.gettype()
71
log.debug('mime_type: %s' % mime_type)
72
if mime_type != 'text/csv':
73
raise PluginError('Didn\'t get CSV export as response. Probably specified list `%s` does not exists.'
75
csv_rows = csv.reader(opener)
76
except urllib2.URLError, e:
77
raise PluginError('Unable to get imdb list: %s' % e.message)
79
# Create an Entry for each movie in the list
82
if not row or row[0] == 'position':
83
# Don't use blank rows or the headings row
86
title = decode_html(row[5]).decode('utf-8')
87
entries.append(Entry(title=title, url=make_url(row[1]), imdb_id=row[1], imdb_name=title))
89
log.critical('IndexError! Unable to handle row: %s' % row)
93
register_plugin(ImdbList, 'imdb_list', api_ver=2)