本文整理汇总了Python中variety.Util.Util.get_local_name方法的典型用法代码示例。如果您正苦于以下问题:Python Util.get_local_name方法的具体用法?Python Util.get_local_name怎么用?Python Util.get_local_name使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类variety.Util.Util
的用法示例。
在下文中一共展示了Util.get_local_name方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get_local_name
# 需要导入模块: from variety.Util import Util [as 别名]
# 或者: from variety.Util.Util import get_local_name [as 别名]
def test_get_local_name(self):
self.assertEqual("img.jpg", Util.get_local_name("http://example.com/a/img?a=b"))
self.assertEqual("img.jpg", Util.get_local_name("http://example.com/a/img.jpg?a=b"))
self.assertEqual("img.jpg", Util.get_local_name("http://example.com/a/img.jpg#x"))
self.assertEqual("img.jpg", Util.get_local_name("http://example.com/a/img.jpg?a=b#x"))
self.assertEqual("im g.jpg", Util.get_local_name("http://example.com/a/im%20g.jpg?a=b#x"))
self.assertEqual("im_g.jpg", Util.get_local_name("http://example.com/a/im%22g.jpg?a=b#x"))
self.assertTrue(len(Util.get_local_name("a" * 1000 + ".jpg")) < 255)
示例2: get_local_filename
# 需要导入模块: from variety.Util import Util [as 别名]
# 或者: from variety.Util.Util import get_local_name [as 别名]
def get_local_filename(self, url):
return os.path.join(self.target_folder, Util.get_local_name(url))
示例3: is_in_favorites
# 需要导入模块: from variety.Util import Util [as 别名]
# 或者: from variety.Util.Util import get_local_name [as 别名]
def is_in_favorites(self, url):
return self.parent and os.path.exists(os.path.join(self.parent.options.favorites_folder, Util.get_local_name(url)))
示例4: fetch
# 需要导入模块: from variety.Util import Util [as 别名]
# 或者: from variety.Util.Util import get_local_name [as 别名]
def fetch(url, to_folder, origin_url=None,
source_type=None, source_location=None, source_name=None,
extra_metadata={},
progress_reporter=lambda a, b: None, verbose=True):
reported = verbose
try:
logger.info(lambda: "Trying to fetch URL %s to %s " % (url, to_folder))
if verbose:
progress_reporter(_("Fetching"), url)
if url.startswith('javascript:'):
if verbose:
progress_reporter(_("Not an image"), url)
return None
if url.find('://') < 0:
url = "file://" + url
r = Util.request(url, stream=True)
if not "content-type" in r.headers:
logger.info(lambda: "Unknown content-type for url " + url)
if verbose:
progress_reporter(_("Not an image"), url)
return None
ct = r.headers["content-type"]
if not ct.startswith("image/"):
logger.info(lambda: "Unsupported content-type for url " + url + ": " + ct)
if verbose:
progress_reporter(_("Not an image"), url)
return None
local_name = Util.get_local_name(r.url)
if "content-disposition" in r.headers:
cd = r.headers["content-disposition"]
cd_name = ImageFetcher.extract_filename_from_content_disposition(cd)
if cd_name:
local_name = cd_name
filename = os.path.join(to_folder, local_name)
if os.path.exists(filename):
m = Util.read_metadata(filename)
if m and m.get("imageURL") == url:
logger.info(lambda: "Local file already exists (%s)" % filename)
return filename
else:
logger.info(lambda:
"File with same name already exists, but from different imageURL; renaming new download")
filename = Util.find_unique_name(filename)
local_name = os.path.basename(filename)
logger.info(lambda: "Fetching to " + filename)
if not reported:
reported = True
progress_reporter(_("Fetching"), url)
with open(filename, 'wb') as f:
Util.request_write_to(r, f)
try:
img = Image.open(filename)
except Exception:
progress_reporter(_("Not an image"), url)
os.unlink(filename)
return None
if img.size[0] < 400 or img.size[1] < 400:
# too small - delete and do not use
progress_reporter(_("Image too small, ignoring it"), url)
os.unlink(filename)
return None
metadata = {"sourceType": source_type or 'fetched',
"sourceName": source_name or "Fetched",
"sourceURL": origin_url or url,
"imageURL": url}
if source_location:
metadata["sourceLocation"] = source_location
metadata.update(extra_metadata)
Util.write_metadata(filename, metadata)
logger.info(lambda: "Fetched %s to %s." % (url, filename))
return filename
except Exception, e:
logger.exception(lambda: "Fetch failed for URL " + url)
if reported:
if isinstance(e, HTTPError) and e.response.status_code in (403, 404):
progress_reporter(
_("Sorry, got %s error...") % str(e.response.status_code),
_("This means the link is no longer valid"))
else:
progress_reporter(
_("Fetch failed for some reason"),
_("To get more information, please run Variety from terminal with -v option and retry the action"))
return None