欢迎光临散文网 会员登陆 & 注册

python皮肤文件获取脚本分享

2023-05-13 08:10 作者:小大小深  | 我要投稿

以下是脚本,错误几乎遇到的全部都有处理,编写不易,还望点个小赞~~(注:要先下载pyautogui第三方库,命令:pip install pyautogui)

------------------------------------------------------------------

import requests

import re

import base64

import _thread

import pyautogui

from time import sleep

import os

import time


print(f"{time.strftime('%H:%M:%S', time.localtime())}  正在准备.....")


# 变量定义

name_list = open(input("请输入皮肤名文本文件位置"))

log = open("请输入日志文本文件位置(注:要先创建好文件)", "a+")

print_pond = []

pan = True

list = name_list.readlines()

run = True

skin_pond = []

start = 426618

theard = 0


header1 = {

    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"

    " AppleWebKit/537.36 (KHTML, like Gecko)"

    " Chrome/92.0.4515.131"

    " Safari/537.36 Edg/92.0.902.67",


    "Accept": "text/html,application/xhtml+xml,application/xml;"

    "q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",


    "Accept-Encoding": "gzip, deflate, br",

    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",

    "Cache-Control": "max-age=0",

    "Connection": "keep-alive",

    "Host": "api.mojang.com",

    "sec-ch-ua": '"Chromium";v="92", " Not A;Brand";v="99", "Microsoft Edge";v="92"',

    "sec-ch-ua-mobile": "?0",

    "Sec-Fetch-Dest": "document",

    "Sec-Fetch-Mode": "navigate",

    "Sec-Fetch-Site": "none",

    "Sec-Fetch-User": "?1",

    "Upgrade-Insecure-Requests": "1"

}


header2 = {"authority": "sessionserver.mojang.com",

           "method": "GET",

           "path": "/session/minecraft/profile/75999265ff224440a192440615475d9c",

           "scheme": "https",


           "accept": "text/html,application/xhtml+xml,application/xml;"

           "q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",


           "accept-encoding": "gzip, deflate, br",

           "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",

           "cache-control": "max-age=0",

           "sec-ch-ua": '"Chromium";v="92", " Not A;Brand";v="99", "Microsoft Edge";v="92"',

           "sec-ch-ua-mobile": "?0",

           "sec-fetch-dest": "document",

           "sec-fetch-mode": "navigate",

           "sec-fetch-site": "none",

           "sec-fetch-user": "?1",

           "upgrade-insecure-requests": "1",


           "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "

           "AppleWebKit/537.36 (KHTML, like Gecko) "

           "Chrome/92.0.4515.131 "

           "Safari/537.36 "

           "Edg/92.0.902.67"

           }

# 变量定义


# 函数定义


def gtime():  # 获取当前时间

    return time.strftime(r"%H:%M:%S", time.localtime())+"  "


def html_get(url, the_header, id, error1="网络不可用!", error2="错误!(网站已响应,数据无效!)"):  # 获取html

    global pan

    while not pan:  # 在错误解除之前一直等待

        sleep(1)

    is_one = False

    not_one = False

    htm = None

    code = ""

    text = ""

    to_try = 0

    while True:

        try:

            htm = requests.get(url, headers=the_header, timeout=8)

            htm = str(htm.text)

            code = "OK"


            try:  # 错误检测

                # 网站响应,但无数据

                if htm == "":

                    code = "error2"


                # 内部服务器错误

                if htm == '{"error":"Internal Server Error","errorMessage":"The server encountered an unexpected condition which prevented it from fulfilling the request"}':

                    pan = False

                    print(gtime()+f"get_skin_thread(id:{id}):内部服务器错误,等待10秒后再次尝试")

                    sleep(10)

                    raise OSError


                # 请求过多

                if htm == '{"error":"TooManyRequestsException","errorMessage":"The client has sent too many requests within a certain amount of time"}':

                    while not pan:  # 在错误解除之前一直等待

                        if is_one:

                            break

                        sleep(1)

                        not_one = True

                    pan = False

                    if not not_one:

                        is_one = True

                    text = text + "\n" + \

                        gtime()+f"get_skin_thread(id:{id}):请求过多!等待10秒后再次尝试"

                    print(gtime()+f"get_skin_thread(id:{id}):请求过多!等待10秒后再次尝试")

                    sleep(10)

                    raise OSError


                # 网站服务不可用

                if htm[1] == "!":

                    pan = False

                    print(gtime()+f"get_skin_thread(id:{id}):网站服务不可用,等待60秒后再次尝试")

                    sleep(60)

                    raise OSError

            except IndexError:

                pass


            pan = True

            break

        except OSError:

            pass

        except Exception:

            text = text + "\n" + \

                gtime()+f"get_skin_thread(id:{id}):网站超时!尝试重新连接。(第{to_try+1}次)"

            print(gtime()+f"get_skin_thread(id:{id}):网站超时!尝试重新连接。(第{to_try+1}次)")


            to_try += 1

            if to_try > 10:  # 网站无响应十次,退出

                code = "error1"

                break


    if code == "error1":  # 网站无响应,停止运行

        text = text + "\n"+gtime() + error1

        code = "break"


    if code == "error2":

        text = text + "\n"+gtime() + error2

        code = "continue"  # 网站响应,但无数据,跳过

    return htm, text, code


