1# -*- coding: utf-8 -*-
2
3__revision__ = '$Id$'
4
5# Copyright (c) 2010-2015 Enrico Carlesso
6# Copyright (c) 2011 Filippo Valsorda
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU Library General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
21
22# You may use and distribute this software under the terms of the
23# GNU General Public License, version 2 or later
24
25import gutils, movie
26import string, re
27
28plugin_name         = 'IMDb-IT'
29plugin_description  = 'Internet Movie Database in Italiano'
30plugin_url          = 'italian.imdb.com'
31plugin_language     = _('Italian')
32plugin_author       = 'Enrico Carlesso, Filippo Valsorda'
33plugin_author_email = 'enrico@ecarlesso.org, filosottile.wiki -at- gmail com'
34plugin_version      = '0.3'
35
36class Plugin(movie.Movie):
37    def __init__(self, id):
38        self.encode   = 'iso8859-1'
39        self.movie_id = id
40        self.url      = "http://www.imdb.it/title/tt%s" % self.movie_id
41        self.o_url    = "http://www.imdb.com/title/tt%s" % self.movie_id
42
43    def initialize(self):
44        self.cast_page = self.open_page(url=self.url + '/fullcredits')
45        self.plot_page = self.open_page(url=self.url + '/plotsummary')
46        self.o_page = self.open_page(url=self.o_url)
47
48    def get_image(self):
49        tmp = string.find(self.page, 'a name="poster"')
50        if tmp == -1:        # poster not available
51            self.image_url = ''
52        else:
53            self.image_url = gutils.trim(self.page[tmp:], 'src="', '"')
54
55    def get_o_title(self):
56        self.o_title = gutils.trim(self.o_page, '<span class="title-extra">', '<i>(original title)</i>')
57        if self.o_title == '':
58            self.o_title = gutils.regextrim(self.o_page, '<h1>', '([ ]|[&][#][0-9]+[;])<span')
59        if self.o_title == '':
60            self.o_title = re.sub('[(].*', '', gutils.trim(self.o_page, '<title>', '</title>'))
61
62    def get_title(self):
63        self.title = gutils.regextrim(self.page, '<h1>', '([ ]|[&][#][0-9]+[;])<span')
64        if self.title == '':
65            self.title = re.sub('[(].*', '', gutils.trim(self.page, '<title>', '</title>'))
66
67    def get_director(self):
68        self.director = ''
69        parts = re.split('<a href=', gutils.trim(self.cast_page, '>Regia di<', '</table>'))
70        if len(parts) > 1:
71            for part in parts[1:]:
72                director = gutils.trim(part, '>', '<')
73                self.director = self.director + director + ', '
74            self.director = self.director[0:len(self.director) - 2]
75
76    def get_plot(self):
77        self.plot = gutils.trim(self.plot_page, '<div id="swiki.2.1">', '</div>')
78        self.plot = gutils.convert_entities(self.plot)
79
80    def get_year(self):
81        self.year = gutils.trim(self.page, '<title>', '</title>')
82        self.year = gutils.trim(self.year, '(', ')')
83
84    def get_runtime(self):
85        self.runtime = gutils.trim(self.page, '<h5>Durata:</h5>', ' min')
86
87    def get_genre(self):
88        self.genre = gutils.trim(self.page, '<h5>Genere:</h5>', '</div>')
89        self.genre = self.__before_more(self.genre)
90
91    def get_cast(self):
92        self.cast = ''
93        self.cast = gutils.trim(self.cast_page, '<table class="cast">', '</table>')
94        if self.cast == '':
95            self.cast = gutils.trim(self.page, '<table class="cast">', '</table>')
96        self.cast = string.replace(self.cast, ' ... ', _(' as '))
97        self.cast = string.replace(self.cast, '...', _(' as '))
98        self.cast = string.replace(self.cast, '</tr><tr>', "\n")
99        self.cast = re.sub('</tr>[ \t]*<tr[ \t]*class="even">', "\n", self.cast)
100        self.cast = re.sub('</tr>[ \t]*<tr[ \t]*class="odd">', "\n", self.cast)
101        self.cast = gutils.convert_entities(self.cast)
102
103    def get_classification(self):
104        self.classification = gutils.trim(self.page, '<h5>Divieti:</h5>', '</div>')
105
106    def get_studio(self):
107        self.studio = gutils.trim(self.page, '<h5>Compagnia:</h5>', '</a>')
108
109    def get_o_site(self):
110        self.o_site = "http://www.imdb.it/title/tt%s/officialsites" % self.movie_id
111
112    def get_site(self):
113        self.site = "http://www.imdb.it/title/tt%s" % self.movie_id
114
115    def get_trailer(self):
116        self.trailer = "http://www.imdb.it/title/tt%s/trailers" % self.movie_id
117
118    def get_country(self):
119        self.country = gutils.trim(self.page, '<h5>Nazionalit&#xE0;:</h5>', '</div>')
120
121    def get_rating(self):
122        self.rating = gutils.trim(self.page, '<div class="starbar-meta">', '/10</b>')
123        self.rating = gutils.clean(self.rating).replace(',', '.')
124        if self.rating:
125            try:
126                self.rating = round(float(self.rating), 0)
127            except Exception as e:
128                self.rating = 0
129
130    def get_notes(self):
131        self.notes = ''
132        language = gutils.trim(self.page, '<h5>Lingua:</h5>', '</div>')
133        language = gutils.strip_tags(language)
134        language = re.sub('[\n]+', '', language)
135        language = re.sub('[ ]+', ' ', language)
136        language = language.rstrip()
137        color = gutils.trim(self.page, '<h5>Colore:</h5>', '</div>')
138        color = gutils.strip_tags(color)
139        color = re.sub('[\n]+', '', color)
140        color = re.sub('[ ]+', ' ', color)
141        color = color.rstrip()
142        sound = gutils.trim(self.page, '<h5>Sonoro:</h5>', '</div>')
143        sound = gutils.strip_tags(sound)
144        sound = re.sub('[\n]+', '', sound)
145        sound = re.sub('[ ]+', ' ', sound)
146        sound = sound.rstrip()
147        date = gutils.trim(self.page, '<h5>Data di uscita:</h5>', '<a class="tn15more inline"')
148        date = re.sub('[\n]+', '', date)
149        date = re.sub('[ ]+', ' ', date)
150        date = gutils.clean(date)
151        if len(language)>0:
152            self.notes = "%s: %s\n" %(_('Language'), language)
153        if len(sound)>0:
154            self.notes += "%s: %s\n" %(gutils.strip_tags(_('<b>Audio</b>')), sound)
155        if len(color)>0:
156            self.notes += "%s: %s\n" %(_('Color'), color)
157        if len(date)>0:
158            self.notes += "%s: %s\n" %(_('Data di uscita'), date)
159
160    def get_screenplay(self):
161        self.screenplay = ''
162        parts = re.split('<a href=', gutils.trim(self.cast_page, '>Scritto da<', '</table>'))
163        if len(parts) > 1:
164            for part in parts[1:]:
165                screenplay = gutils.trim(part, '>', '<')
166                if screenplay == 'WGA':
167                    continue
168                screenplay = screenplay.replace(' (scritto da)', '')
169                screenplay = screenplay.replace(' e<', '<')
170                self.screenplay = self.screenplay + screenplay + ', '
171            if len(self.screenplay) > 2:
172                self.screenplay = self.screenplay[0:len(self.screenplay) - 2]
173
174    def get_cameraman(self):
175        self.cameraman = ''
176
177    def __before_more(self, data):
178        tmp = string.find(data, '>ancora<')
179        if tmp > 0:
180            data = data[:tmp] + '>'
181        return data
182
183class SearchPlugin(movie.SearchMovie):
184    PATTERN = re.compile(r"""<a href=['"]/title/tt([0-9]+)/[^>]+[>](.*?)</td>""")
185    PATTERN_DIRECT = re.compile(r"""value="/title/tt([0-9]+)""")
186
187    def __init__(self):
188        self.original_url_search   = 'http://www.imdb.it/find?s=tt&q='
189        self.translated_url_search = 'http://www.imdb.it/find?s=tt&q='
190        self.encode                = 'utf8'
191
192    def search(self,parent_window):
193        if not self.open_search(parent_window):
194            return None
195        return self.page
196
197    def get_searches(self):
198        elements = string.split(self.page, '<tr')
199        if len(elements):
200            for element in elements[1:]:
201                match = self.PATTERN.findall(element)
202                if len(match) > 1:
203                    tmp = re.sub('^[0-9]+[.]', '', gutils.clean(match[1][1]))
204                    self.ids.append(match[1][0])
205                    self.titles.append(tmp)
206        if len(self.ids) < 2:
207            # try to find a direct result
208            match = self.PATTERN_DIRECT.findall(self.page)
209            if len(match) > 0:
210                self.ids.append(match[0])
211