Skip to content

Instantly share code, notes, and snippets.

@JordanDworaczyk
Created September 24, 2021 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JordanDworaczyk/a647208d31d4938c40a33559230e986d to your computer and use it in GitHub Desktop.
Save JordanDworaczyk/a647208d31d4938c40a33559230e986d to your computer and use it in GitHub Desktop.
Download all of your Snapchat memories.
#!/usr/bin/env python3
"""
This is a script which uses the zip file obtained via Snapchat.
It takes the user's data and retrieves that user's Snapchat
memories. The media is then saved onto disk.
This is free and unencumbered software released into the public domain.
"""
__author__ = 'Jordan Taylor Dworaczyk'
__license__ = 'Unlicense'
__version__ = '1.0.0'
import json
import urllib.request
from datetime import datetime
from pathlib import Path
SUFFIX = {'PHOTO': 'jpg', 'VIDEO': 'mp4'}
PATH = Path('.')
MEDIA_DIR = Path('media')
MEDIA_DIR.mkdir(exist_ok=True)
QUERY = PATH.glob('**/memories_history.json')
def metadata(file):
"Load Snapchat's memories_history data."
with file.open('r') as f:
return json.load(f)
def retrieve_link(url):
"Retrieve link to stored media via Snapchat's hosted server."
request = urllib.request.Request(url, method='POST')
try:
with urllib.request.urlopen(request) as response:
return response.read()
except urllib.request.HTTPError as e:
raise RuntimeWarning("Could not access media.") from e
def retrieve_media(link):
"Retrieve your 'memory' as media (either .mp4 or .jpg). "
try:
with urllib.request.urlopen(link.decode()) as response:
return response.read()
except urllib.request.HTTPError as e:
raise RuntimeWarning("Could not access media.") from e
def save_media(data, name, media_type):
"Save media to disk."
with open(f"{name}.{SUFFIX[media_type]}", "wb") as f:
f.write(data)
def remove_utc(date):
"Removes UTC from string."
return date.replace(' UTC', '')
def date_path(date):
"Create path on your system for media."
date_of_memory = datetime.fromisoformat(remove_utc(date))
day_dir = MEDIA_DIR / Path(str(date_of_memory.date()))
time = Path(str(date_of_memory.time()))
day_dir.mkdir(exist_ok=True)
return day_dir / time
def download_memories():
"Download memories from Snapchat."
for filename in QUERY:
meta_data = metadata(filename)
saved_media = meta_data['Saved Media']
for media in saved_media:
url = media['Download Link']
date = media['Date']
media_type = media['Media Type']
try:
link = retrieve_link(url)
data = retrieve_media(link)
except RuntimeWarning:
print(f"Could not access {media_type} on {date}.")
else:
path_name = date_path(date)
save_media(data, path_name, media_type)
if __name__ == "__main__":
download_memories()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment