def complete(self): complete_data = { "drive_id": self.drive_id, "file_id": self.file_id, "upload_id": self.upload_id } complete_post = requests.post( 'https://api.aliyundrive.com/v2/file/complete', json.dumps(complete_data), headers=self.headers, verify=False) requests_post_json = complete_post.json() self.check_auth(requests_post_json, self.complete) s = time.time() - self.start_time # print(requests_post_json) # print(complete_data) if 'file_id' in requests_post_json: print_success('【{filename}】上传成功!消耗{s}秒'.format( filename=self.filename, s=s)) return True else: print_warn('【{filename}】上传失败!消耗{s}秒'.format(filename=self.filename, s=s)) return False
def complete(self, file_id, upload_id): complete_data = { "drive_id": self.drive_id, "file_id": file_id, "upload_id": upload_id } complete_post = requests.post( 'https://api.aliyundrive.com/v2/file/complete', json.dumps(complete_data), headers=self.headers, verify=False ) complete_post_json = complete_post.json() if complete_post_json.get('code') == 'AccessTokenInvalid': print_info('AccessToken已失效,尝试刷新AccessToken中') if self.token_refresh(): print_info('AccessToken刷新成功,返回创建上传任务') self.complete(file_id, upload_id) return print_error('无法刷新AccessToken,准备退出') exit() s = time.time() - self.start_time if 'file_id' in complete_post_json: print_success('【{filename}】上传成功!消耗{s}秒'.format(filename=self.filename, s=s)) return True else: print_warn('【{filename}】上传失败!消耗{s}秒'.format(filename=self.filename, s=s)) return False
def upload_file(path, filepath): drive = AliyunDrive(DATA['DRIVE_ID'], DATA['ROOT_PATH'], DATA['CHUNK_SIZE']) # 刷新token drive.token_refresh() realpath = path + filepath drive.load_file(filepath, realpath) # 创建目录 LOCK.acquire() try: parent_folder_id = drive.get_parent_folder_id(filepath) finally: LOCK.release() # 断点续传 if DATA['RESUME'] and drive.filepath_hash in DATA['tasks']: c_task = DATA['tasks'][drive.filepath_hash] if 0 not in ( c_task['drive_id'], c_task['file_id'], c_task['upload_id'], c_task['part_number'], c_task['chunk_size'], ): drive.drive_id = c_task['drive_id'] drive.file_id = c_task['file_id'] drive.upload_id = c_task['upload_id'] drive.part_number = c_task['part_number'] drive.chunk_size = c_task['chunk_size'] # 获取上传地址 drive.part_upload_url_list = drive.get_upload_url() # 上传 drive.upload() # 提交 if drive.complete(): return drive.filepath_hash return False # 创建上传 create_post_json = drive.create(parent_folder_id) if 'rapid_upload' in create_post_json and create_post_json['rapid_upload']: print_success('【{filename}】秒传成功!消耗{s}秒'.format(filename=drive.filename, s=time.time() - drive.start_time)) return drive.filepath_hash # 上传 drive.upload() # 提交 if drive.complete(): return drive.filepath_hash return False
def notice_aria2(pic_list, username, caption): if len(pic_list) <= 0: return path = ARIA2_PATH.rstrip('/') + '/{username}/{caption}/'.format( username=username, caption=caption) for pic in pic_list: data = { "jsonrpc": "2.0", "method": "aria2.addUri", "id": "lixiaoen", "params": ["token:%s" % ARIA2_TOKEN, [pic], { "dir": path }] } requests.post(ARIA2_JSONRPC, data=json.dumps(data)) print_success('发送下载任务带aria2服务器成功')
def upload_file(path, filepath): realpath = path + filepath drive.load_file(filepath, realpath) # 创建目录 parent_folder_id = get_parent_folder_id(ROOT_PATH, filepath) # 创建上传 create_post_json = drive.create(parent_folder_id) if 'rapid_upload' in create_post_json and create_post_json['rapid_upload']: print_success('【{filename}】秒传成功!消耗{s}秒'.format(filename=drive.filename, s=time.time() - drive.start_time)) return True upload_url = create_post_json['part_info_list'][0]['upload_url'] file_id = create_post_json['file_id'] upload_id = create_post_json['upload_id'] # 上传 drive.upload(upload_url) # 提交 return drive.complete(file_id, upload_id)
def IDS(state, goal, limit=100, stop=False): global global_counter all_neighbors = [] for motif in state: all_neighbors.append(create_neighbor_motifs(motif)) all_neighbors = [item for sublist in all_neighbors for item in sublist] for motif in all_neighbors: print('Checking ' + motif + '...', end='\r') if common.is_motif_valid(motif) == False: all_neighbors.remove(motif) if motif not in destoryers: destoryers.append(motif) elif len(motif) == limit: answers.append(motif) common.print_success(motif + ' is a valid motif.') global_counter += 1 if len(all_neighbors[0]) < limit: IDS(all_neighbors, goal, limit, True) return
def get_album_pic(uid, album, username): global photo_list print_info('开始获取相册【{album_id}】'.format(album_id=album['album_id'])) page = 1 count = 100 pic_list = [] while True: print_info('正在获取第{page}页'.format(page=page)) temp_url = 'https://photo.weibo.com/photos/get_all?uid={uid}&album_id={album_id}&count={count}&page={page}&type={type}&__rnd={time}' url = temp_url.format( uid=uid, album_id=album['album_id'], count=count, page=page, type=album['type'], time=time.time(), ) requests_get = requests.get(url, headers=headers) try: photo_list = requests_get.json()['data']['photo_list'] except Exception as e: print_error('登录信息无效,请更新!') raise Exception(e) if len(photo_list) <= 0: break for photo in photo_list: pic_url = (PIC_FORMAT).format(pic_id=photo['pic_pid']) pic_list.append(pic_url) print_success('第{page}页获取完毕,共{count}条'.format(page=page, count=len(photo_list))) page += 1 print_success('相册【{album_id}】获取完毕'.format(album_id=album['album_id'])) # 保存 save_album(pic_list, username, album) # 通知aria2 if ARIA2_ENABLE: notice_aria2(pic_list, username, album['caption'])
def start_hill_climbing(): answers = [] for i in range(0, maximum_restarts): motif, fitness, is_valid = check_motif_fitness_score_and_validation( common.generate_random_potential_motif()) if is_valid == True: if not motif in answers: print('\n') answers.append(motif) common.print_success(motif + ' is a discovered motif with score of ' + map_fitness_to_number(fitness)) else: common.print_warning(motif + ' is not a motif because of ' + map_fitness_to_number(fitness) + ' fitness score.') if len(answers) > 0: common.print_success( str(len(answers)) + ' motif(s) with length of ' + str(common.input_motif_length) + ' motifs were discovered.') else: common.print_error('No motifs with length of ' + str(common.input_motif_length) + ' were found.')
IDS(starter_motifs, desired_hamming_distance, length) def create_neighbor_motifs(motif): neighbor_motifs = [] if motif is None: motif = '' for i in range(0, len(common.motif_characters)): should_pass = True neighbor_motif = motif + common.motif_characters[i] if any(destroyer in motif for destroyer in destoryers): should_pass = False if should_pass == True: neighbor_motifs.append(neighbor_motif) return neighbor_motifs def handled_previously(motif): return motif in created_motifs_by_far st = common.start_time() discover_motifs(common.input_motif_length, common.input_hamming_distance) if global_counter > 0: common.print_success(str(global_counter) + ' motif with the length of ' + str(common.input_motif_length) + ' found.') else: common.print_error('No motifs with length of ' + str(common.input_motif_length) + ' were found.') common.end_time(st)
# 保存 save_album(pic_list, username, album) # 通知aria2 if ARIA2_ENABLE: notice_aria2(pic_list, username, album['caption']) def get_username(uid): try: requests_get = requests.get('https://photo.weibo.com/%s/albums?rd=1' % uid, headers=headers) except: return get_username(uid) try: get_text = requests_get.text username = re.findall(r"<title>(.+?)的专辑\s-\s微相册<\/title>", get_text)[0] return username except Exception as e: print_error('登录信息无效,请更新!') raise Exception(e) for uid in UID_LIST: username = get_username(uid) print_info('正在获取用户【{uid}】的相册'.format(uid=uid)) album_list = get_albums(uid) print_success('获取用户【{uid}】的相册成功'.format(uid=uid)) for album in album_list: get_album_pic(uid, album, username)
if most_close_to_answer is None or best_score > most_close_to_answer_score: most_close_to_answer, most_close_to_answer_score = best_motif, best_score print(best_motif + ' is best motif with fitness of ' + str(get_fitness_score_percentage(best_motif))) new_population.append(best_motif) population = list(set(new_population)) if time.clock() - ga_time > enough_time: done = True for motif in population: if common.is_motif_valid(motif) == True: done = True answer = motif break if answer is not None: return answer else: return {'best_answer': most_close_to_answer, 'best_score': most_close_to_answer_score} st = common.start_time() answer = implement_genetic_algorithm(50) if str(answer) == answer: common.print_success('\n' + answer + ' is a valid motif.') else: common.print_warning('Best found motif is ' + answer['best_answer'] + ' with fitness score of ' + str(answer['best_score'])) common.end_time(st)