Commit c626d9ab authored by hujun's avatar hujun

1

parent 155227b6
...@@ -12,6 +12,7 @@ import threading ...@@ -12,6 +12,7 @@ import threading
class DownFile: class DownFile:
download_num = 0 download_num = 0
old_num = 0 old_num = 0
local = threading.local()
def __init__(self, url, save_path="./", call_date_string="call_date", page_string="page_no"): def __init__(self, url, save_path="./", call_date_string="call_date", page_string="page_no"):
""" """
...@@ -79,81 +80,82 @@ class DownFile: ...@@ -79,81 +80,82 @@ class DownFile:
:param data: :param data:
:return: :return:
""" """
url = local_file = "" self.local.url = self.local.local_file = ""
count_sleep = 0 self.local.count_sleep = 0
for item in data: for item in data:
if count_sleep >= 50: if self.local.count_sleep >= 50:
self.log('sleep', '-----10s-----') self.log('sleep', '-----10s-----')
time.sleep(10) time.sleep(10)
count_sleep = 0 self.local.count_sleep = 0
if len(item['voice_file']) > 0: if len(item['voice_file']) > 0:
local_file = self.save_path + item['voice_file'] self.local.local_file = self.save_path + item['voice_file']
if self.check_file(local_file): if self.check_file(self.local.local_file):
self.lock.acquire() self.lock.acquire()
self.old_num = self.old_num + 1 self.old_num = self.old_num + 1
self.lock.release() self.lock.release()
continue continue
time_array = time.strptime(item['call_time'], '%Y-%m-%d %H:%M:%S') self.local.time_array = time.strptime(item['call_time'], '%Y-%m-%d %H:%M:%S')
local_dir = time.strftime('%Y%m%d', time_array) self.local.local_dir = time.strftime('%Y%m%d', self.local.time_array)
if int(item['type']) == 1: if int(item['type']) == 1:
try: try:
count_sleep = count_sleep + 1 # 增加暂停计数 self.local.count_sleep = self.local.count_sleep + 1 # 增加暂停计数
url = self.ali_yun_sdk(item['call_time'], item['call_id']) self.local.url = self.ali_yun_sdk(item['call_time'], item['call_id'])
is_ok = 0 self.local.is_ok = 0
except Exception as result: except Exception as result:
self.log("exception 20s------------", str(result)) self.log("exception 20s------------", str(result))
time.sleep(20) time.sleep(20)
is_ok = 1 self.local.is_ok = 1
# 第二次尝试 # 第二次尝试
if is_ok == 1: if self.local.is_ok == 1:
try: try:
count_sleep = count_sleep + 1 # 增加暂停计数 self.local.count_sleep = self.local.count_sleep + 1 # 增加暂停计数
url = self.ali_yun_sdk(item['call_time'], item['call_id']) self.local.url = self.ali_yun_sdk(item['call_time'], item['call_id'])
except Exception as result: except Exception as result:
self.log( self.log(
"two exception error-----call_time=" + item['call_time'] + '&call_id=' + item['call_id'], "two exception error-----call_time=" + item['call_time'] + '&call_id=' + item['call_id'],
str(result)) str(result))
if len(url) == 0: if len(self.local.url) == 0:
continue continue
# 当数据库未有记录 # 当数据库未有记录
if item['voice_file'] == '': if item['voice_file'] == '':
string_end = url.find("?") self.local.string_end = self.local.url.find("?")
file_url = url[0:string_end] self.local.file_url = self.local.url[0:self.local.string_end]
local_file = self.save_path + local_dir + '/' + re.findall(r".com\D.*", file_url)[0][5:] # 阿里云文件名 self.local.local_file = self.save_path + self.local.local_dir + '/' + \
re.findall(r".com\D.*", self.local.file_url)[0][5:] # 阿里云文件名
else: else:
local_file = self.save_path + '/' + item['voice_file'] # 使用已载录音文件名 self.local.local_file = self.save_path + '/' + item['voice_file'] # 使用已载录音文件名
else: else:
if not item['mp3_url']: if not item['mp3_url']:
continue continue
url = item['mp3_url'] self.local.url = item['mp3_url']
array = url.split('/') self.local.array = self.local.url.split('/')
local_file = self.save_path + local_dir + '/' + array[-1] # 容联云文件名 self.local.local_file = self.save_path + self.local.local_dir + '/' + self.local.array[-1] # 容联云文件名
if len(local_file) == 0: if len(self.local.local_file) == 0:
continue continue
if self.check_file(local_file): if self.check_file(self.local.local_file):
self.lock.acquire() self.lock.acquire()
self.old_num = self.old_num + 1 self.old_num = self.old_num + 1
self.lock.release() self.lock.release()
continue continue
self.check_dir(self.save_path + local_dir) self.check_dir(self.save_path + self.local.local_dir)
result = request.urlretrieve(url, local_file) self.local.result = request.urlretrieve(self.local.url, self.local.local_file)
self.lock.acquire() self.lock.acquire()
self.download_num = self.download_num + 1 self.download_num = self.download_num + 1
self.lock.release() self.lock.release()
self.log('url', url) self.log('url', self.local.url)
self.log('download_file', result) self.log('download_file', self.local.result)
self.log("download_num", self.download_num) self.log("download_num", self.download_num)
self.log("old_num", self.old_num) self.log("old_num", self.old_num)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment