Skip to content

Instantly share code, notes, and snippets.

@DerrikMilligan
Last active July 2, 2021 04:26
Show Gist options
  • Save DerrikMilligan/e7905e1a364f02158496787a58afcb52 to your computer and use it in GitHub Desktop.
Save DerrikMilligan/e7905e1a364f02158496787a58afcb52 to your computer and use it in GitHub Desktop.
A small script that takes the snapchat dump memories.json file and will download all the files from it concurrently
import concurrent.futures
import os
import re
import json
import mimetypes
# required libraries
import magic
import requests
base_dir = os.path.dirname(os.path.abspath(__file__))
json_file = base_dir + "/memories_history.json"
output_dir = base_dir + "/output"
print(f"base_dir is {base_dir}")
print(f"json_file is {json_file}")
print(f"output_dir is {output_dir}")
def download_file(file_info):
try:
pieces = file_info['Download Link'].split('?')
setup = requests.post(pieces[0], data=pieces[1], headers = {'Content-Type': 'application/x-www-form-urlencoded'})
if setup.status_code == 200:
uid = re.findall(r"sid=(.*?)&", file_info['Download Link'])[0]
download_url = setup.content
file_stream = requests.get(download_url, stream = True)
file_path = output_dir + "/" + uid
# Download the file with the stream
with open(file_path, 'wb') as file:
print(f"Downloading file: {file_path}")
for chunk in file_stream:
file.write(chunk)
# Get the file info so we can preserve the creation time
stat = os.stat(file_path)
# Rename the file with the correct extension
mime = magic.from_file(file_path, mime = True)
extension = mimetypes.guess_extension(mime)
os.rename(file_path, file_path + extension)
# Put the original timestamp back after renaming the file
os.utime(file_path + extension, (stat.st_atime, stat.st_mtime))
except Exception as e:
print(e)
if __name__ == "__main__":
with open(json_file) as f:
data = json.load(f)
# If you want to download every file one at a time
# for file in data["Saved Media"]:
# download_file(file)
with concurrent.futures.ThreadPoolExecutor(4) as executor:
executor.map(download_file, data["Saved Media"])
@DerrikMilligan
Copy link
Author

DerrikMilligan commented Jul 1, 2021

You can get the memory_history.json by requesting all your Snapchat data here

You'll need to install these python libraries for this to work.

pip3 install requests
pip3 install python-magic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment