import json
import logging
import sys
import time
import os
import instagram

def saveJSON(timestamp, type, content):
    dirpath = os.getcwd()
    path = os.path.join(dirpath, "json", str(timestamp) + "_" + type + ".json")
    dirpath = os.path.dirname(path)
    os.makedirs(dirpath, exist_ok=True)
    f = open(path, "tx")
    json.dump(content, f)
    f.close()

def main():
    configfile = "config.json"
    logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)

    try:
        logging.info("Opening config file.")
        f = open(configfile, "tr")
    except FileNotFoundError:
        logging.info("File not found.")
        logging.info("Creating new config file.")
        f = open(configfile, "tx+")

    try:
        logging.info("Loading config file.")
        config = json.load(f)
    except json.decoder.JSONDecodeError:
        logging.info("Config file corrupted.")
        logging.info("Creating new config file.")
        f.close()
        f = open(configfile, "tw+")
        logging.info("Updating config.")
        iguserid = input("Enter your IG user ID: ")
        igsessionid = input("Enter your IG session ID: ")
        igcsrftoken = input("Enter your IG CSRF token: ")
        config = {
            "ds_user_id" : iguserid,
            "sessionid" : igsessionid,
            "csrftoken": igcsrftoken,
        }
        logging.info("Saving config.")
        json.dump(config, f) # Save config
        
    #logging.info("Config settings:")
    #logging.info("%s", config) # Contains private data
    f.close()

    # Insert error checking to see if config is valid and works

    logging.info("Initialize IG interface.")
    ig = instagram.instagram(config)
    logging.info("Get story tray.")
    traytime = int(time.time())
    storyresp = ig.getStories()
    storyjson = storyresp.json()
    saveJSON(traytime, "tray", storyjson)

    logging.info("Downloading story tray.")
    ig.downloadTray(storyjson)
    users = ig.getUserIDs(storyjson)
    for user in users:
        reeltime = int(time.time())
        uresp = ig.getUserStories(user)
        ujson = uresp.json()
        saveJSON(reeltime, "reel_" + str(user), ujson)
        ig.downloadReel(ujson)

    logging.info("Done.")
    
if __name__ == "__main__":
    main()

@@ -69,16 +69,31 @@
 nstagram
+%0Aimport tarfile
 %0A%0Adef sa
@@ -2202,24 +2202,573 @@
 eel(ujson)%0A%0A
+    logging.info(%22Collecting list of JSON objects.%22)%0A    jsonlist = %5B%5D%0A    for file in os.listdir(%22json%22):%0A        if file.endswith(%22.json%22):%0A            jsonlist.append(os.path.join(%22json%22, file))%0A        %0A    logging.info(%22Creating tar.xz file of JSON objects.%22)%0A    path = os.path.join(os.getcwd(), %22json%22, str(traytime) + %22.tar.xz%22)%0A    tar = tarfile.open(path, %22x:xz%22)%0A    for path in jsonlist:%0A        tar.add(path)%0A    tar.close()%0A    %0A    logging.info(%22Removing old JSON objects.%22)%0A    for path in jsonlist:%0A        os.remove(path)%0A        %0A
     logging.

Archive JSON files