From e2f17015fdea214968533f7d0de05f1bc1b4aae7 Mon Sep 17 00:00:00 2001 From: ForgQi <34411314+ForgQi@users.noreply.github.com> Date: Sun, 20 May 2018 01:24:45 +0800 Subject: [PATCH] delete dir --- configlog.ini | 39 ------ download.py | 229 ------------------------------- upload.py | 372 -------------------------------------------------- work.py | 177 ------------------------ 4 files changed, 817 deletions(-) delete mode 100644 configlog.ini delete mode 100644 download.py delete mode 100644 upload.py delete mode 100644 work.py diff --git a/configlog.ini b/configlog.ini deleted file mode 100644 index 90dc040..0000000 --- a/configlog.ini +++ /dev/null @@ -1,39 +0,0 @@ -[loggers] -keys=root, log01 - -[logger_root] -level=INFO -handlers=handler01 - -[logger_log01] -level=INFO -handlers=handler02 -qualname=log01 - - -[handlers] -keys=handler01,handler02 - -[handler_handler01] -class=StreamHandler -level=INFO -formatter=form02 -args=(sys.stdout,) - -[handler_handler02] -class=work.SafeRotatingFileHandler -level=INFO -formatter=form01 -args=("ds_update.log", "W0", 1, 1) - - -[formatters] -keys=form01,form02 - -[formatter_form01] -format=%(asctime)s %(filename)s[line:%(lineno)d](Pid:%(process)s) %(levelname)s %(message)s -[formatter_form02] -format=%(lineno)d[%(levelname)s]%(message)s - - - diff --git a/download.py b/download.py deleted file mode 100644 index a5f1fd9..0000000 --- a/download.py +++ /dev/null @@ -1,229 +0,0 @@ -from upload import * -import os -import requests -import json -import time -import youtube_dl - -# logger = logging.getLogger('log01') - -# links = ['www.twitch.tv/innovation_s2','www.panda.tv/1160340','www.twitch.tv/sc2soo','www.panda.tv/1150595','www.twitch.tv/kimdaeyeob3'] - -root_url = ['https://www.twitch.tv/', 'https://www.panda.tv/', 'https://api.twitch.tv/kraken/streams/'] - -links_id = { - '星际2Innovation吕布卫星人族天梯第一视角': ['innovation_s2', '1160340'], - '星际2soO输本虫族天梯第一视角': ['sc2soo', '1150595'], - '星际2sOs狗哥神族天梯第一视角': ['', '1160930'], - '星际2Stats拔本神族天梯第一视角': ['kimdaeyeob3'], - '星际2Dark暗本虫族天梯第一视角': ['qkrfuddn0'], - '星际2Scarlett噶姐虫族天梯第一视角': ['scarlettm'], - '星际2GuMiho砸本人族天梯第一视角': ['gumiho'], - '星际2Maru人族天梯第一视角': ['maru072'], - '星际2TY全教主全太阳人族天梯第一视角': ['sc2tyty'], - '星际2ByuN武圣人族天梯第一视角': ['byunprime'], - '星际2小herO神族天梯第一视角': ['dmadkr0818'], - '星际2Zest神族天梯第一视角': ['sc2_zest'], - '星际2PartinG跳跳胖丁神族天梯第一视角':['partingthebigboy'], - # '星际ForGG火车王人族天梯第一视角': ['forgg'], - # '星际2NoRegreT莲弟虫族天梯第一视角': ['noregret_'] - # 'test':['expertmma','37229'], - # 'test1':['byunprime','10003'] -} - -headers = { - 'client-id': 'jzkbprff40iqj646a697cyrvl0zt2m6' -} - - -def get_twitch_stream(url, key): - try: - res = requests.get(url, headers=headers) - res.close() - except requests.exceptions.SSLError: - logger.error('获取流信息发生错误') - logger.error(requests.exceptions.SSLError, exc_info = True) - return None - except requests.exceptions.ConnectionError: - logger.exception('During handling of the above exception, another exception occurred:') - return None - - try: - s = json.loads(res.text) - # s = res.json() - except json.decoder.JSONDecodeError: - logger.exception('Expecting value') - return None - print(key) - try: - stream = s['stream'] - except KeyError : - logger.error(KeyError, exc_info = True) - return None - return stream - - -def download_twitch_stream(dict, q, confirm_url, key_, url_, tfile_name_, value_=None): - status_ = get_twitch_stream(confirm_url,key_) - # print(status_) - if str(status_) != 'None': - if value_ is None: - value_ = dict[key_] - dict.pop(key_) - else: - logger.info('准备递归下载'+key_) - print('开始下载twitch', key_) - logger.info('开始下载twitch:' + key_) - ydl_opts = { - 'outtmpl': tfile_name_, - # 'format': '720p' - # 'external_downloader_args':['-timeout', '5'] - # 'keep_fragments':True - } - list_info = [] - - with youtube_dl.YoutubeDL() as ydl: - info = ydl.extract_info(url_, download=False) - for i in info['formats']: - list_info.append(i['format_id']) - - if key_ in ['星际2ByuN武圣人族天梯第一视角', '星际2Innovation吕布卫星人族天梯第一视角', '星际2Maru人族天梯第一视角']: - pass - elif '720p' in list_info: - ydl_opts['format'] = '720p' - elif '720p60' in list_info: - ydl_opts['format'] = '720p60' - - - try: - with youtube_dl.YoutubeDL(ydl_opts) as ydl: - - pid = os.getpid() - q.put([pid, tfile_name_]) - - ydl.download([url_]) - - print('下载完成') - logger.info('下载完成' + key_) - - uploads(tfile_name_, url_) - - except youtube_dl.utils.DownloadError: - - # print('分段下载') - try: - os.rename(tfile_name_ + '.part', tfile_name_[:-4] + str(time.time())[:10] + tfile_name_[-4:]) - logger.info('更名{0}为{1}+时间'.format(tfile_name_ + '.part', tfile_name_)) - except FileExistsError: - os.rename(tfile_name_ + '.part', tfile_name_[:-4] + str(time.time())[:10] + tfile_name_[-4:]) - logger.info('FileExistsError更名{0}为{1}'.format(tfile_name_ + '.part', tfile_name_)) - - download_twitch_stream(dict, q, confirm_url, key_, url_, tfile_name_, value_) - - finally: - dict[key_] = value_ - logger.info('退出下载') - - elif len(dict[key_]) == 1: - value_ = dict[key_] - dict.pop(key_) - - supplemental_upload(dict, tfile_name_, key_, url_, value_) - - -def download_panda_stream(dict, q, key_, url_, pfile_name_, value_=None): - if value_ is None: - value_ = dict[key_] - dict.pop(key_) - else: - logger.info('准备递归下载'+key_) - list_info = [] - with youtube_dl.YoutubeDL() as ydl: - try: - info = ydl.extract_info(url_, download=False) - for i in info['formats']: - list_info.append(i['format_id']) - except youtube_dl.utils.DownloadError: - logger.debug('%s未开播或读取下载信息失败' % key_) - - # logger.debug('准备补充上传'+key_) - supplemental_upload(dict, pfile_name_, key_, url_, value_) - return - - ydl_opts = { - 'outtmpl': pfile_name_, - # 'format': 'HD-flv', - } - - - # if 'HD-flv' in list_info: - # ydl_opts = { - # 'outtmpl': pfile_name_, - # 'format': 'HD-flv', - # } - # else: - # ydl_opts = { - # 'outtmpl': pfile_name_, - # 'format': 'SD-flv', - # # 'keep_fragments':True - # # 'postprocessors': [{ - # # 'key': 'FFmpegFixupM3u8', - # # 'preferredcodec': 'mp3', - # # 'preferredquality': '1900', - # # }], - # } - try: - with youtube_dl.YoutubeDL(ydl_opts) as ydl: - pid = os.getpid() - # print(pid) - q.put([pid, pfile_name_]) - - # if os.path.isfile(pfile_name_): - # os.rename(pfile_name_, pfile_name_ + '.part') - # logger.info('存在{0}更名为{1}'.format(pfile_name_, pfile_name_ + '.part')) - - logger.info('开始下载pandaTV' + key_) - ydl.download([url_]) - - print('下载完成') - logger.info('下载完成' + key_) - - with youtube_dl.YoutubeDL() as ydl: - try: - info = ydl.extract_info(url_, download=False) - except youtube_dl.utils.DownloadError: - - uploads(pfile_name_, url_) - - return - - logger.info('实际未下载完成' + key_) - - if os.path.isfile(pfile_name_): - os.rename(pfile_name_, pfile_name_[:-4] + str(time.time())[:10] + pfile_name_[-4:]) - logger.info('存在{0}更名为{1}'.format(pfile_name_, pfile_name_[:-4] + '时间' + pfile_name_[-4:])) - - download_panda_stream(dict, q, key_, url_, pfile_name_, value_) - except KeyboardInterrupt: - # except youtube_dl.utils.DownloadError: - try: - # logger.info('更名{0}'.format(pfile_name_+ '.part')) - os.rename(pfile_name_ + '.part', pfile_name_[:-4] + str(time.time())[:10] + pfile_name_[-4:]) - logger.info('下载进程发生KeyboardInterrupt更名{0}为{1}+时间'.format(pfile_name_ + '.part', pfile_name_)) - except FileExistsError: - os.rename(pfile_name_ + '.part', pfile_name_[:-4] + str(time.time())[:10] + pfile_name_[-4:]) - logger.info('FileExistsError:更名{0}为{1}'.format(pfile_name_ + '.part', pfile_name_)) - download_panda_stream(dict, q, key_, url_, pfile_name_, value_) - finally: - dict[key_] = value_ - logger.info('退出下载') - - -def download_stream(dict, q, confirm_url, key_, twitch_url_, panda_url_, tfile_name_, pfile_name_): - # print('子进程id%s'%(os.getpid())) - download_twitch_stream(dict, q, confirm_url, key_, twitch_url_, tfile_name_) - download_panda_stream(dict, q, key_, panda_url_, pfile_name_) - - -if __name__ == '__main__': - get_twitch_stream('https://api.twitch.tv/kraken/streams/1160340','233') diff --git a/upload.py b/upload.py deleted file mode 100644 index 8242a8c..0000000 --- a/upload.py +++ /dev/null @@ -1,372 +0,0 @@ -import json -import os -from selenium import webdriver -import selenium -import time -from bin.slider import slider_cracker -from selenium.webdriver.common.by import By -from selenium.webdriver.support.ui import WebDriverWait -from selenium.webdriver.support import expected_conditions as EC -from selenium.webdriver.common.keys import Keys -import logging - - -# 10800 18000 4110 - - -logger = logging.getLogger('log01') - -def get_file(filename): - file_list = [] - for file_name in os.listdir(): - if filename[:-15] in file_name: - file_list.append(file_name) - return sorted(file_list) - - -def remove_filelist(file_list): - for r in file_list: - os.remove(r) - logger.info('删除-' + r) - -def filter_file(file_list): - for r in file_list: - file_size = os.path.getsize(r) / 1024 / 1024 / 1024 - if file_size <= 0.1: - os.remove(r) - logger.info('过滤删除-' + r) - - -def is_element_exist(driver, xpath): - s = driver.find_elements_by_xpath(xpath=xpath) - if len(s) == 0: - print("元素未找到:%s"%xpath) - return False - elif len(s) == 1: - return True - else: - print("找到%s个元素:%s"%(len(s),xpath)) - return False - -def upload(video_list, link, title_): - - filename = 'bin/bilibili.cookie' - user_name = '' - pass_word = '' - - video_path = assemble_videopath(video_list) - - service_log_path = "{}/chromedriver.log".format('/home') - options = webdriver.ChromeOptions() - - options.add_argument('headless') - driver = webdriver.Chrome(executable_path='/usr/bin/chromedriver', chrome_options=options, - service_log_path=service_log_path) - try: - # service_log_path = "{}/chromedriver.log".format('/home') - # service_log_path = "{}\\chromedriver.log".format('D:\\bilibiliupload') - - - # video_path = '/home/wardiii.mp4' - # video_path = 'D:\\p2.flv\nD:\\p1.flv\nD:\\享受星际!!!看会比赛.flv' - # link = 'https://www.panda.tv/1160340' - - # options = webdriver.ChromeOptions() - # - # options.add_argument('headless') - - # options.add_argument('no-sandbox') - # options.binary_location = '/usr/bin/google-chrome-stable' - - - # driver = webdriver.Chrome(executable_path='/usr/bin/chromedriver', chrome_options=options, - # service_log_path=service_log_path) - # driver = webdriver.Chrome(executable_path=r'D:\bilibiliupload\chromedriver.exe',service_log_path=service_log_path) - driver.get("https://www.bilibili.com") - # driver.delete_all_cookies() - # cookies = [ - # {'domain': '.bilibili.com', 'expiry': 1611825707.027044, 'httpOnly': False, 'name': 'buvid3', 'path': '/', - # 'secure': False, 'value': 'A558A031-653B-4890-917D-C205F81B84F93220infoc'}, - # {'domain': '.bilibili.com', 'expiry': 1611825706, 'httpOnly': False, 'name': 'fts', 'path': '/', - # 'secure': False, 'value': '1517217707'}, - # {'domain': '.bilibili.com', 'expiry': 1519809706, 'httpOnly': False, 'name': 'finger', 'path': '/', - # 'secure': False, 'value': 'edc6ecda'}, - # {'domain': '.bilibili.com', 'expiry': 1519809723.74952, 'httpOnly': False, 'name': 'DedeUserID', - # 'path': '/', 'secure': False, 'value': '274986200'}, - # {'domain': '.bilibili.com', 'expiry': 1548753707.683233, 'httpOnly': False, 'name': 'sid', 'path': '/', - # 'secure': False, 'value': 'llkelw0h'}, - # {'domain': '.bilibili.com', 'expiry': 1519809723.749743, 'httpOnly': False, 'name': 'bili_jct', 'path': '/', - # 'secure': False, 'value': 'af58729164b6a192b07cd720ded677a5'}, - # {'domain': '.bilibili.com', 'expiry': 1519809723.749621, 'httpOnly': False, 'name': 'DedeUserID__ckMd5', - # 'path': '/', 'secure': False, 'value': '1206e39bc044fe98'}, - # {'domain': '.bilibili.com', 'expiry': 1519809723.749689, 'httpOnly': True, 'name': 'SESSDATA', 'path': '/', - # 'secure': False, 'value': '3e02f3d0%2C1519809723%2C3ee58d91'}, - # {'domain': '.bilibili.com', 'expiry': 2177452800.339961, 'httpOnly': False, 'name': 'LIVE_BUVID', - # 'path': '/', 'secure': False, 'value': 'AUTO7015172177246979'}, - # {'domain': '.bilibili.com', 'expiry': 1517221325.776721, 'httpOnly': True, 'name': '_dfcaptcha', - # 'path': '/', 'secure': False, 'value': 'a32213f1be85682d37cde24aedc2d135'}] - - with open(filename) as f: - new_cookie = json.load(f) - - for cookie in new_cookie: - # print(cookie) - driver.add_cookie(cookie) - - # cookie = driver.get_cookies() - # print(cookie) - # driver.get("https://www.bilibili.com") - - driver.get("https://member.bilibili.com/video/upload.html") - - # print(driver.title) - WebDriverWait(driver, 20).until( - EC.presence_of_element_located((By.NAME, 'file'))) - upload = driver.find_element_by_name('file') - - print(driver.title) - - cookie = driver.get_cookies() - # - with open(filename, "w") as f: - json.dump(cookie, f) - - # logger.info(driver.title) - logger.info('开始上传' + title_) - upload.send_keys(video_path) # send_keys - # sb = driver.find_element_by_xpath(r'//*[@id="app"]/div[3]/div/div/div/div/div') - # sb.click() - # with open('bilibili.html', 'w', encoding='utf-8') as f: - # f.write(driver.page_source) # 保存网页到本地 - # print('截图') - # screen_shot = driver.save_screenshot('bin/1.png') - # time.sleep(20) - # screen_shot = driver.save_screenshot('bin/0.png') - # print('截图') - # print(upload.get_attribute('value')) # check value - - time.sleep(1) - if is_element_exist(driver, r'//*[@id="app"]/div[3]/div/div/div/div/div'): - # # sb = driver.find_element_by_xpath(r'//*[@id="app"]/div[3]/div/div/div/div/div') - js = "var q=document.getElementsByClassName('new-feature-guide-container')[0].style.display='none';" - driver.execute_script(js) - # sb.click() - - while True: - info = driver.find_elements_by_xpath('//*[@id="item"]/div/div[2]/div[3]/div[1]/div[1]/div/div/div[2]/div[1]/div[3]') - # print(info) - for t in info: - # print(t) - if t.text != '': - print(t.text) - # else: - # print('出问题啦') - time.sleep(10) - text = driver.find_elements_by_xpath('//*[@id="item"]/div/div[2]/div[3]/div[1]/div[1]/div/div/div[2]/div[1]/div[2]') - aggregate = set() - for s in text: - print(s.text) - aggregate.add(s.text) - # if text == 'Upload complete' or text == '上传完成': - # break - - if len(aggregate) == 1 and ('Upload complete' in aggregate or '上传完成' in aggregate): - break - logger.info('上传%s\n个数%s' % (title_, len(info))) - - - # print(text) - # pageSource = driver.page_source - # print(pageSource) - # hid = driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[2]/div/div[2]/div[2]/div[2]/div[1]/div[1]/div/div[1]/label[1]/div') - # driver.execute_script('arguments[0].style.display="none";',hid) - # hid_1 = driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[2]/div/div[2]/div[2]/div[2]/div[1]/div[1]/div/div[1]/label[1]') - # driver.execute_script('arguments[0].style.display="none";',hid_1) - - js = "var q=document.getElementsByClassName('content-header-right')[0].scrollIntoView();" - driver.execute_script(js) - - # screen_shot = driver.save_screenshot('bin/0.png') - # print('截图') - # 点击模板 - driver.find_element_by_xpath(r'//*[@id="item"]/div/div[2]/div[3]/div[2]/div[1]/div[2]/div[1]').click() - driver.find_element_by_xpath(r'//*[@id="item"]/div/div[2]/div[3]/div[2]/div[1]/div[3]/div[1]').click() - # 点击转载 - # click = driver.find_element_by_xpath( - # '//*[@id="item"]/div/div[2]/div[3]/div[2]/div[2]/div[1]/div[1]/div[2]/div[2]/div') - # - # click.click() - - # 输入转载来源 - Input = driver.find_element_by_xpath( - '//*[@id="item"]/div/div[2]/div[3]/div[2]/div[2]/div[1]/div[1]/div[3]/div/div[1]/div/input') - Input.send_keys(link) - # 点击游戏 - # driver.find_element_by_xpath( - # '//*[@id="item"]/div/div[2]/div[3]/div[2]/div[2]/div[1]/div[2]/div[2]/div[1]/div[3]/div').click() - # 点击电子竞技 - # print('第1步') - # driver.find_element_by_xpath( - # '//*[@id="item"]/div/div[2]/div[3]/div[2]/div[2]/div[1]/div[2]/div[2]/div[1]/div[3]/div[2]/div[6]/span[1]').click() - # print('第2步') - # 稿件标题 - title = driver.find_element_by_xpath( - '//*[@id="item"]/div/div[2]/div[3]/div[2]/div[2]/div[1]/div[3]/div[2]/div[1]/div/input') - title.send_keys(Keys.CONTROL + 'a') - title.send_keys(Keys.BACKSPACE) - title.send_keys(title_) - # WebDriverWait(driver, 10).until( - # EC.presence_of_element_located((By.XPATH, '//*[@id="app"]/div[2]/div[2]/div/div[2]/div[2]/div[2]/div[1]/div[4]/div[2]/div[2]/div[2]/div[last()]/div/input'))) - # //*[@id="app"]/div[2]/div[2]/div/div[2]/div[2]/div[2]/div[1]/div[4]/div[2]/div[2]/div[2]/div[last()]/div/input - # //*[@id="item"]/div/div[2]/div[3]/div[2]/div[2]/div[1]/div[4]/div[2]/div/div[2]/div[2]/div[1]/input - # //*[@id="item"]/div/div[2]/div[3]/div[2]/div[2]/div[1]/div[4]/div[2]/div/div[2]/div[2]/div[last()]/input - # //*[@id="item"]/div/div[2]/div[3]/div[2]/div[2]/div[1]/div[4]/div[2]/div/div[2]/div[2]/div[1]/input - - # text = driver.find_element_by_xpath( - # '//*[@id="item"]/div/div[2]/div[3]/div[2]/div[2]/div[1]/div[4]/div[2]/div/div[2]/div[2]/div[1]/input') - # text.send_keys('星际争霸2') - # text.send_keys(Keys.ENTER) - # time.sleep(0.5) - # text.send_keys('第一视角') - # text.send_keys(Keys.ENTER) - # time.sleep(0.5) - # text.send_keys('职业选手') - # text.send_keys(Keys.ENTER) - # time.sleep(0.5) - # - # text.send_keys(Keys.CONTROL + 'a') - # - # text.send_keys('虚空之遗') - # text.send_keys(Keys.ENTER) - # time.sleep(0.5) - - # 输入相关游戏 - text_1 = driver.find_element_by_xpath( - '//*[@id="item"]/div/div[2]/div[3]/div[2]/div[2]/div[1]/div[5]/div/div/div[1]/div[2]/div/div/input') - text_1.send_keys('星际争霸2') - # 简介 - text_2 = driver.find_element_by_xpath( - '//*[@id="item"]/div/div[2]/div[3]/div[2]/div[2]/div[1]/div[5]/div/div/div[2]/div[2]/div[1]/textarea') - text_2.send_keys('职业选手直播第一视角录像。\n欢迎加入星际交流群一起玩耍,群号:178459358') - - driver.find_element_by_xpath('//*[@id="item"]/div/div[2]/div[3]/div[2]/div[3]/div[1]').click() - # screen_shot = driver.save_screenshot('bin/1.png') - # print('截图') - time.sleep(1) - upload_success = driver.find_element_by_xpath(r'//*[@id="item"]/div/div[3]/p[1]').text - logger.info(upload_success) - # print('稿件提交完成!') - logger.info('%s提交完成!' % title_) - remove_filelist(video_list) - except selenium.common.exceptions.NoSuchElementException: - logger.exception('发生错误') - # except selenium.common.exceptions.TimeoutException: - # logger.exception('超时') - except selenium.common.exceptions.TimeoutException: - logger.info('准备更新cookie') - # screen_shot = driver.save_screenshot('bin/1.png') - WebDriverWait(driver, 10).until( - EC.presence_of_element_located((By.XPATH, r'//*[@id="login-username"]'))) - - username = driver.find_element_by_xpath(r'//*[@id="login-username"]') - username.send_keys(user_name) - - password = driver.find_element_by_xpath('//*[@id="login-passwd"]') - password.send_keys(pass_word) - # logger.info('第四步') - cracker = slider_cracker(driver) - - cracker.crack() - - time.sleep(5) - if driver.title == '投稿 - 哔哩哔哩弹幕视频网 - ( ゜- ゜)つロ 乾杯~ - bilibili': - cookie = driver.get_cookies() - print(cookie) - with open(filename, "w") as f: - json.dump(cookie, f) - logger.info('更新cookie成功') - else: - logger.info('更新cookie失败') - except: - logger.exception('未知错误') - finally: - driver.quit() - logger.info('浏览器驱动退出') - - -def assemble_videopath(file_list): - videopath = '' - root = os.getcwd() - for i in range(len(file_list)): - file = file_list[i] - videopath += root + '/' + file + '\n' - videopath = videopath.rstrip() - return videopath - -def uploads(file_name_, url_): - - logger.info('准备上传' + file_name_[:-4]) - if os.path.isfile(file_name_): - os.rename(file_name_, file_name_[:-4] + str(time.time())[:10] + file_name_[-4:]) - file_list = get_file(file_name_) - filter_file(file_list) - file_list = get_file(file_name_) - logger.debug('获取%s文件列表' % file_name_[:-4]) - # videopath = '' - # root = os.getcwd() - # for i in range(len(file_list)): - # file = file_list[i] - # videopath += root + '/' + file + '\n' - # videopath = videopath.rstrip() - upload(video_list=file_list, link=url_, title_=file_name_[:-4]) - - # for r in file_list: - # os.remove(r) - # logger.info('删除-' + r) - - -def supplemental_upload(dict, file_name_, key_, url_, value_): - try: - for f in os.listdir(): - if file_name_[:-15] in f: - try: - # os.rename(file_name_ + '.part', file_name_[:-4] + str(time.time())[:10] + file_name_[-4:]) - os.rename(file_name_[:-4] + '.mp4' + '.part', - file_name_[:-4] + str(time.time())[:10] + '.mp4') - logger.info('%s存在已更名' % (file_name_[:-4] + '.mp4' + '.part')) - except FileNotFoundError: - # logger.info('%s不存在' % (file_name_ + '.part')) - # logger.info('%s不存在' % (file_name_[:-4] + '.mp4' + '.part')) - pass - try: - os.rename(file_name_[:-4] + '.flv' + '.part', - file_name_[:-4] + str(time.time())[:10] + '.flv') - logger.info('%s存在已更名' % (file_name_[:-4] + '.flv' + '.part')) - except FileNotFoundError: - # logger.info('%s不存在' % (file_name_[:-4] + '.flv' + '.part')) - pass - logger.info('补充上传' + key_) - uploads(file_name_, url_) - break - finally: - dict[key_] = value_ - # os._exit(0) - - -if __name__ == '__main__': - # upload('D:\\p2.flv','5','k') - # file_list = get_file('星际2soo输本虫族天梯第一视角.mp4') - # videopath = '' - # root = os.getcwd() - # for i in range(len(file_list)): - # file = file_list[i] - # videopath += root + '/' + file + '\n' - # # videopath += 'test2018年01月22日.mp4' - # print(file_list) - # print(videopath) - # upload(videopath.rstrip(), '5', 'k') - # for i in range(1): - # print(i) - pass - - diff --git a/work.py b/work.py deleted file mode 100644 index 596eda2..0000000 --- a/work.py +++ /dev/null @@ -1,177 +0,0 @@ -import logging -import signal -from logging.handlers import TimedRotatingFileHandler -from multiprocessing import Process -import time -import os -import errno -import psutil - -# log_fmt = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s' -# formatter = logging.Formatter(log_fmt) -# log_file_handler = TimedRotatingFileHandler(filename="ds_update.log", when="D", interval=1, backupCount=2) -# # log_file_handler.suffix = "%Y-%m-%d.log" -# # log_file_handler.extMatch = re.compile(r"^\d{4}-\d{2}-\d{2}.log$") -# log_file_handler.setFormatter(formatter) -# logging.basicConfig(level=logging.INFO) -# logger = logging.getLogger(__name__) -# logger.addHandler(log_file_handler) -logger = logging.getLogger('log01') - - -def wait_child(signum, frame): - logger.debug('receive SIGCHLD') - try: - while True: - # -1 表示任意子进程 - # os.WNOHANG 表示如果没有可用的需要 wait 退出状态的子进程,立即返回不阻塞 - cpid, status = os.waitpid(-1, os.WNOHANG) - if cpid == 0: - logger.debug('no child process was immediately available') - break - exitcode = status >> 8 - logger.debug('child process %s exit with exitcode %s', cpid, exitcode) - except OSError as e: - if e.errno == errno.ECHILD: - logger.error('current process has no existing unwaited-for child processes.') - else: - raise - logger.debug('handle SIGCHLD end') - - -def kill_child_processes(parent_pid, file_name_, sig=signal.SIGINT): - file_name_ = file_name_ + '.part' - last_file_size = 0.0 - while True: - time.sleep(15) - if os.path.isfile(file_name_): - file_size = os.path.getsize(file_name_) / 1024 / 1024 / 1024 - file_sizes = os.path.getsize(file_name_) - if float(file_sizes) == last_file_size: - try: - parent = psutil.Process(parent_pid) - except psutil.NoSuchProcess: - return - children = parent.children(recursive=True) - if len(children) == 0: - parent.send_signal(sig) - logger.info('下载卡死pandaTV' + file_name_) - else: - for process in children: - # print(process) - process.send_signal(sig) - logger.info('下载卡死' + file_name_) - # time.sleep(1) - if os.path.isfile(file_name_): - logger.info('卡死下载进程可能未成功退出') - continue - else: - logger.info('卡死下载进程成功退出') - break - - last_file_size = file_sizes - - if float(file_size) >= 3.5: - try: - parent = psutil.Process(parent_pid) - except psutil.NoSuchProcess: - return - children = parent.children(recursive=True) - if len(children) == 0: - parent.send_signal(sig) - logger.info('分段下载pandatv' + file_name_) - else: - for process in children: - # print(process) - process.send_signal(sig) - print('分段下载twitch') - logger.info('分段下载twitch' + file_name_) - break - else: - logger.info('退出监控进程') - return - # os._exit(0) - logger.info('退出监控进程') - - -def monitoring(q): - signal.signal(signal.SIGCHLD, wait_child) - while True: - # print('开始监测') - pid, file_name = q.get() - time.sleep(5) - logger.info('获取到{0},{1}'.format(pid,file_name)) - p = Process(target=kill_child_processes, args=(pid, file_name)) - p.start() - - -def new_hook(t, v, tb): - logger.error("Uncaught exception:", exc_info=(t, v, tb)) - - -class SafeRotatingFileHandler(TimedRotatingFileHandler): - def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False): - TimedRotatingFileHandler.__init__(self, filename, when, interval, backupCount, encoding, delay, utc) - - """ - Override doRollover - lines commanded by "##" is changed by cc - """ - - def doRollover(self): - """ - do a rollover; in this case, a date/time stamp is appended to the filename - when the rollover happens. However, you want the file to be named for the - start of the interval, not the current time. If there is a backup count, - then we have to get a list of matching filenames, sort them and remove - the one with the oldest suffix. - - Override, 1. if dfn not exist then do rename - 2. _open with "a" model - """ - if self.stream: - self.stream.close() - self.stream = None - # get the time that this sequence started at and make it a TimeTuple - currentTime = int(time.time()) - dstNow = time.localtime(currentTime)[-1] - t = self.rolloverAt - self.interval - if self.utc: - timeTuple = time.gmtime(t) - else: - timeTuple = time.localtime(t) - dstThen = timeTuple[-1] - if dstNow != dstThen: - if dstNow: - addend = 3600 - else: - addend = -3600 - timeTuple = time.localtime(t + addend) - dfn = self.baseFilename + "." + time.strftime(self.suffix, timeTuple) - ## if os.path.exists(dfn): - ## os.remove(dfn) - - # Issue 18940: A file may not have been created if delay is True. - ## if os.path.exists(self.baseFilename): - if not os.path.exists(dfn) and os.path.exists(self.baseFilename): - os.rename(self.baseFilename, dfn) - if self.backupCount > 0: - for s in self.getFilesToDelete(): - os.remove(s) - if not self.delay: - self.mode = "a" - self.stream = self._open() - newRolloverAt = self.computeRollover(currentTime) - while newRolloverAt <= currentTime: - newRolloverAt = newRolloverAt + self.interval - # If DST changes and midnight or weekly rollover, adjust for this. - if (self.when == 'MIDNIGHT' or self.when.startswith('W')) and not self.utc: - dstAtRollover = time.localtime(newRolloverAt)[-1] - if dstNow != dstAtRollover: - if not dstNow: # DST kicks in before next rollover, so we need to deduct an hour - addend = -3600 - else: # DST bows out before next rollover, so we need to add an hour - addend = 3600 - newRolloverAt += addend - self.rolloverAt = newRolloverAt -