-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidjourney.py
executable file
·60 lines (42 loc) · 1.4 KB
/
midjourney.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
import json
from bs4 import BeautifulSoup
import requests
import sys
url = "https://www.midjourney.com/showcase/recent/"
output_path = 'public/images.json'
def get_json_data(url):
response = requests.get(url)
if response.status_code != 200:
print(f"Error: Unable to fetch URL ({response.status_code})")
return None
soup = BeautifulSoup(response.text, 'html.parser')
script_tag = soup.find(
'script', {'id': '__NEXT_DATA__', 'type': 'application/json'})
if script_tag:
json_data = json.loads(script_tag.string)
return json_data
else:
print("No script tag with specified attributes found.")
return None
def get_carousel_images(json_data):
items = []
jobs = json_data.get('props', {}).get('pageProps', {}).get('jobs', [])
for job in jobs:
image_paths = job.get('image_paths', [])
if image_paths:
items.append(image_paths[0])
return items
def save_to_json(carousel_images):
print(f"Saving images to {output_path}")
with open(output_path, 'w') as f:
f.write(json.dumps(carousel_images, indent=2))
def main():
json_data = get_json_data(url)
if not json_data:
print("Error: No JSON data found.")
sys.exit(1)
carousel_images = get_carousel_images(json_data)
save_to_json(carousel_images)
if __name__ == "__main__":
main()