Guest User

Untitled

a guest
Jul 17th, 2023
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. import requests
  2. import time
  3.  
  4. params_template = {
  5. "input": '{"json":{"period":"AllTime","periodMode":"published","sort":"Most Reactions","view":"feed","browsingMode":"NSFW","cursor":1,"authed":true}}',
  6. }
  7.  
  8. urls = open("urls.txt", "a", encoding="utf-8")
  9.  
  10.  
  11. def save_file(url):
  12. # url encode
  13. url = url.replace(" ", "%20")
  14. urls.write(url + "\n")
  15.  
  16.  
  17. def process_page(cursor):
  18. print("Processing page: ", cursor)
  19. params = params_template.copy()
  20. params["input"] = params_template["input"].replace(
  21. '"cursor":1', '"cursor":' + str(cursor)
  22. )
  23.  
  24. while True:
  25. r = requests.get(
  26. "https://civitai.com/api/trpc/image.getInfinite",
  27. headers={
  28. "referer": "https://civitai.com/",
  29. },
  30. params=params,
  31. )
  32. data = r.json()
  33.  
  34. if "More info: http://pris.ly" in r.text:
  35. print("Retrying in 5 seconds...")
  36. time.sleep(5)
  37. continue
  38. else:
  39. break
  40.  
  41. try:
  42. for image in data["result"]["data"]["json"]["items"]:
  43. if image["name"] is None:
  44. continue
  45. url = (
  46. "https://image.civitai.com/xG1nkqKTMzGDvpLrqFT7WA/"
  47. + image["url"]
  48. + "/width="
  49. + str(image["width"])
  50. + "/"
  51. + image["name"]
  52. )
  53. print(url)
  54. save_file(url)
  55.  
  56. return data["result"]["data"]["json"]["nextCursor"]
  57.  
  58. except KeyError:
  59. print("Error: ", data)
  60. return None
  61.  
  62.  
  63. next_page = process_page(102)
  64. while next_page:
  65. next_page = process_page(next_page)
  66.  
Advertisement
Add Comment
Please, Sign In to add comment