Skip to content
This repository has been archived by the owner on Sep 25, 2021. It is now read-only.

Commit

Permalink
Use TheMovieDB v3 api
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruud committed Sep 2, 2013
1 parent 8817699 commit 910578a
Show file tree
Hide file tree
Showing 17 changed files with 2,829 additions and 853 deletions.
11 changes: 0 additions & 11 deletions couchpotato/core/plugins/scanner/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,17 +594,6 @@ def determineMovie(self, group, download_info = None):
except:
pass

# Search based on OpenSubtitleHash
if not imdb_id and not group['is_dvd']:
for cur_file in files['movie']:
movie = fireEvent('movie.by_hash', file = cur_file, merge = True)

if len(movie) > 0:
imdb_id = movie[0].get('imdb')
if imdb_id:
log.debug('Found movie via OpenSubtitleHash: %s', cur_file)
break

# Search based on identifiers
if not imdb_id:
for identifier in group['identifiers']:
Expand Down
140 changes: 39 additions & 101 deletions couchpotato/core/providers/info/themoviedb/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from couchpotato.core.event import addEvent
from couchpotato.core.helpers.encoding import simplifyString, toUnicode
from couchpotato.core.helpers.encoding import simplifyString, toUnicode, ss
from couchpotato.core.helpers.variable import md5
from couchpotato.core.logger import CPLog
from couchpotato.core.providers.info.base import MovieProvider
from themoviedb import tmdb
from couchpotato.environment import Env
import os
import tmdb3
import traceback

log = CPLog(__name__)
Expand All @@ -11,44 +14,13 @@
class TheMovieDb(MovieProvider):

def __init__(self):
addEvent('movie.by_hash', self.byHash)
addEvent('movie.search', self.search, priority = 2)
addEvent('movie.info', self.getInfo, priority = 2)
addEvent('movie.info_by_tmdb', self.getInfoByTMDBId)
addEvent('movie.info_by_tmdb', self.getInfo)

# Use base wrapper
tmdb.configure(self.conf('api_key'))

def byHash(self, file):
''' Find movie by hash '''

if self.isDisabled():
return False

cache_key = 'tmdb.cache.%s' % simplifyString(file)
results = self.getCache(cache_key)

if not results:
log.debug('Searching for movie by hash: %s', file)
try:
raw = tmdb.searchByHashingFile(file)

results = []
if raw:
try:
results = self.parseMovie(raw)
log.info('Found: %s', results['titles'][0] + ' (' + str(results.get('year', 0)) + ')')

self.setCache(cache_key, results)
return results
except SyntaxError, e:
log.error('Failed to parse XML response: %s', e)
return False
except:
log.debug('No movies known by hash for: %s', file)
pass

return results
# Configure TMDB settings
tmdb3.set_key(self.conf('api_key'))
tmdb3.set_cache(engine='file', filename=os.path.join(Env.get('cache_dir'), 'python', 'tmdb.cache'))

def search(self, q, limit = 12):
''' Find movie by name '''
Expand All @@ -65,7 +37,7 @@ def search(self, q, limit = 12):

raw = None
try:
raw = tmdb.search(search_string)
raw = tmdb3.searchMovie(search_string)
except:
log.error('Failed searching TMDB for "%s": %s', (search_string, traceback.format_exc()))

Expand All @@ -75,15 +47,15 @@ def search(self, q, limit = 12):
nr = 0

for movie in raw:
results.append(self.parseMovie(movie))
results.append(self.parseMovie(movie, with_titles = False))

nr += 1
if nr == limit:
break

log.info('Found: %s', [result['titles'][0] + ' (' + str(result.get('year', 0)) + ')' for result in results])

self.setCache(cache_key, results)
self.setCache(md5(ss(cache_key)), results)
return results
except SyntaxError, e:
log.error('Failed to parse XML response: %s', e)
Expand All @@ -105,109 +77,75 @@ def getInfo(self, identifier = None):

try:
log.debug('Getting info: %s', cache_key)
movie = tmdb.imdbLookup(id = identifier)
except:
pass

if movie:
result = self.parseMovie(movie[0])
movie = tmdb3.Movie(identifier)
result = self.parseMovie(movie)
self.setCache(cache_key, result)

return result

def getInfoByTMDBId(self, id = None):

cache_key = 'tmdb.cache.%s' % id
result = self.getCache(cache_key)

if not result:
result = {}
movie = None

try:
log.debug('Getting info: %s', cache_key)
movie = tmdb.getMovieInfo(id = id)
except:
pass

if movie:
result = self.parseMovie(movie)
self.setCache(cache_key, result)

return result

def parseMovie(self, movie):
def parseMovie(self, movie, with_titles = True):

# Images
poster = self.getImage(movie, type = 'poster', size = 'cover')
#backdrop = self.getImage(movie, type = 'backdrop', size = 'w1280')
poster = self.getImage(movie, type = 'poster', size = 'poster')
poster_original = self.getImage(movie, type = 'poster', size = 'original')
backdrop_original = self.getImage(movie, type = 'backdrop', size = 'original')

# Genres
try:
genres = self.getCategory(movie, 'genre')
genres = [genre.name for genre in movie.genres]
except:
genres = []

# 1900 is the same as None
year = str(movie.get('released', 'none'))[:4]
if year == '1900' or year.lower() == 'none':
year = str(movie.releasedate or '')[:4]
if not movie.releasedate or year == '1900' or year.lower() == 'none':
year = None

movie_data = {
'via_tmdb': True,
'tmdb_id': int(movie.get('id', 0)),
'titles': [toUnicode(movie.get('name'))],
'original_title': movie.get('original_name'),
'tmdb_id': movie.id,
'titles': [toUnicode(movie.title)],
'original_title': movie.originaltitle,
'images': {
'poster': [poster] if poster else [],
#'backdrop': [backdrop] if backdrop else [],
'poster_original': [poster_original] if poster_original else [],
'backdrop_original': [backdrop_original] if backdrop_original else [],
},
'imdb': movie.get('imdb_id'),
'mpaa': movie.get('certification', ''),
'runtime': movie.get('runtime'),
'released': movie.get('released'),
'imdb': movie.imdb,
'runtime': movie.runtime,
'released': movie.releasedate,
'year': year,
'plot': movie.get('overview'),
'plot': movie.overview,
'genres': genres,
}

movie_data = dict((k, v) for k, v in movie_data.iteritems() if v)

# Add alternative names
for alt in ['original_name', 'alternative_name']:
alt_name = toUnicode(movie.get(alt))
if alt_name and not alt_name in movie_data['titles'] and alt_name.lower() != 'none' and alt_name != None:
movie_data['titles'].append(alt_name)
if with_titles:
movie_data['titles'].append(movie.originaltitle)
for alt in movie.alternate_titles:
alt_name = alt.title
if alt_name and not alt_name in movie_data['titles'] and alt_name.lower() != 'none' and alt_name != None:
movie_data['titles'].append(alt_name)

movie_data['titles'] = list(set(movie_data['titles']))

return movie_data

def getImage(self, movie, type = 'poster', size = 'cover'):
def getImage(self, movie, type = 'poster', size = 'poster'):

image_url = ''
for image in movie.get('images', []):
if(image.get('type') == type) and image.get(size):
image_url = image.get(size)
break
try:
image_url = getattr(movie, type).geturl(size='original')
except:
log.debug('Failed getting %s.%s for "%s"', (type, size, movie.title))

return image_url

def getCategory(self, movie, type = 'genre'):

cats = movie.get('categories', {}).get(type)

categories = []
for category in cats:
try:
categories.append(category)
except:
pass

return categories

def isDisabled(self):
if self.conf('api_key') == '':
log.error('No API key provided.')
Expand Down
2 changes: 1 addition & 1 deletion couchpotato/core/providers/userscript/tmdb/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TMDB(UserscriptBase):

def getMovie(self, url):
match = re.search('(?P<id>\d+)', url)
movie = fireEvent('movie.info_by_tmdb', id = match.group('id'), merge = True)
movie = fireEvent('movie.info_by_tmdb', identifier = match.group('id'), merge = True)

if movie['imdb']:
return self.getInfo(movie['imdb'])
Expand Down
Empty file removed libs/themoviedb/__init__.py
Empty file.
Loading

5 comments on commit 910578a

@OverlordQ
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throws errors on windows:

09-02 22:21:35 ERROR ?[31m[providers.info.themoviedb] Failed searching TMDB for "ocean s twelve 2004": Traceback (most recent call last):
  File "e:\tools\CouchPotatoServer\couchpotato\core\providers\info\themoviedb\main.py", line 40, in search
    raw = tmdb3.searchMovie(search_string)
  File "e:\tools\CouchPotatoServer\libs\tmdb3\tmdb_api.py", line 118, in searchMovie
    return MovieSearchResult(Request('search/movie', **kwargs), locale=locale)
  File "e:\tools\CouchPotatoServer\libs\tmdb3\tmdb_api.py", line 145, in __init__
    lambda x: Movie(raw=x, locale=locale))
  File "e:\tools\CouchPotatoServer\libs\tmdb3\pager.py", line 101, in __init__
    super(PagedRequest, self).__init__(self._getpage(1), 20)
  File "e:\tools\CouchPotatoServer\libs\tmdb3\pager.py", line 56, in __init__
    self._data = list(iterable)
  File "e:\tools\CouchPotatoServer\libs\tmdb3\pager.py", line 105, in _getpage
    res = req.readJSON()
  File "e:\tools\CouchPotatoServer\libs\tmdb3\cache.py", line 106, in __call__
    data = self.cache.get(key)
  File "e:\tools\CouchPotatoServer\libs\tmdb3\cache.py", line 64, in get
    self._import()
  File "e:\tools\CouchPotatoServer\libs\tmdb3\cache.py", line 33, in _import
    data = self._engine.get(self._age)
  File "e:\tools\CouchPotatoServer\libs\tmdb3\cache_file.py", line 262, in get
    return self._read(date)
  File "e:\tools\CouchPotatoServer\libs\tmdb3\cache_file.py", line 111, in __exit__
    msvcrt.locking(self.fileobj.fileno(), msvcrt.LK_UNLCK, self.size)
IOError: [Errno 13] Permission denied

@jkaberg
Copy link
Contributor

@jkaberg jkaberg commented on 910578a Sep 3, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IOError: [Errno 13] Permission denied

Make sure you got write permissions......

@RuudBurger
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It writes to the same folder as CP does for caching. I need to see why this wouldn't work.

@OverlordQ
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RuudBurger
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just pushed an update, so we use our own caching system. Please check it out.

Please sign in to comment.