def poster(token, owner_id, path_folder, time_of_post, format_, captcha_sid_, captcha_key_): file_name = list_files_folder(path_folder, format_)[0] message = take_name_from_excel("VK_list.xlsx", file_name) message = message.strip() pic_path = path_folder + file_name try: session = vk.AuthSession(access_token=token) vk_api = vk.API(session) result = vk_api.docs.getWallUploadServer(v=5.103)['upload_url'] img = {'file': (pic_path, open(r'' + pic_path, 'rb'))} upload_url = result with open(r'' + pic_path, 'rb') as f: img = {'file': (pic_path, f)} with requests.post(upload_url, files=img) as r: # result = json.loads(r.text) # CAPTCHA ######################################################### ## captcha_sid=788746128668,captcha_key="vaav", # result = vk_api.docs.save(file=result['file'], v=5.102) result = vk_api.docs.save(file=result['file'], captcha_sid=captcha_sid_, captcha_key=captcha_key_, v=5.101) ######################################################### attachments = ("doc" + str(result['doc']["owner_id"]) + "_" + str(result['doc']["id"])) upload = vk_api.wall.post(message=message, publish_date=time_of_post, owner_id=-1 * (owner_id), from_group=1, attachments=attachments, v=5.103) return (upload, pic_path, message) except Exception as error: print("Error in posrer function") Logger(str(error) + " " + pic_path) return (None, pic_path, error)
def download(url, direction): dir = os.path.abspath(os.curdir) if url[-3:] == "jpg": file_name = url.split("/")[-1] file_name = file_name[:-3] + "jpg" else: file_name = url.split("/")[-1][0:21] + ".gif" print("Downloading... ", dir + "\{}\\".format(direction) + file_name) try: # open in binary mode with open(dir + "\{}\\".format(direction) + file_name, "wb") as file: response = requests.get(url) file.write(response.content) file.close() except Exception as e: Logger(e)
def vk_post(format_, path_folder, min_shift, token_post, owner_id, captcha_sid_, captcha_key_): time_zero = take_time(token_post, owner_id) if time_zero == None: print("\nServer don't response") elif len(list_files_folder(path_folder, format_)) < 1: print("\nOutput objects folder is empty") else: print("\nStart posting objects {}".format(format_)) time_of_post = time_zero + (min_shift * 60) i = 1 while len(list_files_folder(path_folder, format_)) > 0: poster_result = poster(token_post, owner_id, path_folder, time_of_post, format_, captcha_sid_, captcha_key_) if poster_result[0] != None: number_of_posts = get_posts_number(token_post, owner_id) print( "\n\npost uploaded {}, number of posts <<< {} >>>\n". format(i, number_of_posts), poster_result[0:1], datetime.datetime.fromtimestamp(time_of_post)) with open(poster_result[1]) as existing_file: existing_file.close() os.remove(poster_result[1]) print("{} removed\n".format( poster_result[1].split("\\")[1])) if number_of_posts != 150: time_of_post = time_of_post + (min_shift * 60) time.sleep(random.randint(1, 3) * 60) i = i + 1 else: Logger( "Access to adding post denied: cannot schedule more than 150 posts." ) time.sleep(43200) else: error = poster_result[2] Logger(error) if "14. Captcha needed." in str(error): link_captcha = error.captcha_img captcha_sid_ = error.captcha_sid captcha_key_ = captcha_solve(link_captcha) elif "214. Access to adding post denied: can only schedule 25 posts on a day" in str( error): catcha25_timer = timer( datetime.datetime.fromtimestamp( take_time(token_post, owner_id))) time_of_post = time_of_post + catcha25_timer # print(datetime.datetime.fromtimestamp(time_of_post)) time.sleep(random.randint(1, 3) * 60) elif "Access to adding post denied: cannot schedule more than 150 posts." in str( error): Logger(error) time.sleep(43200) elif 'charmap' in str(error): # file_name_del = list_files_folder(path_folder, format_)[0] with open(poster_result[1]) as existing_file: # existing_file.close() os.remove(existing_file) else: break
ads = d["ads"] token = d["token_parser"] list_of = [] path_folder = direction + "\\" min_shift = d["min_shift"] token_post = d["token_post"] owner_id = d["owner_id"] #test_line ################################################### a = input( "Select '0' for PARSER,\nselect '1' for POST,\nselect '2' for Parser&Post: \n" ) Logger(d) if a == "0": vk_parser(format_, direction, group_id_list, date_start, date_end, comments, likes, ads, token, list_of) input("\nParsing was done") elif a == "1": vk_post(format_, path_folder, min_shift, token_post, owner_id, captcha_sid_, captcha_key_) else: vk_parser(format_, direction, group_id_list, date_start, date_end, comments, likes, ads, token, list_of) vk_post(format_, path_folder, min_shift, token_post, owner_id,
comments = d["comments"] likes = d["likes"] ads = d["ads"] token = d["token_parser"] list_of = [] path_folder = direction + "\\" min_shift = d["min_shift"] token_post = d["token_post"] owner_id = d["owner_id"] ################################################### a = input( "Select '0' for PARSER,\nselect '1' for POST,\nselect '2' for Parser&Post: \n" ) Logger(d) try: if a == "0": vk_parser( format_, direction, group_id_list, date_start, date_end, comments, likes, ads, token, list_of,