Skip to content

Commit

Permalink
feat: add image download server option
Browse files Browse the repository at this point in the history
- 新增 后端图片下载服务器选项
- 修复 文件写出 BUG
- 优化 通过代理导致的 SSL 错误问题
- 完善 Token 引导文字描述
  • Loading branch information
txperl committed Apr 11, 2021
1 parent 2411915 commit b27b028
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 29 deletions.
4 changes: 3 additions & 1 deletion app/core/biu/login_token.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# thanks to @github/ZipFile, https://gist.github.com/ZipFile/c9ebedb224406f4f11845ab700124362
import platform
import requests
from base64 import urlsafe_b64encode
from hashlib import sha256
from secrets import token_urlsafe
from urllib.parse import urlencode

import requests
from requests_toolbelt.adapters import host_header_ssl

from ...lib.common.msg import biuMsg
Expand Down Expand Up @@ -58,6 +59,7 @@ def login(self, host=AUTH_TOKEN_URL_HOST, kw={}, newCode=False):
else:
self.msger.arr(
"[Login] 请按以下步骤进行操作:",
"注意: 程序每次启动时要求获取的 Code 都不同,不可复用之前获取到的,且 Code 不带有任何引号或等号",
f"1. 访问「{LOGIN_URL}?{urlencode(self.login_params)}」",
"(若您别无他法,还是不能访问以上网址,那可参考此方式「https://github.com/mashirozx/Pixiv-Nginx」先进行配置)",
"2. 打开浏览器的「开发者工具 / Dev Console / F12」,切换至「Network」标签",
Expand Down
56 changes: 32 additions & 24 deletions app/core/biu/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# coding=utf-8
import json
import os
import sys
import platform
import sys
import threading
from concurrent.futures import ThreadPoolExecutor

Expand All @@ -19,8 +19,8 @@
@CMDProcessor.core_register_auto("biu", {"config": "{ROOTPATH}config.yml"})
class core_module_biu(object):
def __init__(self, info=None):
self.ver = 200020
self.lowestConfVer = 3
self.ver = 200030
self.lowestConfVer = 4
self.place = "local"
self.sysPlc = platform.system()
self.apiType = "public"
Expand Down Expand Up @@ -56,6 +56,7 @@ def auto(self):
if self.apiType != "byPassSni":
self.__checkNetwork() # 检测网络是否可通
self.__login() # 登录
self.__setImageHost() # 设置图片服务器地址
self.__showRdyInfo() # 展示初始化完成信息
return self

Expand Down Expand Up @@ -102,7 +103,7 @@ def __getBiuInfo(self):
"""
try:
return json.loads(
requests.get("https://biu.tls.moe/d/biuinfo.json", timeout=6).text
requests.get("https://biu.tls.moe/d/biuinfo.json", timeout=6, verify=False).text
)
except:
return {"version": -1, "pApiURL": "public-api.secure.pixiv.net"}
Expand All @@ -125,10 +126,10 @@ def __checkNetwork(self):
try:
if self.proxy != "":
requests.get(
"https://pixiv.net/", proxies={"https": self.proxy}, timeout=6,
"https://pixiv.net/", proxies={"https": self.proxy}, timeout=10, verify=False
)
else:
requests.get("https://pixiv.net/", timeout=6)
requests.get("https://pixiv.net/", timeout=10, verify=False)
except:
self.msger.msg("无法访问 Pixiv,启用 byPassSni API")
self.apiType = "byPassSni"
Expand All @@ -155,19 +156,32 @@ def __login(self):
else:
self.__loginPublicAPI(**args)
except Exception as e:
token = 0
self.msger.msg("由于 Pixiv 禁止了目前使用的 Login API 账号密码登陆方式,暂时只能使用 Token 进行登录")
try:
self.msger.msg("由于 Pixiv 禁止了目前使用的 Login API 账号密码登陆方式,暂时只能使用 Token 进行登录")
if input("是否开始手动获取 Token 后使用? (y / n): ") != "y":
raise Exception("User Cancelled.")
raise Exception("用户取消操作。")
login = login_with_token()
try:
ip = login.get_host_ip(self.biuInfo["pApiURL"])
token = login.login(host=ip, newCode=True)
except Exception as te:
self.msger.error(te, header=False)
self.msger.red("免代理请求失败。开始尝试代理方式,请务必确保程序可通过您的设置访问 Pixiv")
proxy = login.get_proxy(self.proxy)
token = login.login(kw={"proxies": {"https": proxy}})
err = str(te)
if "'code': 918" in err:
self.msger.red(
"Code 错误。请注意程序每次启动时要求获取的 Code 都不同,不可复用之前获取到的,且 Code 不带有任何引号或等号。")
if input("是否立即重试? (y / n): ") != "y":
raise Exception("用户取消操作。")
elif "'code': 1508" in err:
self.msger.red(
"Code 已过期。请在手动进行 Token 获取操作时快一些即可。")
if input("是否立即重试? (y / n): ") != "y":
raise Exception("用户取消操作。")
else:
self.msger.error(err, header=False)
self.msger.red("免代理请求失败。开始尝试代理方式,请务必确保程序可通过您的设置访问 Pixiv")
proxy = login.get_proxy(self.proxy)
token = login.login(kw={"proxies": {"https": proxy}})
if self.sets["account"]["isToken"]:
ifile.aout(
self.ENVORON["ROOTPATH"] + "usr/.token.json",
Expand Down Expand Up @@ -266,12 +280,6 @@ def __loginAppAPI(self, username=None, password=None, token=None):

self.msger.msg(f"{self.apiType} API 登陆成功")

if self.apiType != "app":
try:
self.__getPximgTrueIP()
except:
self.msger.msg("Pixiv 图片服务器 IP 获取失败")

def __showRdyInfo(self):
"""
展示初始化成功消息。
Expand Down Expand Up @@ -372,14 +380,14 @@ def appWorksPurer(self, da):
}
da[i] = r

