Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- from pixivpy3 import *
- import sys
- import getopt
- import os
- from random import choice
- from subprocess import Popen
- def getImage(argv):
- #ignore this part, it's just to get some parameters from the command line
- arg_content_type = ""
- arg_filename = ""
- arg_help = "{0} -f <file_name> -ct <content-type>".format(argv[0])
- try:
- opts, args = getopt.getopt(argv[1:], "hf:ct:", ["help", "filename=", "content-type="])
- except:
- print(arg_help)
- sys.exit(2)
- for opt, arg in opts:
- if opt in ("-h", "--help"):
- print(arg_help) # print the help message
- sys.exit(2)
- elif opt in ("-ct", "--content-type"):
- arg_content_type = arg
- elif opt in ("-f", "--filename"):
- arg_filename = arg
- # Down below it's the part that we are talking about
- # *****************************************************************************
- api = AppPixivAPI()
- refreshTokenFile = open("refresh_token.txt").readlines()
- 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
- accessTokenFile = open("access_token.txt").readlines()
- api.set_auth(access_token=accessTokenFile[0].strip(
- ), 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"
- json_result = api.illust_recommended(
- content_type=arg_content_type or "illust")
- # *****************************************************************************
- illusts_size = len(json_result.illusts)
- if illusts_size == 0:
- print("MissingResultError")
- sys.exit(2)
- random_index = os.urandom(1)[0] % illusts_size
- api.download(json_result.illusts[random_index].image_urls.medium, path= "../../../public/images/pixiv/", fname=arg_filename)
- json_result.illusts[random_index].image_urls.medium = "https://autibot.it/images/pixiv/" + arg_filename
- 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)
- if len(finalJson) > 1 and finalJson != "null":
- print(finalJson)
- sys.exit(0)
- else:
- print("MissingResultError")
- sys.exit(2)
- if __name__ == "__main__":
- getImage(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment