Created
January 3, 2023 20:25
-
-
Save dte/13600c279fbd552c3d569d72ab395d6f to your computer and use it in GitHub Desktop.
Download your midjourney gallery
This file contains 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
import requests | |
import re | |
import json | |
import os | |
import sys | |
UA = 'Midjourney-history-sync/1.0' | |
USER_ID = '###' | |
SESSION_TOKEN = "###" | |
headers = { | |
"User-Agent": UA, | |
"Cookie": f"__Secure-next-auth.session-token={SESSION_TOKEN}", | |
"Content-Type": "application/json" | |
} | |
url = "https://www.midjourney.com/api/app/recent-jobs/" | |
params = { | |
"orderBy": "new", | |
"user_id_ranked_score": "null", | |
"jobType": "null", | |
"jobStatus": "completed", | |
"page": 1, | |
"userId": USER_ID, | |
"dedupe": "true", | |
"refreshApi": 0 | |
} | |
response = requests.get(url, headers=headers, params=params) | |
jobs = response.json() | |
if not os.path.exists("jobs"): | |
os.makedirs("jobs") | |
with open("jobs/last.json", "w") as f: | |
json.dump(jobs, f) | |
for job in sorted(jobs, key=lambda x: x["enqueue_time"]): | |
job_id = job["id"] | |
if not re.match(r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", job_id): | |
raise ValueError(f"Potentially unsafe job ID '{job_id}' -- stopping!") | |
tdir = f"jobs/{job_id}" | |
if os.path.exists(tdir): | |
if os.path.exists(f"{tdir}/completed"): | |
print(f"Skipping {job_id} -- already downloaded.", file=sys.stderr) | |
continue | |
else: | |
print(f"Warning: {job_id} did not finish syncing. Will try again!", file=sys.stderr) | |
else: | |
print(f"Downloading {job_id}", file=sys.stderr) | |
os.makedirs(tdir) | |
with open(f"{tdir}/job.json", "w") as f: | |
json.dump(job, f) | |
for img_url in job["image_paths"]: | |
fname = img_url.split("/")[-1] | |
if not re.match(r"^[0-9_]+\.(png|jpg|jpeg|webp)$", fname): | |
raise ValueError(f"Potentially unsafe image path '{img_url}' ending in '{fname}' -- stopping!") | |
print(f" {fname}", file=sys.stderr) | |
response = requests.get(img_url, headers=headers) | |
with open(f"{tdir}/{fname}", "wb") as f: | |
f.write(response.content) | |
with open(f"{tdir}/completed", "w"): | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment