def _get(self, url, parent_window, decode=True): # initialize the progress dialog once for the following search process if not self.progress: self.progress = Progress(parent_window) data = None url = url.encode('utf-8', 'replace') log.debug('Using url <%s>', url) self.progress.set_data(parent_window, _('Searching'), _('Wait a moment'), True) try: retriever = Retriever(url, parent_window, self.progress) retriever.start() while retriever.isAlive(): self.progress.pulse() if self.progress.status: retriever.join() while gtk.events_pending(): gtk.main_iteration() try: if retriever.html: ifile = file(retriever.html[0], 'rb') try: data = ifile.read() finally: ifile.close() # check for gzip compressed pages before decoding to unicode if len(data) > 2 and data[0:2] == '\037\213': data = gutils.decompress(data) if decode: data = data.decode('utf-8', 'replace') os.remove(retriever.html[0]) except IOError: log.exception('') finally: self.progress.hide() urlcleanup() return data
def get_poster(self, item): """Returns file path to the new poster""" from movie import Progress, Retriever file_to_copy = tempfile.mktemp(suffix=self.widgets["movie"]["number"].get_text(), dir=self.locations["temp"]) file_to_copy += ".jpg" canceled = False try: progress = Progress(self.widgets["window"], _("Fetching poster"), _("Wait a moment")) retriever = Retriever(item.LargeImage.URL, self.widgets["window"], progress, file_to_copy) retriever.start() while retriever.isAlive(): progress.pulse() if progress.status: canceled = True while gtk.events_pending(): gtk.main_iteration() progress.close() urlcleanup() except: canceled = True gutils.warning(_("Sorry. A connection error has occurred.")) try: os.remove(file_to_copy) except: log.error("no permission for %s" % file_to_copy) if not canceled: if os.path.isfile(file_to_copy): im = None try: im = Image.open(file_to_copy) except IOError: log.warn("failed to identify %s" % file_to_copy) if im and im.size == (1, 1): url = FancyURLopener().open("http://www.amazon.com/gp/product/images/%s" % item.ASIN).read() if url.find("no-img-sm._V47056216_.gif") > 0: log.warn("No image available") gutils.warning(_("Sorry. This movie is listed but has no poster available at Amazon.com.")) return False url = gutils.after(url, 'id="imageViewerDiv"><img src="') url = gutils.before(url, '" id="prodImage"') urlretrieve(url, file_to_copy) try: im = Image.open(file_to_copy) except IOError: log.warn("failed to identify %s", file_to_copy) if not im: # something wrong with the image, give some feedback to the user log.warn("No image available") gutils.warning(_("Sorry. This movie is listed but has no poster available at Amazon.com.")) return False if im.mode != "RGB": # convert GIFs im = im.convert("RGB") im.save(file_to_copy, "JPEG") # set to None because the file is locked otherwise (os.remove throws an exception) im = None handler = self.widgets["big_poster"].set_from_file(file_to_copy) self.widgets["poster_window"].show() self.widgets["poster_window"].move(0, 0) if gutils.question(_("Do you want to use this poster instead?"), self.widgets["window"]): return file_to_copy else: log.info("Reverting to previous poster and deleting new one from disk.") try: os.remove(file_to_copy) except: log.error("cannot remove %s", file_to_copy) self.widgets["poster_window"].hide() else: gutils.warning(_("Sorry. This movie is listed but has no poster available at Amazon.com.")) else: # cleanup temporary files after canceling the download if os.path.isfile(file_to_copy): try: os.remove(file_to_copy) except: log.error("cannot remove %s", file_to_copy)
class GriffithExtension(Base): name = 'MoviePosterDB' description = _('Fetch posters from MoviePosterDB.com') author = 'Michael Jahn' email = '*****@*****.**' version = 1 api = 1 enabled = True toolbar_icon = 'ge_movieposterdb.png' baseurltitle = 'http://www.movieposterdb.com/embed.inc.php?movie_title=%s' baseurltitleyear = 'http://www.movieposterdb.com/embed.inc.php?movie_title=%s[%s]' progress = None def toolbar_icon_clicked(self, widget, movie): log.info('fetching poster from MoviePosterDB.com...') self.movie = movie # correction of the movie name for the search o_title = None title = None if movie.o_title: if movie.o_title.endswith(', The'): o_title = u"The %s" % movie.o_title[:-5] else: o_title = movie.o_title if movie.title: if movie.title.endswith(', The'): title = u"The %s" % movie.title[:-5] else: title = movie.title # try to get an url to the large version of a poster for the movie # (requests are in the order: # original title + year, original title only, title + year, title only) try: largeurl = None data = False if o_title: if movie.year: url = self.baseurltitleyear % (o_title, movie.year) data = self._get(url, self.widgets['window']) if data and data.find('movie/0000000') < 0: largeurl = gutils.trim(data, 'src=\\"', '\\"').replace('/t_', '/l_') if not data or not largeurl: url = self.baseurltitle % o_title data = self._get(url, self.widgets['window']) if data and data.find('movie/0000000') < 0: largeurl = gutils.trim(data, 'src=\\"', '\\"').replace('/t_', '/l_') if not data or not largeurl and title and title != o_title: if movie.year: url = self.baseurltitleyear % (title, movie.year) data = self._get(url, self.widgets['window']) if data and data.find('movie/0000000') < 0: largeurl = gutils.trim(data, 'src=\\"', '\\"').replace('/t_', '/l_') if not data or not largeurl: url = self.baseurltitle % title data = self._get(url, self.widgets['window']) if data and data.find('movie/0000000') < 0: largeurl = gutils.trim(data, 'src=\\"', '\\"').replace('/t_', '/l_') except: log.exception('') gutils.warning(_('No posters found for this movie.')) return if not data or not largeurl: gutils.warning(_('No posters found for this movie.')) return # got the url for a large poster, fetch the data, show preview and update the # movie entry if the user want it data = self._get(largeurl, self.widgets['window'], decode=False) if not data: gutils.warning(_('No posters found for this movie.')) return if self._show_preview(data): update_image_from_memory(self.app, movie.number, data) def _show_preview(self, data): loader = gtk.gdk.PixbufLoader() loader.write(data, len(data)) loader.close() # show before set_from_pixbuf because it doesn't resize otherwise self.widgets['poster_window'].show() handler = self.widgets['big_poster'].set_from_pixbuf(loader.get_pixbuf()) result = gutils.question(_("Do you want to use this poster instead?"), self.widgets['window']) self.widgets['poster_window'].hide() return result def _get(self, url, parent_window, decode=True): # initialize the progress dialog once for the following search process if not self.progress: self.progress = Progress(parent_window) data = None url = url.encode('utf-8', 'replace') log.debug('Using url <%s>', url) self.progress.set_data(parent_window, _('Searching'), _('Wait a moment'), True) try: retriever = Retriever(url, parent_window, self.progress) retriever.start() while retriever.isAlive(): self.progress.pulse() if self.progress.status: retriever.join() while gtk.events_pending(): gtk.main_iteration() try: if retriever.html: ifile = file(retriever.html[0], 'rb') try: data = ifile.read() finally: ifile.close() # check for gzip compressed pages before decoding to unicode if len(data) > 2 and data[0:2] == '\037\213': data = gutils.decompress(data) if decode: data = data.decode('utf-8', 'replace') os.remove(retriever.html[0]) except IOError: log.exception('') finally: self.progress.hide() urlcleanup() return data
def get_poster(self, item): """Returns file path to the new poster""" from movie import Progress, Retriever file_to_copy = tempfile.mktemp(suffix=self.widgets['movie']['number'].get_text(), \ dir=self.locations['temp']) file_to_copy += ".jpg" canceled = False try: progress = Progress(self.widgets['window'], _("Fetching poster"), _("Wait a moment")) retriever = Retriever(item.LargeImage.URL, self.widgets['window'], progress, file_to_copy) retriever.start() while retriever.isAlive(): progress.pulse() if progress.status: canceled = True while gtk.events_pending(): gtk.main_iteration() progress.close() urlcleanup() except: canceled = True gutils.warning(_("Sorry. A connection error has occurred.")) try: os.remove(file_to_copy) except: log.error("no permission for %s" % file_to_copy) if not canceled: if os.path.isfile(file_to_copy): im = None try: im = Image.open(file_to_copy) except IOError: log.warn("failed to identify %s" % file_to_copy) if im and im.size == (1, 1): url = FancyURLopener().open("http://www.amazon.com/gp/product/images/%s" % item.ASIN).read() if url.find('no-img-sm._V47056216_.gif') > 0: log.warn('No image available') gutils.warning(_("Sorry. This movie is listed but has no poster available at Amazon.com.")) return False url = gutils.after(url, 'id="imageViewerDiv"><img src="') url = gutils.before(url, '" id="prodImage"') urlretrieve(url, file_to_copy) try: im = Image.open(file_to_copy) except IOError: log.warn("failed to identify %s", file_to_copy) if not im: # something wrong with the image, give some feedback to the user log.warn('No image available') gutils.warning(_("Sorry. This movie is listed but has no poster available at Amazon.com.")) return False if im.mode != 'RGB': # convert GIFs im = im.convert('RGB') im.save(file_to_copy, 'JPEG') # set to None because the file is locked otherwise (os.remove throws an exception) im = None handler = self.widgets['big_poster'].set_from_file(file_to_copy) self.widgets['poster_window'].show() self.widgets['poster_window'].move(0, 0) if gutils.question(_("Do you want to use this poster instead?"), self.widgets['window']): return file_to_copy else: log.info("Reverting to previous poster and deleting new one from disk.") try: os.remove(file_to_copy) except: log.error('cannot remove %s', file_to_copy) self.widgets['poster_window'].hide() else: gutils.warning(_("Sorry. This movie is listed but has no poster available at Amazon.com.")) else: # cleanup temporary files after canceling the download if os.path.isfile(file_to_copy): try: os.remove(file_to_copy) except: log.error('cannot remove %s', file_to_copy)