1# -*- coding: UTF-8 -*-
2
3__revision__ = '$Id$'
4
5# Copyright (c) 2011 Ivo Nunes
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU Library General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
20
21# You may use and distribute this software under the terms of the
22# GNU General Public License, version 2 or later
23
24import gutils
25import movie
26import string
27import re
28import urllib.request, urllib.parse, urllib.error
29
30plugin_name         = "IMDb-pt"
31plugin_description  = "Internet Movie Database Portuguese"
32plugin_url          = "www.imdb.pt"
33plugin_language     = _("Portuguese")
34plugin_author       = "Ivo Nunes"
35plugin_author_email = "<netherblood@gmail.com>"
36plugin_version      = "0.1"
37
38class Plugin(movie.Movie):
39    def __init__(self, id):
40        self.encode   ='iso-8859-1'
41        self.movie_id = id
42        self.url      = "http://www.imdb.pt/title/tt" + str(self.movie_id)
43
44    def initialize(self):
45        self.page = gutils.convert_entities(self.page)
46        self.cast_page = self.open_page(url=self.url + '/fullcredits')
47        self.cast_page = gutils.convert_entities(self.cast_page)
48        self.plot_page = self.open_page(url=self.url + '/plotsummary')
49        self.plot_page = gutils.convert_entities(self.plot_page)
50
51    def get_image(self):
52        self.image_url = gutils.trim(self.page, 'src="http://ia.media-imdb.com/images/', '.jpg" /></a>')
53        self.image_url = "http://ia.media-imdb.com/images/" + self.image_url + ".jpg"
54
55    def get_title(self):
56        self.title = gutils.trim(self.page, '<title>', ' (')
57        self.title = self.title.encode(self.encode)
58
59    def get_o_title(self):
60        self.o_title = gutils.trim(self.page, 'Conhecido Como:</h5><div class="info-content">"', '"')
61        self.o_title = self.o_title.encode(self.encode)
62
63    def get_director(self):
64        self.director = gutils.trim(self.page, '<h5>Diretor:</h5>', '</a><br/>')
65        self.director = gutils.strip_tags(self.director)
66
67    def get_plot(self):
68        self.plot = gutils.trim(self.plot_page, '<div id="swiki.2.1">', '</div>')
69        self.plot = gutils.strip_tags(self.plot)
70        self.plot = self.plot.encode(self.encode)
71
72    def get_year(self):
73        self.year = gutils.trim(self.page, ' (', ')</title>')
74
75    def get_runtime(self):
76        self.runtime = gutils.trim(self.page, '<h5>Duração:</h5><div class="info-content">', ' min')
77        self.runtime = self.runtime.encode(self.encode)
78
79    def get_genre(self):
80        self.genre = gutils.trim(self.page, '<h5>Gênero:</h5>', '</div>')
81        self.genre = gutils.strip_tags(self.genre)
82        self.genre = string.replace(self.genre, " | ", ", ")
83        self.genre = self.genre.encode(self.encode)
84
85    def get_cast(self):
86        self.cast = ''
87        self.cast = gutils.trim(self.cast_page, '<table class="cast">', '</table>')
88        if self.cast == '':
89            self.cast = gutils.trim(self.page, '<table class="cast">', '</table>')
90        self.cast = string.replace(self.cast, ' ... ', _(' como ').encode('utf8'))
91        self.cast = string.replace(self.cast, '...', _(' como ').encode('utf8'))
92        self.cast = string.replace(self.cast, '</tr><tr>', "\n")
93        self.cast = re.sub('</tr>[ \t]*<tr[ \t]*class="even">', "\n", self.cast)
94        self.cast = re.sub('</tr>[ \t]*<tr[ \t]*class="odd">', "\n", self.cast)
95        self.cast = self.__before_more(self.cast)
96        self.cast = re.sub('[ ]+', ' ', self.cast)
97
98    def get_classification(self):
99        self.classification = gutils.trim(self.page, '<h5>Certificação:</h5><div class="info-content">', '</div>')
100        self.classification = gutils.strip_tags(self.classification)
101        self.classification = string.replace(self.classification, " | ", ", ")
102        self.classification = self.classification.encode(self.encode)
103
104    def get_studio(self):
105        self.studio = gutils.trim(self.page, '<h5>Companhia :</h5><div class="info-content">', 'Exibir mais</a>')
106        self.studio = gutils.strip_tags(self.studio)
107        self.studio = self.studio.encode(self.encode)
108
109    def get_o_site(self):
110        self.o_site = ""
111
112    def get_site(self):
113        self.site = self.url
114
115    def get_trailer(self):
116        self.trailer = "http://www.imdb.com/title/" + str(self.movie_id) + "/trailers"
117
118    def get_country(self):
119        self.country = gutils.trim(self.page, '<h5>País:</h5><div class="info-content">', '</div>')
120        self.country = string.replace(self.country, " | ", ", ")
121        self.country = self.country.encode(self.encode)
122
123    def get_notes(self):
124        self.notes = ''
125
126    def get_rating(self):
127        self.rating = gutils.trim(self.page, '<div class="starbar-meta">', '/10')
128        self.rating = gutils.strip_tags(self.rating)
129        self.rating = string.replace(self.rating, ",", ".")
130        if self.rating:
131            self.rating = float(self.rating)
132            self.rating = round(self.rating)
133
134    def get_screenplay(self):
135        self.screenplay = ''
136        parts = re.split('<a href=', gutils.trim(self.cast_page, '>Créditos como roteirista<', '</table>'))
137        if len(parts) > 1:
138            for part in parts[1:]:
139                screenplay = gutils.trim(part, '>', '<')
140                if screenplay == 'WGA':
141                    continue
142                screenplay = screenplay.replace(' (escrito por)', '')
143                screenplay = screenplay.replace(' and<', '<')
144                self.screenplay = self.screenplay + screenplay + ', '
145            if len(self.screenplay) > 2:
146                self.screenplay = self.screenplay[0:len(self.screenplay) - 2]
147
148    def get_cameraman(self):
149        self.cameraman = string.replace('<' + gutils.trim(self.cast_page, '>Direção de Fotografia de<', '</table>'), '(diretor de fotografia) ', '')
150
151    def __before_more(self, data):
152        for element in ['>Exibir mais<', '>Full summary<', '>Full synopsis<']:
153            tmp = string.find(data, element)
154            if tmp>0:
155                data = data[:tmp] + '>'
156        return data
157
158class SearchPlugin(movie.SearchMovie):
159    PATTERN = re.compile(r"""<a href=['"]/title/tt([0-9]+)/[^>]+[>](.*?)</td>""")
160
161    def __init__(self):
162        self.original_url_search   = 'http://www.imdb.pt/find?s=tt&q='
163        self.translated_url_search = 'http://www.imdb.pt/find?s=tt&q='
164        self.encode                = 'utf8'
165
166    def search(self, parent_window):
167        """Perform the web search"""
168        if not self.open_search(parent_window):
169            return None
170        return self.page
171
172    def get_searches(self):
173        """Try to find both id and film title for each search result"""
174        elements = string.split(self.page, '<tr')
175        if len(elements):
176            for element in elements[1:]:
177                match = self.PATTERN.findall(element)
178                if len(match) > 1:
179                    tmp = re.sub('^[0-9]+[.]', '', gutils.clean(match[1][1]))
180                    self.ids.append(match[1][0])
181                    self.titles.append(tmp)