flexget.plugins.input.imdb_list
Covered: 29 lines
Missed: 43 lines
Skipped 22 lines
Percent: 40 %
 1
import logging
 2
import csv
 3
import re
 4
import urllib
 5
import urllib2
 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."""
21
    def validator(self):
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)
29
        return root
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'):
36
            cookiehandler = urllib2.HTTPCookieProcessor()
37
            urlopener = partial(urlopener, handlers=[cookiehandler])
39
            log.verbose('Logging in ...')
42
            params = urllib.urlencode({'login': config['username'], 'password': config['password']})
43
            try:
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)
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
57
                else:
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'])
66
        try:
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.'
74
                    % config['list'])
75
            csv_rows = csv.reader(opener)
76
        except urllib2.URLError, e:
77
            raise PluginError('Unable to get imdb list: %s' % e.message)
80
        entries = []
81
        for row in csv_rows:
82
            if not row or row[0] == 'position':
84
                continue
85
            try:
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))
88
            except IndexError:
89
                log.critical('IndexError! Unable to handle row: %s' % row)
90
        return entries
93
register_plugin(ImdbList, 'imdb_list', api_ver=2)