本文整理汇总了Python中CommonUtils.md5方法的典型用法代码示例。如果您正苦于以下问题:Python CommonUtils.md5方法的具体用法?Python CommonUtils.md5怎么用?Python CommonUtils.md5使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CommonUtils
的用法示例。
在下文中一共展示了CommonUtils.md5方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: downloadThumbnail
# 需要导入模块: import CommonUtils [as 别名]
# 或者: from CommonUtils import md5 [as 别名]
def downloadThumbnail(self):
"""
下载缩略图
"""
cacheDir = "cache" + os.path.sep + self._infoType + os.path.sep + "img" + os.path.sep + str(self._newsId)
url = self._thumbnail
thumbnailPath = self.requestUrlContent(url, cacheDir, CommonUtils.md5(url) + os.path.splitext(url)[-1])
if not os.path.isfile(thumbnailPath):
return None
# 处理成5种尺寸, 按宽度自适应
# XS:100, S:200, M:400, L:600, XL:900
im = Image.open(thumbnailPath)
width, height = im.size
filename, ext = os.path.splitext(os.path.basename(thumbnailPath))
thumbnailCachePath = self._thumbnailCachePath
# XS
target_file = os.path.join(thumbnailCachePath, filename+"_portfolio_"+"XS"+ext)
try:
_width = 100
_heigh = _width * height / width
out = im.resize((_width, _heigh), Image.ANTIALIAS)
out.save(target_file)
except Exception, e:
shutil.copyfile(thumbnailPath, target_file)
示例2: requestUrlContent
# 需要导入模块: import CommonUtils [as 别名]
# 或者: from CommonUtils import md5 [as 别名]
def requestUrlContent(url, cache_dir=None, filename=None):
if cache_dir != None and not os.path.isdir(cache_dir):
os.makedirs(cache_dir)
ext = os.path.splitext(url)[-1]
if filename == None:
filename = CommonUtils.md5(url) + ext
target_path = None
if cache_dir == None:
target_path = tempfile.mktemp()
else:
target_path = os.path.join(".", cache_dir)
target_path = os.path.join(target_path, filename)
if target_path == None:
target_path = tempfile.mktemp()
wget = 'wget --user-agent="Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"'
command = wget + ' "%s" -O %s --timeout=60 --tries=2'%(url, target_path)
if (not os.path.isfile(target_path)):
state = os.system(command.encode("utf-8")) # 在windows下是gb2312, 在ubuntu主机上应该是utf-8
if state != 0:
print url, "download failed!"
else:
pass
# 需要保证返回的字符串是ascii编码,否则lxml xpath解析可能会出问题
# phantomjs保存的文件是utf-8编码,所以需要进行转码为ascii编码
return open(target_path).read()
示例3: requestUrlContent
# 需要导入模块: import CommonUtils [as 别名]
# 或者: from CommonUtils import md5 [as 别名]
def requestUrlContent(self, url, cache_dir=None, filename=None, js_enable=False, force_update=False):
if cache_dir != None and not os.path.isdir(cache_dir):
os.makedirs(cache_dir)
ext = os.path.splitext(url)[-1]
if filename == None:
filename = CommonUtils.md5(url) + ext
target_path = None
if cache_dir == None:
target_path = tempfile.mktemp()
else:
target_path = os.path.join(".", cache_dir)
target_path = os.path.join(target_path, filename)
if target_path == None:
target_path = tempfile.mktemp()
wget = 'wget --user-agent="Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"'
command = wget + ' "%s" -O %s --timeout=60 --tries=2'%(url, target_path)
if js_enable:
load_web_page_js_dir = os.path.join(".", "bin")
load_web_page_js = os.path.join(load_web_page_js_dir, "load.js")
# load_web_page_js = load_web_page_js.replace("\\", "/")
command = 'phantomjs --load-images=false "%s" "%s" "%s"'%(load_web_page_js, url, target_path)
# print "current dir", os.path.realpath(os.curdir)
# print "Request Url:", command.encode("utf-8")
if (not os.path.isfile(target_path)) or force_update:
state = os.system(command.encode("utf-8")) # 在windows下是gb2312, 在ubuntu主机上应该是utf-8
print url, "download successful!"
else:
# print url, "is already downloaded!"
pass
if ext[1:].lower() in PIC_SUFFIX:
return target_path
# 需要保证返回的字符串是ascii编码,否则lxml xpath解析可能会出问题
# phantomjs保存的文件是utf-8编码,所以需要进行转码为ascii编码
if js_enable:
return open(target_path).read().decode("utf8").encode("gb18030")
else:
return open(target_path).read()
示例4: requestUrlContent
# 需要导入模块: import CommonUtils [as 别名]
# 或者: from CommonUtils import md5 [as 别名]
def requestUrlContent(self, url, cache_dir=None, filename=None):
if cache_dir != None and not os.path.isdir(cache_dir):
os.makedirs(cache_dir)
ext = os.path.splitext(url)[-1]
if filename == None:
filename = CommonUtils.md5(url) + ext
target_path = None
if cache_dir == None:
target_path = tempfile.mktemp()
else:
target_path = os.path.join(".", cache_dir)
target_path = os.path.join(target_path, filename)
if target_path == None:
target_path = tempfile.mktemp()
command = 'wget "%s" -O %s '%(url, target_path)
print "Request Url:", command
state = 0
if not os.path.isfile(target_path):
# 在windows下是gb2312, 在ubuntu主机上应该是utf-8
if platform.system() == 'Windows':
state = os.system(command.encode("gb2312"))
else:
state = os.system(command.encode("utf-8"))
else:
print url, "is already downloaded!"
if ext[1:].lower() in PIC_SUFFIX:
# print "requestUrlContent state", state
if state != 0:
return None
return target_path
return open(target_path).read()