def get_user_video(self, user_id): _first = True count_of_loaded_photo = 0 next_max_id = None while next_max_id or _first: try: _first = False results = self.API.user_feed(user_id=user_id, max_id=next_max_id) for item in results.get('items', []): try: id = item["id"] date = item["caption"]["created_at"] photo_url = item['video_versions'][0]['url'] print('I find video))') except: continue if self._save_photo(photo_url, id, date, extension='mp4'): print('I load video))') db_utils.insert_photo({ "source_id": id, "source": self.Source, "date": date }) count_of_loaded_photo += 1 if count_of_loaded_photo % db_utils.COMMIT_COUNT == 0: db_utils.commit() next_max_id = results.get('next_max_id') except: utils.print_message(traceback.format_exc()) db_utils.commit() return count_of_loaded_photo
def send_to_channel(photo_id, chat_id='-1001189643268'): fn = db_utils.get_fn(photo_id) try: with open(fn, 'rb') as f: caption = ' '.join(random.sample(tags, 2)) BOT.send_photo(chat_id=chat_id, photo=f, caption=caption) db_utils.set_used(photo_id) db_utils.commit() except: utils.print_message('Cannot send file') return False return True # 510929420
def get_timeline(self, K): """ Get K photo from feed timeline :param - K: Count of post in timeline """ result = [] _first = True count_of_loaded_photo = 0 next_max_id = None counter = K while (next_max_id or _first) and counter > 0: try: _first = False results = self.API.feed_timeline(max_id=next_max_id) for item in results.get('feed_items', []): try: id = item["media_or_ad"]["id"] date = item["media_or_ad"]["caption"]["created_at"] photo_url = item["media_or_ad"]["image_versions2"][ "candidates"][0]["url"] except: continue if counter <= 0: return counter -= 1 if self._save_photo(photo_url, id, date): db_utils.insert_photo({ "source_id": id, "source": self.Source, "date": date }) count_of_loaded_photo += 1 if count_of_loaded_photo % db_utils.COMMIT_COUNT == 0: db_utils.commit() next_max_id = results.get('next_max_id') except: utils.print_message(traceback.format_exc()) db_utils.commit() return count_of_loaded_photo
def get_user_photo(self, user_id): """ Get all user photo :param - user_id: Account id in instagram """ _first = True count_of_loaded_photo = 0 next_max_id = None while next_max_id or _first: try: _first = False results = self.API.user_feed(user_id=user_id, max_id=next_max_id) for item in results.get('items', []): try: id = item["id"] date = item["caption"]["created_at"] photo_url = item["image_versions2"]["candidates"][0][ "url"] except: continue if self._save_photo(photo_url, id, date): db_utils.insert_photo({ "source_id": id, "source": self.Source, "date": date }) count_of_loaded_photo += 1 if count_of_loaded_photo % db_utils.COMMIT_COUNT == 0: db_utils.commit() next_max_id = results.get('next_max_id') except: utils.print_message(traceback.format_exc()) db_utils.commit() return count_of_loaded_photo
def main(): if PARAMS['command'] is None: utils.print_message("Error: Empty command. Exit.") return login, password = utils.read_login_pwd() insta = instagram.Instagram(login, password) #statistic inserted_posts = 0 successfuly_posted = 0 new_accounts = 0 posted_comments = 0 utils.print_message("Command: '{}'".format(PARAMS['command'])) if PARAMS['command'].lower() == "insert_posts": for post in PARAMS["posts"]: utils.print_message("Inserted {} of {} rows".format( inserted_posts, len(PARAMS["posts"])), 2, end="\r") try: inserted_posts += 1 if db_utils.add_post(post[0], post[1]) else 0 except: utils.print_message(traceback.format_exc()) pass utils.print_message("Inserted rows {}. In control file {} {}".format( inserted_posts, len(PARAMS["posts"]), " " * 20)) db_utils.commit() elif PARAMS['command'].lower() == "posting": utils.print_message("Processing...") posts = db_utils.get_not_posted_posts(PARAMS["posts_count"]) for post_num, post in enumerate(posts): id, fname, descr = post try: if insta.post_photo(fname, descr): db_utils.set_posted(id) successfuly_posted += 1 if post_num < len(posts) - 1: for seconds in range(PARAMS['timeout']): utils.print_message( "Sleep {} seconds...".format(PARAMS['timeout'] - seconds), 2, end="\r") time.sleep(1) utils.print_message(" " * 25, 2, end="\r") except: utils.print_message(traceback.format_exc()) pass db_utils.commit() utils.print_message("Total new photos posted: {} {}".format( successfuly_posted, " " * 25)) elif PARAMS['command'].lower() == "username2id": utils.print_message("Processing...") for user_num, username in enumerate(PARAMS["usernames"]): utils.print_message( "Get info about account #{} '{}' ({} total)...".format( user_num + 1, username, len(PARAMS["usernames"])), 2, end="\r") try: user_id = insta.API.username_info(username)["user"]["pk"] utils.print_message( "Insert new account '{}' in database... {}".format( username, " " * 10), 2, end="\r") new_accounts += 1 if db_utils.add_user(username, user_id) else 0 utils.print_message( "Info about account '{}' inserted in database{}".format( username, " " * 25), 2) except: utils.print_message(traceback.format_exc()) utils.print_message( "Hasn't info about account '{}' {}".format( username, " " * 25), 2) utils.print_message("Inserted new accounts in database: {} {}".format( new_accounts, " " * 25)) db_utils.commit() elif PARAMS['command'].lower() == "spam": if PARAMS['mode'].lower() in ("users", "all"): utils.print_message("Spam comments to users posts...") posted_comments += insta.spam_in_users_comments([ info[2] for info in db_utils.select_users(PARAMS['users_count']) ], PARAMS['text'], PARAMS['comments_count']) if PARAMS['mode'].lower() in ("feedline", "all"): utils.print_message("Spam comments to feedline posts...") posted_comments += insta.spam_in_timeline_comments( PARAMS['text'], PARAMS['comments_count']) utils.print_message("Posted spam comments: {} {}".format( posted_comments, " " * 25))