def get_not_used_medias_from_users(my_bot, users=None, users_path=USERNAME_DATABASE): if not users: users = read_list_from_file(users_path) users = map(str, users) total_medias = [] for user in users: medias = my_bot.get_user_medias(user, filtration=False) medias = [media for media in medias if not exists_in_posted_medias(media)] total_medias.extend(medias) return total_medias
def get_not_used_medias_from_users(bot, users=None, users_path=USERNAME_DATABASE): if not users: users = read_list_from_file(users_path) users = map(str, users) total_medias = [] for user in users: if user[0] == '#': hashtag = user[1:] medias = bot.get_hashtag_medias(hashtag) elif user == settings['login']: medias = bot.get_user_tags_medias(bot.convert_to_user_id(user)) else: medias = bot.get_user_medias(user, filtration=False) medias = [ media for media in medias if not exists_in_posted_medias(media) ] total_medias.extend(medias) return total_medias
def update_posted_medias(new_media_id, path='posted_medias.txt'): medias = read_list_from_file(path) medias.append(str(new_media_id)) with open(path, 'w') as file: file.writelines('\n'.join(medias)) return True
def exists_in_posted_medias(new_media_id, path='posted_medias.txt'): medias = read_list_from_file(path) return new_media_id in medias
def read_list_from_file(self, file_path): return read_list_from_file(file_path)
def update_posted_medias(new_media_id, path="posted_medias.txt"): medias = read_list_from_file(path) medias.append(str(new_media_id)) with open(path, "w") as file: file.writelines("\n".join(medias)) return True
def exists_in_posted_medias(new_media_id, path=POSTED_MEDIAS): medias = read_list_from_file(path) return str(new_media_id) in medias
photo_path = my_bot.download_photo(new_media_id, description=True) if not photo_path: return False with open(photo_path[:-3] + 'txt', 'r') as f: text = ''.join(f.readlines()) if my_bot.upload_photo(photo_path, text): update_posted_medias(new_media_id, path) my_bot.logger.info('Media_id {0} is saved in {1}'.format( new_media_id, path)) parser = argparse.ArgumentParser(add_help=True) parser.add_argument('-u', type=str, help="username") parser.add_argument('-p', type=str, help="password") parser.add_argument('-proxy', type=str, help="proxy") parser.add_argument('-file', type=str, help="users filename") parser.add_argument('-amount', type=int, help="amount", default=1) parser.add_argument('users', type=str, nargs='*', help='users') args = parser.parse_args() bot = Bot() bot.login() users = None if args.users: users = args.users elif args.file: users = read_list_from_file(args.file) repost_best_photos(bot, users, args.amount)