-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
99 lines (84 loc) · 3.21 KB
/
run.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
import os
import sys
import uvicorn
import logging
import configupdater
from uvicorn import Config, Server
from loguru import logger
LOG_LEVEL = logging.getLevelName(os.environ.get("LOG_LEVEL", "INFO"))
def read_config():
global host, port
# read config file
config = configupdater.ConfigUpdater()
if not os.path.exists('config.ini'):
# Create config file
with open('config.ini', 'w', encoding='utf-8'):
pass
config.read('config.ini', encoding='utf-8')
if not config.has_section("PixivCrawler"):
config.append("\n")
config.add_section("PixivCrawler")
if config.has_option("PixivCrawler", "host"):
host = config["PixivCrawler"]["host"].value
else:
host = "0.0.0.0"
logger.warning("host invalid, using default: " + host)
config.set("PixivCrawler", "host", host)
if config.has_option("PixivCrawler", "port"):
port = int(config["PixivCrawler"]["port"].value)
else:
port = 8000
logger.warning("port invalid, using default: " + str(port))
config.set("PixivCrawler", "port", port)
# save config
with open('config.ini', 'w', encoding='utf-8') as configfile:
config.write(configfile)
logger.info("PixivCrawler config loaded")
class InterceptHandler(logging.Handler):
def emit(self, record):
# Get corresponding Loguru level if it exists
try:
level = logger.level(record.levelname).name
except ValueError:
level = record.levelno
# Find caller from where originated the logged message
frame, depth = logging.currentframe(), 2
while frame.f_code.co_filename == logging.__file__:
frame = frame.f_back
depth += 1
logger.opt(depth=depth, exception=record.exc_info).log(
level, record.getMessage())
def setup_logging():
# intercept everything at the root logger
logging.root.handlers = [InterceptHandler()]
logging.root.setLevel(LOG_LEVEL)
# remove every other logger's handlers
# and propagate to root logger
for name in logging.root.manager.loggerDict.keys():
logging.getLogger(name).handlers = []
logging.getLogger(name).propagate = True
# configure loguru
logger.configure(handlers=[{"sink": sys.stdout,
"format": "<green>{time:YYYY-MM-dd HH:mm:ss}</> | <yellow>{level: <8}</> | {message}", "colorize": True}])
if __name__ == "__main__":
log_config = uvicorn.config.LOGGING_CONFIG
log_config["formatters"]["access"][
"fmt"] = "\x1b[0;32m%(asctime)s\u001b[0m | \u001b[33m%(levelname)-8s\u001b[0m | %(message)s"
log_config["formatters"]["access"]["datefmt"] = "%Y-%m-%d %H:%M:%S"
log_config["formatters"]["default"][
"fmt"] = "\x1b[0;32m%(asctime)s\u001b[0m | \u001b[33m%(levelname)-8s\u001b[0m | %(message)s"
log_config["formatters"]["default"]["datefmt"] = "%Y-%m-%d %H:%M:%S"
setup_logging()
print("---Starting PixivCrawler API---")
read_config()
server = Server(
Config(
"pixiv_crawler_api:app",
host=host,
port=port,
log_level=3,
log_config=log_config,
),
)
setup_logging()
server.run()