forked from soju6jan/lib_metadata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_util.py
executable file
·122 lines (107 loc) · 4.26 KB
/
server_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import json
from framework import SystemModelSetting, app # pylint: disable=import-error
from .plugin import P
from .site_util import SiteUtil
logger = P.logger
server_plugin_ddns = app.config["DEFINE"]["METADATA_SERVER_URL"]
try:
if SystemModelSetting.get("ddns") == server_plugin_ddns:
server_plugin_ddns = "http://127.0.0.1:19999"
except Exception:
pass
class MetadataServerUtil:
@classmethod
def imagehash_ok(cls) -> bool:
try:
import imagehash # pylint: disable=unused-import
return True
except ImportError:
return False
@classmethod
def thumb_ok(cls, thumb_url) -> bool:
if ".discordapp." not in thumb_url:
return False
if not SiteUtil.get_response(thumb_url, method="HEAD", timeout=30).ok:
return False
return True
@classmethod
def hangul_ok(cls, data) -> bool:
for key in ["tagline", "plot"]:
value = (data.get(key, "") or "").strip()
if value and not SiteUtil.is_include_hangul(value):
return False
return True
@classmethod
def get_metadata(cls, code):
try:
url = f"{app.config['DEFINE']['WEB_DIRECT_URL']}/meta/get_meta.php"
params = {"type": "meta", "code": code}
logger.info("서버로부터 메타데이터를 가져오는 중: %s", params)
data = SiteUtil.get_response(url, params=params, timeout=30).json()
if data["ret"] == "success":
return SiteUtil.discord_renew_urls(data["data"])
except Exception:
logger.exception("서버로부터 메타데이터를 가져오는 중 예외:")
return None
@classmethod
def set_metadata(cls, code, data, keyword):
try:
url = f"{server_plugin_ddns}/server/normal/metadata/set"
param = {
"code": code,
"data": json.dumps(data),
"user": SystemModelSetting.get("sjva_me_user_id"),
"keyword": keyword,
}
logger.debug("서버로 메타데이터 보내는 중: %s", param)
data = SiteUtil.get_response(url, post_data=param, timeout=30).json()
if data["ret"] == "success":
logger.info("메타데이터 '%s' 저장 성공. 감사합니다!", code)
except Exception:
logger.exception("서버로 메타데이터 보내는 중 예외:")
@classmethod
def set_metadata_jav_censored(cls, code, data, keyword):
try:
if not cls.imagehash_ok():
return
thumbs = data.get("thumb", [])
if code.startswith("C") and len(thumbs) < 2:
# censored dvd
return
if code.startswith("D") and len(thumbs) < 1:
# censored ama
return
for thumb in thumbs:
if not cls.thumb_ok(thumb.get("value", "")):
return
if not cls.hangul_ok(data):
return
except Exception:
logger.exception("보낼 메타데이터 확인 중 예외:")
else:
cls.set_metadata(code, data, keyword)
@classmethod
def set_metadata_jav_uncensored(cls, code, data, keyword):
try:
thumbs = data.get("thumb", [])
for thumb in thumbs:
if not cls.thumb_ok(thumb.get("value", "")):
return
if not cls.hangul_ok(data):
return
except Exception:
logger.exception("보낼 메타데이터 확인 중 예외:")
else:
cls.set_metadata(code, data, keyword)
@classmethod
def get_meta_extra(cls, code):
try:
url = f"{app.config['DEFINE']['WEB_DIRECT_URL']}/meta/get_meta.php"
params = {"type": "extra", "code": code}
logger.info("서버로부터 메타데이터를 가져오는 중: %s", params)
data = SiteUtil.get_response(url, params=params, timeout=30).json()
if data["ret"] == "success":
return SiteUtil.discord_renew_urls(data["data"])
except Exception:
logger.exception("서버로부터 메타데이터를 가져오는 중 예외:")
return None