def __getPximgTrueIP(self):
def __setImageHost(self):
"""
获取 pixiv 图片服务器地址。
(现暂时直接返回第三方反代地址)
设置 pixiv 图片服务器地址。
"""
# 暂时
self.pximgURL = "https://i.pixiv.cat"
return
if self.sets["biu"]["download"]["imageHost"] != "":
self.pximgURL = self.sets["biu"]["download"]["imageHost"]
if self.apiType == "byPassSni":
self.pximgURL = "https://i.pixiv.cat"

def __clear(self):
if os.name == "nt":
Expand Down
8 changes: 5 additions & 3 deletions app/core/file/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ def ain(uri, mode="r"):
with open(uri, mode) as f:
fileType = uri.split(".")[-1]
if fileType == "json":
f = json.load(f)
r = json.load(f)
elif fileType == "yml" or fileType == "yaml":
f = yaml.safe_load(f)
r = yaml.safe_load(f)
else:
r = f.read()
except:
print("\033[31m[load@failed] %s\033[0m" % (uri))
return False
return f
return r

@staticmethod
def aout(uri, data, mode="w", dRename=True, msg=False):
Expand Down
9 changes: 8 additions & 1 deletion config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ account:
# 是否使用 token 登录

sys:
confVersion: 3
confVersion: 4
# 配置文件版本,请勿修改

host: "127.0.0.1:4001"
Expand Down Expand Up @@ -101,3 +101,10 @@ biu:
whatsUgoira: "webp"
# 将动图转换为指定格式
# 可选 webp, gif

imageHost: ""
# 下载时使用的 Pixiv 图片服务器
# 留空则程序自动判断
# 参考如下:
# https://i.pximg.net 官方图片服务器(需代理)
# https://i.pixiv.cat 第三方反代图片服务器(无需代理)

0 comments on commit b27b028

Please sign in to comment.