Skip to content

Instantly share code, notes, and snippets.

@raleighlittles
Created June 22, 2022 05:14
Show Gist options
  • Save raleighlittles/53a1b5876335f5d877df5ab6d7280497 to your computer and use it in GitHub Desktop.
Save raleighlittles/53a1b5876335f5d877df5ab6d7280497 to your computer and use it in GitHub Desktop.
A simple tool for analyzing your like behavior and viewing activity on Instagram

About

This tool uses your Instagram data to visualize which accounts you interact with most. When you do an export of your Instagram data, every post & comment you liked, as well as every post & video you've viewed, are recorded. This tool simply analyzes that and produces a simple report:

Usage

To use this tool, you obviously first need to download your Instagram data.

Once the data is downloaded, extract it, and move any of the following HTML files to this location:

  • liked_comments.html
  • liked_posts.html
  • posts_viewed.html
  • videos_watched.html

Then run:

$ python3 instagram_analyzer.py

Dependencies

  • LXML for parsing HTML

Code

# File: instagram_analyzer.py
import lxml.html
import collections

def analyze_viewed_videos_or_posts(viewed_filename : str, limit_top_n : int) -> collections.Counter:
    """
    Analyze your viewed posts or videos (the format for both files is the same),
    return a dictionary showing views per username.
    """

    with open(viewed_filename, "rb") as html_file:
        page = html_file.read()

    tree = lxml.html.fromstring(page)

    usernames = []

    # I haven't been able to figure out why, but some elements in the HTML are missing
    # usernames.
    # Not sure where this expression comes from
    for element in tree.body.xpath("//td[@class='_2pin _2piu _a6_r']"):
        try:
            usernames.append(element.getchildren()[0].text)
        except Exception as e:
            continue

    return collections.Counter(usernames).most_common(limit_top_n)


def analyze_liked_posts_or_comments(liked_filename : str, limit_top_n : int) -> collections.Counter:
    """
    Analyze the posts or comments you've liked, and return a dictionary where keys are the usernames
    and values are the number of likes you've given that user's posts or comments
    """
    
    with open(liked_filename, "rb") as html_file:
        page = html_file.read()

    tree = lxml.html.fromstring(page)

    # Random hard-coded expression; not sure where this comes from
    usernames_and_like_counts = collections.Counter([ element.text for element in tree.body.xpath("//div[@class='_3-95 _2pim _a6-h _a6-i']") ])

    return usernames_and_like_counts.most_common(limit_top_n)


if __name__ == "__main__":
    print("Most liked comments: ", analyze_liked_posts_or_comments('liked_comments.html', 10), "\n")
    print("Most liked posts: ", analyze_liked_posts_or_comments('liked_posts.html', 10), "\n")
    print("Most viewed videos: ", analyze_viewed_videos_or_posts('videos_watched.html', 10), "\n")
    print("Most viewed posts: ", analyze_viewed_videos_or_posts('posts_viewed.html', 10), "\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment