import hashlib
import logging
import urllib2
from flexget.utils.tools import urlopener
from flexget.utils.cached_input import cached
from flexget.plugin import register_plugin, PluginError, DependencyError
from flexget.entry import Entry

try:
    import simplejson as json
except ImportError:
    try:
        import json
    except ImportError:
        raise DependencyError(issued_by='trakt_list', missing='simplejson',
                              message='trakt_list requires either simplejson module or python > 2.5')

log = logging.getLogger('trakt_list')


class TraktList(object):
    """"Creates an entry for each item in your trakt list.

    Syntax:

    trakt_list:
      username: <value>
      api_key: <value>
      movies: <all|loved|hated|collection|watchlist>
      series: <all|loved|hated|collection|watchlist|watched>
      custom: <value>

    Options username and api_key are required.
    """

    movie_map = {
        'title': 'title',
        'url': 'url',
        'imdb_id': 'imdb_id',
        'tmdb_id': 'tmdb_id',
        # Generic fields filled by all movie lookup plugins:
        'movie_name': 'title',
        'movie_year': 'year'}

    series_map = {
        'title': 'title',
        'url': 'url',
        'imdb_id': 'imdb_id',
        'tvdb_id': 'tvdb_id',
        'tvrage_id': 'tvrage_id'}

    def validator(self):
        from flexget import validator
        root = validator.factory('dict')
        root.accept('text', key='username', requried=True)
        root.accept('text', key='api_key', required=True)
        root.accept('text', key='password')
        root.accept('choice', key='movies').accept_choices(['all', 'loved', 'hated', 'collection', 'watchlist'])
        root.accept('choice', key='series').accept_choices(['all', 'loved', 'hated', 'collection', 'watched', 'watchlist'])
        root.accept('text', key='custom')
        return root

    @cached('trakt_list', persist='2 hours')
    def on_feed_input(self, feed, config):
        if 'movies' in config and 'series' in config:
            raise PluginError('Cannot use both series list and movies list in the same feed.')
        if 'movies' in config:
            config['data_type'] = 'movies'
            config['list_type'] = config['movies']
            map = self.movie_map
        elif 'series' in config:
            config['data_type'] = 'shows'
            config['list_type'] = config['series']
            map = self.series_map
        elif 'custom' in config:
            config['data_type'] = 'custom'
            config['list_type'] = config['custom'].replace(' ', '-')
            # Map type is per item in custom lists
        else:
            raise PluginError('Must define movie or series lists to retrieve from trakt.')

        url = 'http://api.trakt.tv/user/'
        if config['data_type'] == 'custom':
            url += 'list.json/%(api_key)s/%(username)s/%(list_type)s'
        elif config['list_type'] == 'watchlist':
            url += 'watchlist/%(data_type)s.json/%(api_key)s/%(username)s'
        else:
            url += 'library/%(data_type)s/%(list_type)s.json/%(api_key)s/%(username)s'
        url = url % config

        if 'password' in config:
            auth = {'username': config['username'], 'password': hashlib.sha1(config['password']).hexdigest()}
            url = urllib2.Request(url, json.dumps(auth), {'content-type': 'application/json'})

        entries = []
        log.verbose('Retrieving list %s %s...' % (config['data_type'], config['list_type']))
        try:
            data = json.load(urlopener(url, log, retries=2))
        except urllib2.URLError, e:
            raise PluginError('Could not retrieve url %s' % url)
        if 'error' in data:
            raise PluginError('Error getting trakt list: %s' % data['error'])
        if config['data_type'] == 'custom':
            data = data['items']
        for item in data:
            if config['data_type'] == 'custom':
                if item['type'] == 'movie':
                    map = self.movie_map
                    item = item['movie']
                else:
                    map = self.series_map
                    item = item['show']
            entry = Entry()
            entry.update_using_map(map, item)
            if entry.isvalid():
                entries.append(entry)

        return entries


register_plugin(TraktList, 'trakt_list', api_ver=2)

@@ -41,16 +41,27 @@
 rllib2%0D%0A
+import re%0D%0A
 from fle
@@ -755,24 +755,53 @@
 y: %3Cvalue%3E%0D%0A
+      strip_dates: %3Cyes%7Cno%3E%0D%0A
       movies
@@ -1964,24 +1964,75 @@
 ='custom')%0D%0A
+        root.accept('boolean', key='strip_dates')%0D%0A
         retu
@@ -4478,24 +4478,216 @@
 isvalid():%0D%0A
+                if config.get('strip_dates'):%0D%0A                    # Remove year from end of name if present%0D%0A                    entry%5B'title'%5D = re.sub('%5Cs+%5C(%5Cd%7B4%7D%5C)$', '', entry%5B'title'%5D)%0D%0A
             

Add strip_dates option to trakt_list plugin which removes the year from the end of created entry titles.