def upload(self, file_list): video = Data() with BiliBili(video) as bili: self.login(bili) for file in file_list: video_part = bili.upload_file(file) # 上传视频 video.videos.append(video_part) # 添加已经上传的视频 video.title = self.data["format_title"] video.desc = '''这个自动录制上传的小程序开源在Github:http://t.cn/RgapTpf(或者在Github搜索ForgQi) 交流群:837362626''' video.source = self.data["url"] # 添加转载地址说明 # 设置视频分区,默认为174 生活,其他分区 tid = engine.config['streamers'][self.principal].get('tid') if tid: video.tid = tid tags = engine.config['streamers'][self.principal].get( 'tags', ['星际争霸2', '电子竞技']) if tags: video.set_tag(tags) img_path = engine.config['streamers'][self.principal].get( 'cover_path') if img_path: video.cover = bili.cover_up(img_path).replace('http:', '') ret = bili.submit() # 提交视频 logger.info(f"upload_success:{ret}") self.remove_filelist(file_list)
def run(self): try: logger.info('开始下载%s:%s' % (self.__class__.__name__, self.fname)) self.start() except: logger.exception("Uncaught exception:") finally: logger.info('退出下载')
def run(self): if not self.check_stream(): return False file_name = f'{self.file_name}.{self.suffix}' retval = self.download(file_name) logger.info(f'{retval}part: {file_name}') self.rename(file_name) return retval
def download(self): retval = super().download() if retval == 0: if self.check_stream(): logger.info('实际未下载完成' + self.fname) self.rename(self.ydl_opts['outtmpl']) logger.info('准备递归下载') self.run() return 0 return retval
def rename(file_name): try: os.rename(file_name + '.part', file_name) logger.debug('更名{0}为{1}'.format(file_name + '.part', file_name)) except FileNotFoundError: logger.info('FileNotFoundError:' + file_name) except FileExistsError: os.rename(file_name + '.part', file_name) logger.info('FileExistsError:更名{0}为{1}'.format( file_name + '.part', file_name))
def start(self): i = 0 try: logger.info('开始下载%s:%s' % (self.__class__.__name__, self.fname)) while i < 30: ret = self.run() if ret is False: return elif ret == 1: time.sleep(45) i += 1 except: logger.exception("Uncaught exception:") finally: logger.info(f'退出下载{i}: {self.fname}')
def start(self): file_name = self.file_name if self.check_stream(): file_name += "." + self.suffix pid = os.getpid() monitor = Monitoring(pid, file_name) self.flag = monitor.flag monitor.start() retval = self.download(file_name) self.rename(file_name) monitor.stop() if retval != 0: logger.debug('准备递归下载') self.start() else: logger.info('下载完成' + self.fname)
def __init__(self, urls): BatchCheckBase.__init__(self, pattern_id=VALID_URL_BASE, urls=urls) self.use_id = {} if self.usr_list: login = requests.get(_API_USER, headers=headers, params={'login': self.usr_list}, timeout=5) login.close() else: logger.debug('无twitch主播') return try: for pair in login.json()['data']: self.use_id[pair['id']] = pair['login'] except KeyError: logger.info(login.json()) return
def add_videos(self, videopath): formate_title = self.data["format_title"] WebDriverWait(self.driver, 20).until( ec.presence_of_element_located((By.NAME, 'buploader'))) upload = self.driver.find_element_by_name('buploader') # logger.info(driver.title) upload.send_keys(videopath) # send_keys logger.info('开始上传' + formate_title) time.sleep(2) button = r'//*[@class="new-feature-guide-v2-container"]/div/div/div/div/div[1]' if self.is_element_exist(self.driver, button): sb = self.driver.find_element_by_xpath(button) sb.click() sb.click() sb.click() logger.debug('点击') while True: try: info = self.driver.find_elements_by_class_name( r'item-upload-info') for t in info: if t.text != '': print(t.text) time.sleep(10) text = self.driver.find_elements_by_xpath( r'//*[@class="item-upload-info"]/span') aggregate = set() for s in text: if s.text != '': aggregate.add(s.text) print(s.text) if len(aggregate) == 1 and ('Upload complete' in aggregate or '上传完成' in aggregate): break except selenium.common.exceptions.StaleElementReferenceException: logger.exception( "selenium.common.exceptions.StaleElementReferenceException" ) logger.info('上传%s个数%s' % (formate_title, len(info)))
def login(self, filename): logger.info('准备更新cookie') # screen_shot = driver.save_screenshot('bin/1.png') WebDriverWait(self.driver, 10).until( ec.presence_of_element_located( (By.XPATH, r'//*[@id="login-username"]'))) username = self.driver.find_element_by_xpath( r'//*[@id="login-username"]') username.send_keys(engine.user_name) password = self.driver.find_element_by_xpath('//*[@id="login-passwd"]') password.send_keys(engine.pass_word) self.driver.find_element_by_class_name("btn-login").click() # logger.info('第四步') # try: cracker = slider_cracker(self.driver) cracker.crack() # except: # logger.exception('出错') time.sleep(5) if self.driver.title == '投稿 - 哔哩哔哩弹幕视频网 - ( ゜- ゜)つロ 乾杯~ - bilibili': cookie = self.driver.get_cookies() print(cookie) with open(filename, "w") as f: json.dump(cookie, f) logger.info('更新cookie成功') else: logger.info('更新cookie失败')
def upload(self, file_list): filename = self.persistence_path videopath = self.assemble_videopath(file_list) # service_log_path = "{}/chromedriver.log".format('/home') options = webdriver.ChromeOptions() options.add_argument('headless') self.driver = webdriver.Chrome( executable_path=engine.chromedriver_path, chrome_options=options) # service_log_path=service_log_path) try: self.driver.get("https://www.bilibili.com") # driver.delete_all_cookies() if os.path.isfile(filename): with open(filename) as f: new_cookie = json.load(f) for cookie in new_cookie: if isinstance(cookie.get("expiry"), float): cookie["expiry"] = int(cookie["expiry"]) self.driver.add_cookie(cookie) self.driver.get("https://member.bilibili.com/video/upload.html") # print(driver.title) self.add_videos(videopath) # js = "var q=document.getElementsByClassName('content-header-right')[0].scrollIntoView();" # driver.execute_script(js) cookie = self.driver.get_cookies() with open(filename, "w") as f: json.dump(cookie, f) self.add_information() self.driver.find_element_by_xpath( '//*[@class="upload-v2-container"]/div[2]/div[3]/div[5]/span[1]' ).click() # screen_shot = driver.save_screenshot('bin/1.png') # print('截图') time.sleep(3) upload_success = self.driver.find_element_by_xpath( r'//*[@id="app"]/div/div[3]/h3').text if upload_success == '': self.driver.save_screenshot('err.png') logger.info('稿件提交失败,截图记录') return else: logger.info(upload_success) # logger.info('%s提交完成!' % title_) self.remove_filelist(file_list) except selenium.common.exceptions.NoSuchElementException: logger.exception('发生错误') # except selenium.common.exceptions.TimeoutException: # logger.exception('超时') except selenium.common.exceptions.TimeoutException: self.login(filename) finally: self.driver.quit() logger.info('浏览器驱动退出')