#!/usr/bin/env python3 # INSTAGRAM JSON VIEWER # 2020 (c) Micha Johannes Birklbauer # https://github.com/t0xic-m/ # micha.birklbauer@gmail.com from datetime import datetime as dt import urllib.request as ur import traceback as tb import time import json import os # template html # largely taken and adapted from w3schools.com # -> https://www.w3schools.com/howto/howto_css_fixed_sidebar.asp # -> https://www.w3schools.com/howto/howto_css_chat.asp html_template = \ """ Instagram Data Report """ # template sidebar html sidebar_template = \ """
Profile Information Searches Profile Connections Media Stories Photos Profile Pictures Videos Comments Messages Devices """ # patting myself on the shoulder for this one uwu credits = \ """

Credits

Report created with Micha Birklbauer's Instagram JSON Viewer.

""" # reading profile information and converting it to html string # default filenames and arguments should be fine for all functions if script is in the right directory def read_profile(filename = "profile.json"): # error controls and logging status = 0 errors = [] # html template html_string = "

Profile Information

\n\t\n
\n" # creating an error log for file output error_log = "Encountered errors reading profile: " + str(status) + "\nError messages:\n" + str("\n".join(errors)) + "\n\n" return [html_string, status, error_log] # reading search information and converting it to html string def read_searches(filename = "searches.json"): # error controls and logging status = 0 errors = [] # html template html_string = "

Searches

\n" # json to html conversion try: with open(filename, "r", encoding="utf-8") as f: data = json.load(f) f.close() main_search = data["main_search_history"] shopping_search = data["shopping_search_history"] html_string = html_string + "\t

Profile Searches

\n\t\t\n\t

Shopping Searches

\n\t\t\n
\n" # creating an error log for file output error_log = "Encountered errors reading searches: " + str(status) + "\nError messages:\n" + str("\n".join(errors)) + "\n\n" return [html_string, status, error_log] # reading connection information and converting it to html string def read_connections(filename = "connections.json"): # error controls and logging status = 0 errors = [] # html template html_string = "

Profile Connections

\n\t
\n\n" elif "timestamp_ms" in message: html_chat_string = html_chat_string + "\t" + str(dt.fromtimestamp(int(message["timestamp_ms"])/1000)) + "\n\n\n" else: # throw warning? html_chat_string = html_chat_string + "\t Time could not be extracted. \n\n\n" else: if "created_at" in message: html_chat_string = html_chat_string + "\t" + str(message["created_at"]) + "\n\n\n" elif "timestamp_ms" in message: html_chat_string = html_chat_string + "\t" + str(dt.fromtimestamp(int(message["timestamp_ms"])/1000)) + "\n\n\n" else: # throw warning? html_chat_string = html_chat_string + "\t Time could not be extracted. \n\n\n" else: # this should probably throw an exception pass html_chat_string = html_chat_string + "
\n" ext_filename = "chat/" + str(len(chat_list)) + ".html" with open(ext_filename, "w", encoding="utf-8") as f: content = html_template + "

Messages

\n\n" + html_chat_string + "" f.write(content) f.close() html_string = html_string + "
  • " + str(", ".join(participants_new)) + "
  • \n" except Exception as e: print("ERROR reading messages!") print("Affected conversion: ", str(", ".join(participants_new))) conv_errors.append(str(", ".join(participants_new))) errors.append(str(repr(e))) errors.append("Detailed Traceback:") errors.append(tb.format_exc()) tb.print_exc() status = status + 1 html_string = html_string + "\n" # creating an error log for file output error_log = "Encountered errors reading messages: " + str(status) + "\n\nAffected conversations:\n" + str("\n".join(conv_errors)) + "\n\nError messages:\n" + str("\n".join(errors)) + "\n\n" return [html_string, chat_list, status, error_log] # reading device information and converting it to html string def read_devices(filename = "devices.json"): # error controls and logging status = 0 errors = [] # html template html_string = "

    Devices

    \n\t\n
    \n" # creating an error log for file output error_log = "Encountered errors reading devices: " + str(status) + "\nError messages:\n" + str("\n".join(errors)) + "\n\n" return [html_string, status, error_log] # main function = executes all previous functions and concatenates html string to html file # title can be changed as title = "

    YOUR TITLE HERE

    \n" (should be html) # for args refer to README.md again # needless to say that there are no input checks here either, incorrect inputs will lead to crashes! # so be careful if you don't want things to go sideways def instaview(filenames = ["profile.json", "searches.json", "connections.json", "media.json", "comments.json", "messages.json", "devices.json"], parse = [True, True, True, True, True, True, True], title = None, show_credits = True, verbose = True, logging = True, **kwargs): result = 0 complete_log = "" if title is None: title = "

    INSTAGRAM DATA [" + str(dt.today().strftime('%Y-%m-%d'))+ "]

    \n" if parse[5]: if verbose: print("Reading messages...") try: chat_string, chat_list, status_code, error_log = read_messages(filenames[5], **kwargs) complete_log = complete_log + error_log if status_code != 0: print("ERRORs encountered while reading chats: ", str(status_code)) except Exception as e: chat_string, chat_list = ["", []] result = result + 32 print("FATAL ERROR reading messages!") tb.print_exc() complete_log = complete_log + "\n\nFATAL ERROR reading messages!\n" + tb.format_exc() + "\n\n" if show_credits: sidebar = sidebar_template + " Credits\n\n" else: sidebar = sidebar_template + "\n" end_html = "\n\n\n" if parse[0]: if verbose: print("Reading profile...") try: a, status_code, error_log = read_profile(filenames[0]) complete_log = complete_log + error_log if status_code != 0: print("ERRORs encountered while reading profile: ", str(status_code)) except Exception as e: a = "" result = result + 1 print("FATAL ERROR reading profile!") tb.print_exc() complete_log = complete_log + "\n\nFATAL ERROR reading profile!\n" + tb.format_exc() + "\n\n" if parse[1]: if verbose: print("Reading searches...") try: b, status_code, error_log = read_searches(filenames[1]) complete_log = complete_log + error_log if status_code != 0: print("ERRORs encountered while reading searches: ", str(status_code)) except Exception as e: b = "" result = result + 2 print("FATAL ERROR reading searches!") tb.print_exc() complete_log = complete_log + "\n\nFATAL ERROR reading searches!\n" + tb.format_exc() + "\n\n" if parse[2]: if verbose: print("Reading connections...") try: c, status_code, error_log = read_connections(filenames[2]) complete_log = complete_log + error_log if status_code != 0: print("ERRORs encountered while reading connections: ", str(status_code)) except Exception as e: c = "" result = result + 4 print("FATAL ERROR reading connections!") tb.print_exc() complete_log = complete_log + "\n\nFATAL ERROR reading connections!\n" + tb.format_exc() + "\n\n" if parse[3]: if verbose: print("Reading media...") try: d, status_code, error_log = read_media(filenames[3]) complete_log = complete_log + error_log if status_code != 0: print("ERRORs encountered while reading media: ", str(status_code)) except Exception as e: d = "" result = result + 8 print("FATAL ERROR reading media!") tb.print_exc() complete_log = complete_log + "\n\nFATAL ERROR reading media!\n" + tb.format_exc() + "\n\n" if parse[4]: if verbose: print("Reading comments...") try: g, status_code, error_log = read_comments(filenames[4]) complete_log = complete_log + error_log if status_code != 0: print("ERRORs encountered while reading comments: ", str(status_code)) except Exception as e: g = "" result = result + 16 print("FATAL ERROR reading comments!") tb.print_exc() complete_log = complete_log + "\n\nFATAL ERROR reading comments!\n" + tb.format_exc() + "\n\n" if parse[6]: if verbose: print("Reading devices...") try: h, status_code, error_log = read_devices(filenames[6]) complete_log = complete_log + error_log if status_code != 0: print("ERRORs encountered while reading devices: ", str(status_code)) except Exception as e: h = "" result = result + 16 print("FATAL ERROR reading devices!") tb.print_exc() complete_log = complete_log + "\n\nFATAL ERROR reading devices!\n" + tb.format_exc() + "\n\n" if show_credits: complete_html = html_template + sidebar + "
    \n\n" + title + a + b + c + d + g + chat_string + h + credits + end_html else: complete_html = html_template + sidebar + "
    \n\n" + title + a + b + c + d + g + chat_string + h + end_html with open("instaview_report.html", "w", encoding="utf-8") as f: f.write(complete_html) f.close() if logging: with open("instaview_report.log", "w", encoding="utf-8") as f: f.write(complete_log) f.close() return result if __name__ == '__main__': # functions are run with default args when script is executed # this should terminate succesfully if script is in the right directory try: res = instaview() print(res) except Exception as e: print("ERROR! Is script in right directory?") tb.print_exc()