From a6430c96b3cbadf1180ce897107f0bfdc42c5801 Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Mon, 4 Mar 2013 15:52:57 +0100 Subject: [PATCH] [cineoob] new command movies_in_common --- modules/imdb/backend.py | 3 ++ modules/imdb/browser.py | 11 +++++++- modules/imdb/pages.py | 8 +++++- weboob/applications/cineoob/cineoob.py | 38 +++++++++++++++++++++++++- weboob/capabilities/cinema.py | 10 +++++++ 5 files changed, 67 insertions(+), 3 deletions(-) diff --git a/modules/imdb/backend.py b/modules/imdb/backend.py index f358727c..b73056ef 100644 --- a/modules/imdb/backend.py +++ b/modules/imdb/backend.py @@ -56,3 +56,6 @@ class ImdbBackend(BaseBackend, ICapCinema): def iter_person_movies(self, id): return self.browser.iter_person_movies(id) + + def iter_person_movies_ids(self, id): + return self.browser.iter_person_movies_ids(id) diff --git a/modules/imdb/browser.py b/modules/imdb/browser.py index 62e2f9a4..e20c54e7 100644 --- a/modules/imdb/browser.py +++ b/modules/imdb/browser.py @@ -72,7 +72,11 @@ class ImdbBrowser(BaseBrowser): title = jres['title'] if jres.has_key('runtime'): - duration = int(jres['runtime'][0].split(':')[-1].split()[0]) + dur_str = jres['runtime'][0].split(':') + if len(dur_str) == 1: + duration = int(dur_str[0].split()[0]) + else: + duration = int(dur_str[1].split()[0]) if jres.has_key('also_known_as'): for other_t in jres['also_known_as']: if other_t.has_key('country') and other_t.has_key('title'): @@ -126,3 +130,8 @@ class ImdbBrowser(BaseBrowser): self.location('http://www.imdb.com/name/%s' % person_id) assert self.is_on_page(PersonPage) return self.page.iter_movies(person_id) + + def iter_person_movies_ids(self, person_id): + self.location('http://www.imdb.com/name/%s' % person_id) + assert self.is_on_page(PersonPage) + return self.page.iter_movies_ids(person_id) diff --git a/modules/imdb/pages.py b/modules/imdb/pages.py index ab03563b..f4ef0837 100644 --- a/modules/imdb/pages.py +++ b/modules/imdb/pages.py @@ -70,7 +70,7 @@ class PersonPage(BasePage): biography = descs[0].text names = self.parser.select(td_overview,'h1[itemprop=name]') if len(names) > 0: - name = names[0].text + name = names[0].text.strip() times = self.parser.select(td_overview,'time[itemprop=birthDate]') if len(times) > 0: time = times[0].attrib.get('datetime','').split('-') @@ -96,3 +96,9 @@ class PersonPage(BasePage): a = self.parser.select(movie_div,'b a',1) id = a.attrib.get('href','').strip('/').split('/')[-1] yield self.browser.get_movie(id) + + def iter_movies_ids(self,person_id): + for movie_div in self.parser.select(self.document.getroot(),'div[class~=filmo-row]'): + a = self.parser.select(movie_div,'b a',1) + id = a.attrib.get('href','').strip('/').split('/')[-1] + yield id diff --git a/weboob/applications/cineoob/cineoob.py b/weboob/applications/cineoob/cineoob.py index 12ddd26d..d2ddf397 100644 --- a/weboob/applications/cineoob/cineoob.py +++ b/weboob/applications/cineoob/cineoob.py @@ -157,7 +157,8 @@ class Cineoob(ReplApplication): 'search_person': 'person_list', 'info_person': 'person_info', 'casting': 'person_list', - 'filmography': 'movie_list' + 'filmography': 'movie_list', + 'movies_in_common':'movie_list' } def complete_info(self, text, line, *ignored): @@ -165,6 +166,41 @@ class Cineoob(ReplApplication): if len(args) == 2: return self._complete_object() + def do_movies_in_common(self, line): + """ + movies_in_common person_ID person_ID + + Get the list of common movies between two persons. + """ + id1, id2 = self.parse_command_args(line, 2, 1) + + person1 = self.get_object(id1, 'get_person') + if not person1: + print >>sys.stderr, 'Person not found: %s' % id1 + return 3 + person2 = self.get_object(id2, 'get_person') + if not person2: + print >>sys.stderr, 'Person not found: %s' % id2 + return 3 + + initial_count = self.options.count + self.options.count = None + + lid1 = [] + for backend, id in self.do('iter_person_movies_ids', person1.id): + lid1.append(id) + self.flush() + lid2 = [] + for backend, id in self.do('iter_person_movies_ids', person2.id): + lid2.append(id) + self.flush() + self.options.count = initial_count + inter = list(set(lid1) & set(lid2)) + for common in inter: + movie = self.get_object(common, 'get_movie') + self.cached_format(movie) + self.flush() + def do_info_movie(self, id): """ info_movie movie_ID diff --git a/weboob/capabilities/cinema.py b/weboob/capabilities/cinema.py index b9b17904..65d57fc4 100644 --- a/weboob/capabilities/cinema.py +++ b/weboob/capabilities/cinema.py @@ -133,3 +133,13 @@ class ICapCinema(IBaseCap): :rtype: iter[:class:`Movie`] """ raise NotImplementedError() + + def iter_person_movies_ids(self, _id): + """ + Get the list of movie ids related to a person. + + :param _id: ID of person + :type _id: str + :rtype: iter[str] + """ + raise NotImplementedError()