Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #CIVITAI collection dowload
- import requests
- import os
- import json
- import urllib
- ###
- #SET THIS STUFF AS NEEDED
- collectionID = 8022086
- myApiKey = 'apiapiapi564687apiapi' #<YOUR api key
- folderName = './downloadedImages/'
- ###
- def processPage(url, first=False):
- try:
- response = requests.get(url, headers=requestHeaders)
- response.raise_for_status()
- responseJson = response.json()
- nextCursor = responseJson['result']['data']['json']['nextCursor']
- items = responseJson['result']['data']['json']['items']
- print()
- print('count of images in batch', len(items))
- for item in items:
- try:
- imageName = item['name'].replace('/', '')#sometimes '/' is at start, sometimes isn't
- fileType = item['mimeType'].split('/')[1]
- destination = folderName+imageName
- if('.' not in imageName):#sometimes "name" doesnt contain fileextension
- destination += '.' + fileType
- if(os.path.isfile(destination)):
- print(destination+' already exists, skipping download')
- else:
- url = 'https://image.civitai.com/'+myApiKey+'/'+item['url']+'/'+imageName
- print('downloading image from url', url)
- with requests.get(url, stream=True) as response:
- response.raise_for_status()
- with open(destination, 'wb') as f:
- for chunk in response.iter_content():
- f.write(chunk)
- print('saved to: '+destination)
- except Exception as Y:
- print(Y)
- print("Image download failed")
- except Exception as X:
- print(X)
- if first:
- input("First page request failed, Press Enter to exit...")
- sys.exit()
- else:
- print("Page request failed")
- print('batch done, next cursor', nextCursor)
- return nextCursor
- print('your api key:',myApiKey)
- print('downloading collection ID:',collectionID)
- print('output folder:',folderName)
- os.makedirs(folderName, exist_ok=True)
- requestHeaders = {'Authorization': 'Bearer '+myApiKey}
- #urlString = '%7B%22json%22%3A%7B%22collectionId%22%3A4192940%2C%22period%22%3A%22AllTime%22%2C%22sort%22%3A%22Newest%22%2C%22browsingLevel%22%3A31%2C%22include%22%3A%5B%22cosmetics%22%5D%2C%22cursor%22%3Anull%2C%22authed%22%3Atrue%7D%2C%22meta%22%3A%7B%22values%22%3A%7B%22cursor%22%3A%5B%22undefined%22%5D%7D%7D%7D'
- #decoded = urllib.parse.unquote(urlString)
- #print('decoded',decoded)
- requestData = {
- "json":{
- "collectionId":collectionID,
- "period":"AllTime",
- "sort":"Newest",
- "browsingLevel":31,# = 1(PG) + 2(PG-13) + 4(R) + 8(X) + 16(XXX)
- "include":["cosmetics"],
- "cursor":None,
- "authed":True
- },
- "meta":{"values":{"cursor":["undefined"]}}
- }
- #urlString2 = urllib.parse.quote(json.dumps(requestData,separators=(',', ':')))
- #print('urlString',urlString)
- #print('urlStrjng',urlString2)
- #print('dump',json.dumps(requestData))
- url = 'https://civitai.com/api/trpc/image.getInfinite?input=' + urllib.parse.quote(json.dumps(requestData,separators=(',', ':')))
- nextCursor = processPage(url, True)
- del requestData["meta"] #delete "meta" part, only used in first request
- while(nextCursor!=None):
- requestData['json']['cursor'] = nextCursor
- url = 'https://civitai.com/api/trpc/image.getInfinite?input=' + urllib.parse.quote(json.dumps(requestData,separators=(',', ':')))
- nextCursor = processPage(url)
- input("All done, Press Enter to finish...")
Advertisement
Add Comment
Please, Sign In to add comment