Guest User

Untitled

a guest
Mar 7th, 2023
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1. import json
  2. from pixivpy3 import *
  3. import sys
  4. import getopt
  5. import os
  6. from random import choice
  7. from subprocess import Popen
  8.  
  9.  
  10. def getImage(argv):
  11.     #ignore this part, it's just to get some parameters from the command line
  12.     arg_content_type = ""
  13.     arg_filename = ""
  14.     arg_help = "{0} -f <file_name> -ct <content-type>".format(argv[0])
  15.  
  16.     try:
  17.         opts, args = getopt.getopt(argv[1:], "hf:ct:", ["help", "filename=", "content-type="])
  18.     except:
  19.         print(arg_help)
  20.         sys.exit(2)
  21.     for opt, arg in opts:
  22.         if opt in ("-h", "--help"):
  23.             print(arg_help)  # print the help message
  24.             sys.exit(2)
  25.         elif opt in ("-ct", "--content-type"):
  26.             arg_content_type = arg
  27.         elif opt in ("-f", "--filename"):
  28.             arg_filename = arg
  29.     # Down below it's the part that we are talking about
  30.     # *****************************************************************************
  31.     api = AppPixivAPI()
  32.     refreshTokenFile = open("refresh_token.txt").readlines()
  33.     Popen(["python3", "pixiv_auth.py", "refresh", refreshTokenFile[0].strip()]) # this runs the "File 1" with the parameter refresh so you need to pass your refresh_token
  34.     accessTokenFile = open("access_token.txt").readlines()
  35.     api.set_auth(access_token=accessTokenFile[0].strip(
  36.     ), refresh_token=refreshTokenFile[0].strip()) # we pass both the refresh token and the newly acquired access_token which was just saved in the access_token.txt file in the "File 1"
  37.     json_result = api.illust_recommended(
  38.         content_type=arg_content_type or "illust")
  39.     # *****************************************************************************
  40.     illusts_size = len(json_result.illusts)
  41.    
  42.     if illusts_size == 0:
  43.         print("MissingResultError")
  44.         sys.exit(2)
  45.     random_index = os.urandom(1)[0] % illusts_size
  46.     api.download(json_result.illusts[random_index].image_urls.medium, path= "../../../public/images/pixiv/", fname=arg_filename)
  47.     json_result.illusts[random_index].image_urls.medium = "https://autibot.it/images/pixiv/" + arg_filename
  48.     finalJson = json.dumps({"id": json_result.illusts[random_index].id, "title": json_result.illusts[random_index].title, "image_url": json_result.illusts[random_index].image_urls.medium, "tags": json_result.illusts[random_index].tags}, default=lambda o: o.__dict__, sort_keys=True, indent=4)
  49.     if len(finalJson) > 1 and finalJson != "null":
  50.         print(finalJson)
  51.         sys.exit(0)
  52.     else:
  53.         print("MissingResultError")
  54.         sys.exit(2)
  55.  
  56. if __name__ == "__main__":
  57.     getImage(sys.argv)
  58.  
Advertisement
Add Comment
Please, Sign In to add comment