def get_post_from_url(post_url): with SetupBrowserEnvironment() as browser: now_datetime = arrow.now('US/Pacific') try: instagram_post = InstagramPost(browser, post_url) instagram_post.extract_post_info() post_stats = { 'username': instagram_post.username, 'post_url': post_url, 'likes': instagram_post.likes, 'views': instagram_post.views, 'caption': instagram_post.caption, 'checked_date': now_datetime.format('MM-DD-YYYY'), 'checked_time': now_datetime.format('hh:mm:ss A'), 'still_up': True } except NoInstaPostPageFound: post_stats = { 'post_url': post_url, 'checked_date': now_datetime.format('MM-DD-YYYY'), 'checked_time': now_datetime.format('hh:mm:ss A'), 'still_up': False } return [post_stats]
def extract_user_posts(browser, num_of_posts_to_do): links2, preview_imgs = get_num_posts(browser, num_of_posts_to_do) post_infos = [] counter = 1 # into user_commented_total_list I will add all username links who commented on any post of this user user_commented_total_list = [] for postlink in links2: InstaLogger.logger().info(f"\n {counter} / {len(links2)}") counter = counter + 1 try: instagram_post = InstagramPost(browser, postlink) instagram_post.extract_post_info() location = { 'location_url': instagram_post.location_url, 'location_name': instagram_post.location_name, 'location_id': instagram_post.location_id, 'latitude': instagram_post.lat, 'longitude': instagram_post.lng, } post_infos.append({ 'caption': instagram_post.caption, 'location': location, 'imgs': instagram_post.imgs, 'imgdesc': instagram_post.imgdesc, 'preview_img': preview_imgs.get(instagram_post.postlink, None), 'date': instagram_post.date, 'tags': instagram_post.tags, 'likes': { 'count': instagram_post.likes, 'list': instagram_post.user_liked_list }, 'views': instagram_post.views, 'url': instagram_post.postlink, 'comments': { 'count': instagram_post.commentscount, 'list': instagram_post.user_comments }, 'mentions': instagram_post.mentions }) user_commented_total_list = user_commented_total_list + instagram_post.user_commented_list except NoSuchElementException as err: InstaLogger.logger().error("Could not get information from post: " + instagram_post.postlink) InstaLogger.logger().error(err) return post_infos, user_commented_total_list
def extract_user_posts(browser, ig_user, num_of_posts_to_do): links2, preview_imgs = get_num_posts(browser, ig_user, num_of_posts_to_do) post_infos = [] counter = 1 # into user_commented_total_list I will add all username links who commented on any post of this user user_commented_total_list = [] with open(ig_user.username + "/" + ig_user.username + "_posts.txt", "r") as f: link_temp = [item.strip() for item in f.readlines()] for postlink in links2: print(postlink) if([item.strip() for item in link_temp if postlink in item][0].startswith("#")): print("Post: ", postlink.split("/")[-2], " already fetched.") continue InstaLogger.logger().info(f"\n {counter} / {len(links2)}") counter = counter + 1 try: instagram_post = InstagramPost(browser, postlink) instagram_post.extract_post_info() location = { 'location_url': instagram_post.location_url, 'location_name': instagram_post.location_name, 'location_id': instagram_post.location_id, 'latitude': instagram_post.lat, 'longitude': instagram_post.lng, } post_infos.append({ 'caption': instagram_post.caption, #instagram_post.get('caption', 'None'), 'location': location, 'imgs': instagram_post.imgs, 'imgdesc': instagram_post.imgdesc, 'preview_img': preview_imgs.get(instagram_post.postlink, None), 'date': instagram_post.date, 'tags': instagram_post.tags, 'likes': { 'count': instagram_post.likes, 'list': instagram_post.user_liked_list }, 'views': instagram_post.views, 'url': instagram_post.postlink, 'comments': { 'count': instagram_post.commentscount, 'list': instagram_post.user_comments }, 'mentions': instagram_post.mentions }) user_commented_total_list = user_commented_total_list + instagram_post.user_commented_list except NoSuchElementException as err: InstaLogger.logger().error("Could not get information from post: " + instagram_post.postlink) InstaLogger.logger().error(err) #RISHABH print("Post: ", postlink.split("/")[-2], " fetching complete.") # save # in the file link_temp[link_temp.index(postlink)] = "# " + postlink with open(ig_user.username + "/" + ig_user.username + "_posts.txt", "w") as f: f.write("\n".join(link_temp)) return post_infos, user_commented_total_list