本文整理汇总了Python中urllib.FancyURLopener.find方法的典型用法代码示例。如果您正苦于以下问题:Python FancyURLopener.find方法的具体用法?Python FancyURLopener.find怎么用?Python FancyURLopener.find使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib.FancyURLopener
的用法示例。
在下文中一共展示了FancyURLopener.find方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_poster
# 需要导入模块: from urllib import FancyURLopener [as 别名]
# 或者: from urllib.FancyURLopener import find [as 别名]
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)
示例2: get_poster
# 需要导入模块: from urllib import FancyURLopener [as 别名]
# 或者: from urllib.FancyURLopener import find [as 别名]
def get_poster(self, f, result, current_poster):
from widgets import reconnect_add_signals
if f is None:
treeselection = self.widgets['results']['treeview'].get_selection()
(tmp_model, tmp_iter) = treeselection.get_selected()
if tmp_iter is None:
return False
f = int(tmp_model.get_value(tmp_iter, 0))
self.widgets['results']['window'].hide()
file_to_copy = tempfile.mktemp(suffix=self.widgets['movie']['number'].get_text(), \
dir=self.locations['temp'])
file_to_copy += ".jpg"
if len(result[f].ImageUrlLarge):
try:
progress = movie.Progress(self.widgets['window'],_("Fetching poster"),_("Wait a moment"))
retriever = movie.Retriever(result[f].ImageUrlLarge, self.widgets['window'], progress, file_to_copy)
retriever.start()
while retriever.isAlive():
progress.pulse()
if progress.status:
retriever.suspend()
while gtk.events_pending():
gtk.main_iteration()
progress.close()
urlcleanup()
except:
gutils.warning(self, _("Sorry. A connection error has occurred."))
if os.path.isfile(file_to_copy):
try:
im = Image.open(file_to_copy)
except IOError:
self.debug.show("failed to identify %s"%file_to_copy)
if im.size == (1,1):
from urllib import FancyURLopener, urlretrieve
url = FancyURLopener().open("http://www.amazon.com/gp/product/images/%s" % result[f].Asin).read()
if url.find('no-img-sm._V47056216_.gif') > 0:
self.debug.show('No image available')
gutils.warning(self, _("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:
self.debug.show("failed to identify %s"%file_to_copy)
if im.mode != 'RGB': # convert GIFs
im = im.convert('RGB')
im.save(file_to_copy, 'JPEG')
handler = self.widgets['big_poster'].set_from_file(file_to_copy)
self.widgets['poster_window'].show()
self.widgets['poster_window'].move(0,0)
response = gutils.question(self, \
_("Do you want to use this poster instead?"), \
1, self.widgets['window'])
if response == -8:
self.debug.show("Using fetched poster, updating and removing old one from disk.")
update_image(self, self.widgets['movie']['number'].get_text(), file_to_copy)
else:
self.debug.show("Reverting to previous poster and deleting new one from disk.")
try:
os.remove(file_to_copy)
except:
self.debug.show("no permission for %s"%file_to_copy)
self.widgets['poster_window'].hide()
else:
gutils.warning(self, _("Sorry. This movie is listed but has no poster available at Amazon.com."))
reconnect_add_signals(self)
示例3: get_poster
# 需要导入模块: from urllib import FancyURLopener [as 别名]
# 或者: from urllib.FancyURLopener import find [as 别名]
def get_poster(self, f, result):
from widgets import reconnect_add_signals
if f is None:
treeselection = self.widgets['results']['treeview'].get_selection()
(tmp_model, tmp_iter) = treeselection.get_selected()
if tmp_iter is None:
return False
f = int(tmp_model.get_value(tmp_iter, 0))
self.widgets['results']['window'].hide()
file_to_copy = tempfile.mktemp(suffix=self.widgets['movie']['number'].get_text(), \
dir=self.locations['temp'])
file_to_copy += ".jpg"
canceled = False
if len(result.Item[f].LargeImage.URL):
try:
progress = movie.Progress(self.widgets['window'],_("Fetching poster"),_("Wait a moment"))
retriever = movie.Retriever(result.Item[f].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" % result.Item[f].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)
response = gutils.question(_("Do you want to use this poster instead?"), True, self.widgets['window'])
if response == -8:
log.info("Using fetched poster, updating and removing old one from disk.")
update_image(self, self.widgets['movie']['number'].get_text(), 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("no permission for %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("no permission for %s"%file_to_copy)
# reconnect the signals to the shared result list
reconnect_add_signals(self)