Advertisement
Guest User

Get E621 Tag Parents

a guest
Sep 30th, 2022
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import requests
  4. import time
  5.  
  6. # set these!
  7. USERNAME = "" # e621 username
  8. API_KEY = "" # e621 api key
  9. # you need to enable API access to get a key.
  10. # to enable API access, you must go to your e621 API page (Account > Manage API Access) and generate an API key.
  11.  
  12. pages = 400
  13. mode = "aliases" # aliases/implications
  14.  
  15.  
  16.  
  17.  
  18. # setting up variables for POST request
  19. url = "https://e621.net/tag_" + mode + ".json"
  20. headers = {'user-agent': 'tagAliases/' + USERNAME}
  21. auth = requests.auth.HTTPBasicAuth(USERNAME, API_KEY)
  22.  
  23. # Input the numbers and namespaces that you want to include here
  24. catMap = {"0":"", "1":"creator:", "3":"series:", "4":"character:", "5":"species:", "8":"lore:"}
  25. relevantCats = ["0", "1", "3", "4", "5", "8"]
  26.  
  27. # 0 General ""
  28. # 1 Artist "creator:"
  29. # 3 Copyright "series:"
  30. # 4 Character "character:"
  31. # 5 Species "species:"
  32. # 6 invalid "?"
  33. # 7 Meta "meta:"
  34. # 8 Lore "lore:"
  35.  
  36. for firstCat in relevantCats:
  37.     for secondCat in relevantCats:
  38.         print('starting ' + firstCat + '-' + secondCat)
  39.         for i in range(1, pages+1, 1):
  40.             r = requests.get(url, params = {"search[antecedent_tag][category]": firstCat, "search[consequent_tag][category]": secondCat, "limit": "1000", "search[order]": "tag_count", "page": i, "search[status]": "Approved"}, headers=headers, auth=auth)
  41.  
  42.             # stop searches if we get any code back other than 200 (OK)
  43.             if (r.status_code != 200):
  44.                 print("ERR:", r.status_code)
  45.                 exit
  46.  
  47.             dummyJsonString = 'tag_' + mode
  48.             dummyJson = {dummyJsonString: []}
  49.             if (r.json() == dummyJson):
  50.                 print('finished ' + firstCat + '-' + secondCat + ' at page ' + str(i-1))
  51.                 break
  52.  
  53.             f = open(mode + ".txt", 'a')
  54.             for curTag in r.json():
  55.                 if(curTag['consequent_name'] != 'avoid_posting' and curTag['consequent_name'] != 'conditional_dnp'):
  56.                     FileLine = catMap[firstCat] + curTag['antecedent_name'] + "\n" + catMap[secondCat] + curTag['consequent_name'] + "\n\n"
  57.                     f.write(FileLine)
  58.                     # ConsoleLine = catMap[firstCat] + curTag['antecedent_name'] + " -> " + catMap[secondCat] + curTag['consequent_name']
  59.                     # print(ConsoleLine)
  60.  
  61.             f.close()
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement