Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import time
- params_template = {
- "input": '{"json":{"period":"AllTime","periodMode":"published","sort":"Most Reactions","view":"feed","browsingMode":"NSFW","cursor":1,"authed":true}}',
- }
- urls = open("urls.txt", "a", encoding="utf-8")
- def save_file(url):
- # url encode
- url = url.replace(" ", "%20")
- urls.write(url + "\n")
- def process_page(cursor):
- print("Processing page: ", cursor)
- params = params_template.copy()
- params["input"] = params_template["input"].replace(
- '"cursor":1', '"cursor":' + str(cursor)
- )
- while True:
- r = requests.get(
- "https://civitai.com/api/trpc/image.getInfinite",
- headers={
- "referer": "https://civitai.com/",
- },
- params=params,
- )
- data = r.json()
- if "More info: http://pris.ly" in r.text:
- print("Retrying in 5 seconds...")
- time.sleep(5)
- continue
- else:
- break
- try:
- for image in data["result"]["data"]["json"]["items"]:
- if image["name"] is None:
- continue
- url = (
- "https://image.civitai.com/xG1nkqKTMzGDvpLrqFT7WA/"
- + image["url"]
- + "/width="
- + str(image["width"])
- + "/"
- + image["name"]
- )
- print(url)
- save_file(url)
- return data["result"]["data"]["json"]["nextCursor"]
- except KeyError:
- print("Error: ", data)
- return None
- next_page = process_page(102)
- while next_page:
- next_page = process_page(next_page)
Advertisement
Add Comment
Please, Sign In to add comment