def download(url, path, name):  # 下载一个文件

    while True:

        try:

            r = requests.get(url)

            break

        except ConnectionError:

            pass

        except Exception:

            pass

    try:

        with open(path, 'wb') as f:

            f.write(r.content)

    except OSError:

        path = "D:\\Desktop\\auto_4\\"+name+"_.png"

        try:

            with open(path, 'wb') as f:

                f.write(r.content)

        except OSError:

            pass


def get_a_skin(id):  # 获取一个皮肤

    global print_pond

    global pan

    global run

    global theard


    theard += 1

    name = list[i]

    print_text = ""

    code = ""

    print_text = print_text + \

        gtime()+f" -----------------------get_skin_thread(id:{id})_print--------------------------"

    print_text = print_text+"\n"+gtime()+f"个数:{id}"

    print_text = print_text+"\n"+gtime()+"玩家名:"+str(name)


    url = "https://api.mojang.com/users/profiles/minecraft/"+name  # 获取URL


    # 请求

    html, text, code = html_get(url, header1, id, error2="此皮肤不存在!")

    print_text = print_text + text

    # 错误响应

    if code == "break":

        print_pond.append(print_text)

        run = False

        return

    if code == "continue":

        print_pond.append(print_text)

        return

    # 错误响应

    # 请求


    # 获取URL

    uuid = str(re.findall("[0123456789abcdef]{32}", html)[0])

    print_text = print_text+"\n"+gtime()+"玩家uuid:"+str(uuid)

    header2["path"] = "/session/minecraft/profile/"+uuid

    url = "https://sessionserver.mojang.com/session/minecraft/profile/"+uuid

    # 获取URL


    # 请求

    html, text, code = html_get(url, header2, id)

    print_text = print_text + text

    # 错误响应

    if code == "break":

        print_pond.append(print_text)

        run = False

        return

    if code == "continue":

        print_pond.append(print_text)

        return

    # 错误响应

    # 请求


    base = str(re.findall(r'"value" : "[\w=]+"', html)[0])  # 提取base64编码


    # base64解码

    base = base[11:len(base)-1]

    html = base64.b64decode(base)

    html = html.decode('utf-8')

    # base64解码


    # 获取皮肤url

    try:  # 尝试两种过滤方案

        url = re.findall(r'''"SKIN" : {

  "url" : "http://textures.minecraft.net/texture/[0123456789abcdef]+''', html)[0]

    except IndexError:

        url = re.findall(r'''"SKIN" : {

      "url" : "http://textures.minecraft.net/texture/[0123456789abcdef]+''', html)[0]

    url = url[26:len(url)]

    # 获取皮肤url


    # 保存

    path = "D:\\Desktop\\auto_4\\"+name+".png"

    print_text = print_text+"\n"+gtime()+"皮肤网址:"+str(url)

    print_text = print_text+"\n"+gtime()+"保存地址:"+str(path)

    download(url, path, name)

    # 保存


    code = "OK"


    print_pond.append(print_text)

    theard -= 1

    return


def thread_print():

    global print_pond

    while run:

        try:

            print(print_pond[0])

            print_pond.pop(0)

            sleep(0.1)

        except IndexError:

            pass


def thread_stop():  # 停止线程函数

    global run

    choose = pyautogui.confirm(text="请选择", title="选择框", buttons=["停止程序"])

    if choose == "停止程序":

        run = False


# 函数定义


# 打开记录

try:

    f = open(r"D:\Desktop\1.txt")

    start = int(f.read())

    start = start - 2

    f.close()

except Exception:

    pass

# 打开记录


# 处理名字

for i in range(len(list)):

    if list[i][len(list[i])-1] == "\n":

        list[i] = list[i][:len(list[i])-1]

# 处理名字


_thread.start_new_thread(thread_print, tuple())  # 启动输出线程


for i in range(start, len(list)):

    while not pan:

        sleep(1)

    n = list[i]

    p = "D:\\Desktop\\auto_4\\"+n+".png"

    if not os.path.isfile(p):

        _thread.start_new_thread(get_a_skin, (i,))


        f = open(r"D:\Desktop\1.txt", "w+")  # 保存记录

        print(i, file=f)

        f.close()


        sleep(1.1)

    else:

        print(gtime()+f"Main Theard:{n}的皮肤已存在")

    if not run:

        break

run = False

name_list.close()

log.close()


-----------------------

python皮肤文件获取脚本分享的评论 (共 条)

分享到微博请遵守国家法律