Created
July 30, 2023 15:38
-
-
Save Ailothaen/e0db39930a25c0bd9fdb7e4348d9b967 to your computer and use it in GitHub Desktop.
Download all Discord emoji/stickers from HAR file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import requests | |
import argparse, json, os, time | |
# -------------------------- # | |
# Functions # | |
# -------------------------- # | |
def get_url_info(url): | |
""" | |
Sees if an URL is "interesting", and if yes, returns useful info such as filename, best URL for download... | |
""" | |
# Emoji | |
if url.startswith("https://cdn.discordapp.com/emojis/"): | |
url_without_qs = url.split("?")[0] | |
filename = url_without_qs.split("/")[-1] | |
best_url = url_without_qs+"?quality=lossless" | |
return ("emoji", best_url, filename) | |
# Stickers | |
if url.startswith("https://media.discordapp.net/stickers/"): | |
url_without_qs = url.split("?")[0] | |
filename = url_without_qs.split("/")[-1] | |
best_url = url_without_qs+"?quality=lossless" | |
return ("sticker", best_url, filename) | |
else: | |
return None | |
def download_file(url, filename, chunk_size=8192): | |
""" | |
Download a file with requests. | |
Adapted from https://stackoverflow.com/a/16696317/5418422 | |
""" | |
with requests.get(url, stream=True) as r: | |
r.raise_for_status() | |
with open(filename, 'wb') as f: | |
for chunk in r.iter_content(chunk_size=chunk_size): | |
f.write(chunk) | |
def mkdir_safe(dir): | |
""" | |
os.mkdir() that does not complain if directory already exists | |
""" | |
try: | |
os.mkdir(dir) | |
except FileExistsError: | |
pass | |
# -------------------------- # | |
# Arguments # | |
# -------------------------- # | |
parser = argparse.ArgumentParser() | |
parser.add_argument('har', help='HAR file (retrieved with Firefox)') | |
parser.add_argument('-c', '--count-prefix', help='prefix files retrieved with an incremental count', action="store_true") | |
args = parser.parse_args() | |
# -------------------------- # | |
# Process # | |
# -------------------------- # | |
# Opening HAR file and getting all URLs from it | |
with open(args.har, "r") as f: | |
har = json.load(f) | |
urls_extracted = [entry["request"]["url"] for entry in har["log"]["entries"]] | |
urls_structured = {} | |
# Filter to keep only the relevant URLs, | |
# and ensure each URL is only present once. | |
for url in urls_extracted: | |
url_info = get_url_info(url) | |
if url_info is not None: | |
if url_info[1] not in urls_structured: | |
urls_structured[url_info[1]] = (url_info[0], url_info[2]) | |
print(f"{len(urls_structured)} emoji and stickers to download") | |
mkdir_safe("emoji") | |
mkdir_safe("sticker") | |
count = 0 | |
for url, url_info in urls_structured.items(): | |
if args.count_prefix: | |
download_file(url, f"{url_info[0]}/{count:05}_{url_info[1]}") | |
else: | |
download_file(url, f"{url_info[0]}/{url_info[1]}") | |
count += 1 | |
time.sleep(0.2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment