2021-02-06 08:58:58 +08:00
|
|
|
#!/usr/local/bin/python3
|
|
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
|
|
# YYeTsBot - server.py
|
|
|
|
|
# 2/5/21 21:02
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
__author__ = "Benny <benny.think@gmail.com>"
|
|
|
|
|
|
2021-02-07 10:33:12 +08:00
|
|
|
import logging
|
2021-07-11 15:50:52 +08:00
|
|
|
import os
|
2022-02-06 18:51:41 +08:00
|
|
|
import pathlib
|
2022-12-31 14:33:20 +01:00
|
|
|
import threading
|
2023-03-19 17:41:19 +01:00
|
|
|
from zoneinfo import ZoneInfo
|
2022-02-22 18:54:02 +08:00
|
|
|
|
2021-09-23 18:24:24 +08:00
|
|
|
import tornado.autoreload
|
2021-04-15 20:15:59 +08:00
|
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
2022-02-22 18:54:02 +08:00
|
|
|
from apscheduler.triggers.cron import CronTrigger
|
2021-07-11 15:50:52 +08:00
|
|
|
from tornado import httpserver, ioloop, options, web
|
2021-02-06 08:58:58 +08:00
|
|
|
from tornado.log import enable_pretty_logging
|
2021-06-17 10:39:47 +08:00
|
|
|
|
2023-03-19 17:35:39 +01:00
|
|
|
from common.dump_db import entry_dump
|
|
|
|
|
from common.sync import YYSub, sync_douban
|
2023-03-20 19:12:25 +01:00
|
|
|
from common.utils import Cloudflare, setup_logger
|
2023-03-19 17:35:39 +01:00
|
|
|
from databases.base import SearchEngine
|
|
|
|
|
from databases.other import Other
|
|
|
|
|
from handlers.base import IndexHandler, NotFoundHandler
|
|
|
|
|
from handlers.comment import (
|
2023-03-10 23:59:40 +01:00
|
|
|
CommentChildHandler,
|
|
|
|
|
CommentHandler,
|
|
|
|
|
CommentNewestHandler,
|
|
|
|
|
CommentReactionHandler,
|
2023-04-25 21:52:53 +02:00
|
|
|
CommentSearchHandler,
|
2023-03-19 17:35:39 +01:00
|
|
|
NotificationHandler,
|
|
|
|
|
)
|
|
|
|
|
from handlers.douban import DoubanHandler, DoubanReportHandler
|
|
|
|
|
from handlers.grafana import (
|
2023-03-10 23:59:40 +01:00
|
|
|
GrafanaIndexHandler,
|
|
|
|
|
GrafanaQueryHandler,
|
|
|
|
|
GrafanaSearchHandler,
|
|
|
|
|
MetricsHandler,
|
2023-03-19 17:35:39 +01:00
|
|
|
)
|
|
|
|
|
from handlers.oauth import (
|
|
|
|
|
FacebookAuth2LoginHandler,
|
|
|
|
|
GitHubOAuth2LoginHandler,
|
|
|
|
|
GoogleOAuth2LoginHandler,
|
2023-03-10 23:59:40 +01:00
|
|
|
MSOAuth2LoginHandler,
|
2023-03-19 17:35:39 +01:00
|
|
|
TwitterOAuth2LoginHandler,
|
|
|
|
|
)
|
|
|
|
|
from handlers.other import (
|
|
|
|
|
AnnouncementHandler,
|
|
|
|
|
BlacklistHandler,
|
|
|
|
|
CaptchaHandler,
|
|
|
|
|
CategoryHandler,
|
|
|
|
|
DBDumpHandler,
|
|
|
|
|
SpamProcessHandler,
|
|
|
|
|
)
|
|
|
|
|
from handlers.resources import (
|
2023-04-25 21:52:53 +02:00
|
|
|
AdsenseStatusHandler,
|
2023-03-10 23:59:40 +01:00
|
|
|
NameHandler,
|
|
|
|
|
ResourceHandler,
|
|
|
|
|
ResourceLatestHandler,
|
|
|
|
|
TopHandler,
|
|
|
|
|
)
|
2023-03-19 17:35:39 +01:00
|
|
|
from handlers.user import LikeHandler, UserAvatarHandler, UserEmailHandler, UserHandler
|
2021-02-06 08:58:58 +08:00
|
|
|
|
|
|
|
|
enable_pretty_logging()
|
2023-03-06 22:16:00 +01:00
|
|
|
setup_logger()
|
2023-03-19 17:35:39 +01:00
|
|
|
|
2021-02-10 10:01:35 +08:00
|
|
|
if os.getenv("debug"):
|
2023-03-06 22:16:00 +01:00
|
|
|
logging.getLogger().setLevel(logging.DEBUG)
|
2021-02-06 19:58:40 +08:00
|
|
|
|
2021-06-11 17:08:30 +08:00
|
|
|
|
2021-02-06 08:58:58 +08:00
|
|
|
class RunServer:
|
2023-03-10 23:59:40 +01:00
|
|
|
static_path = pathlib.Path(__file__).parent.joinpath("templates")
|
2021-02-06 08:58:58 +08:00
|
|
|
handlers = [
|
2023-03-10 23:59:40 +01:00
|
|
|
(r"/", IndexHandler),
|
|
|
|
|
(r"/api/resource", ResourceHandler),
|
|
|
|
|
(r"/api/resource/latest", ResourceLatestHandler),
|
|
|
|
|
(r"/api/top", TopHandler),
|
|
|
|
|
(r"/api/like", LikeHandler),
|
|
|
|
|
(r"/api/user", UserHandler),
|
|
|
|
|
(r"/api/user/avatar/?(.*)", UserAvatarHandler),
|
|
|
|
|
(r"/api/user/email", UserEmailHandler),
|
|
|
|
|
(r"/api/name", NameHandler),
|
2023-03-26 21:11:52 +02:00
|
|
|
(r"/api/adsense", AdsenseStatusHandler),
|
2023-03-10 23:59:40 +01:00
|
|
|
(r"/api/comment", CommentHandler),
|
2023-04-24 18:45:12 +02:00
|
|
|
(r"/api/comment/search", CommentSearchHandler),
|
2023-03-10 23:59:40 +01:00
|
|
|
(r"/api/comment/reaction", CommentReactionHandler),
|
|
|
|
|
(r"/api/comment/child", CommentChildHandler),
|
|
|
|
|
(r"/api/comment/newest", CommentNewestHandler),
|
|
|
|
|
(r"/api/captcha", CaptchaHandler),
|
|
|
|
|
(r"/api/metrics", MetricsHandler),
|
|
|
|
|
(r"/api/grafana/", GrafanaIndexHandler),
|
|
|
|
|
(r"/api/grafana/search", GrafanaSearchHandler),
|
|
|
|
|
(r"/api/grafana/query", GrafanaQueryHandler),
|
|
|
|
|
(r"/api/blacklist", BlacklistHandler),
|
|
|
|
|
(r"/api/db_dump", DBDumpHandler),
|
|
|
|
|
(r"/api/announcement", AnnouncementHandler),
|
|
|
|
|
(r"/api/douban", DoubanHandler),
|
|
|
|
|
(r"/api/douban/report", DoubanReportHandler),
|
|
|
|
|
(r"/api/notification", NotificationHandler),
|
|
|
|
|
(r"/api/category", CategoryHandler),
|
|
|
|
|
(r"/api/admin/spam", SpamProcessHandler),
|
|
|
|
|
(r"/auth/github", GitHubOAuth2LoginHandler),
|
|
|
|
|
(r"/auth/google", GoogleOAuth2LoginHandler),
|
|
|
|
|
(r"/auth/twitter", TwitterOAuth2LoginHandler),
|
|
|
|
|
(r"/auth/microsoft", MSOAuth2LoginHandler),
|
|
|
|
|
(r"/auth/facebook", FacebookAuth2LoginHandler),
|
|
|
|
|
(
|
2023-03-19 22:53:00 +01:00
|
|
|
r"/(.*\.html|.*\.js|.*\.css|.*\.png|.*\.jpg|.*\.ico|.*\.gif|.*\.woff2|.*\.gz|.*\.zip|"
|
|
|
|
|
r".*\.svg|.*\.json|.*\.txt)",
|
2023-03-10 23:59:40 +01:00
|
|
|
web.StaticFileHandler,
|
|
|
|
|
{"path": static_path},
|
|
|
|
|
),
|
2021-02-06 08:58:58 +08:00
|
|
|
]
|
2021-04-04 14:08:32 +08:00
|
|
|
settings = {
|
2021-04-14 21:21:31 +08:00
|
|
|
"cookie_secret": os.getenv("cookie_secret", "eo2kcgpKwXj8Q3PKYj6nIL1J4j3b58DX"),
|
2021-06-15 14:34:52 +08:00
|
|
|
"default_handler_class": NotFoundHandler,
|
|
|
|
|
"login_url": "/login",
|
2023-03-19 17:35:39 +01:00
|
|
|
"google_oauth": {
|
|
|
|
|
"key": os.getenv("GOOGLE_CLIENT_ID"),
|
|
|
|
|
"secret": os.getenv("GOOGLE_CLIENT_SECRET"),
|
|
|
|
|
},
|
|
|
|
|
"github_oauth": {
|
|
|
|
|
"key": os.getenv("GITHUB_CLIENT_ID"),
|
|
|
|
|
"secret": os.getenv("GITHUB_CLIENT_SECRET"),
|
|
|
|
|
},
|
|
|
|
|
"ms_oauth": {
|
|
|
|
|
"key": os.getenv("MS_CLIENT_ID"),
|
|
|
|
|
"secret": os.getenv("MS_CLIENT_SECRET"),
|
|
|
|
|
},
|
|
|
|
|
"fb_oauth": {
|
|
|
|
|
"key": os.getenv("FB_CLIENT_ID"),
|
|
|
|
|
"secret": os.getenv("FB_CLIENT_SECRET"),
|
|
|
|
|
},
|
2023-02-09 19:06:56 +01:00
|
|
|
"twitter_consumer_key": os.getenv("TWITTER_CONSUMER_KEY"),
|
|
|
|
|
"twitter_consumer_secret": os.getenv("TWITTER_CONSUMER_SECRET"),
|
2021-04-04 14:08:32 +08:00
|
|
|
}
|
2021-04-14 21:21:31 +08:00
|
|
|
application = web.Application(handlers, **settings)
|
2021-02-06 08:58:58 +08:00
|
|
|
|
|
|
|
|
@staticmethod
|
2021-04-14 21:21:31 +08:00
|
|
|
def run_server(port, host):
|
|
|
|
|
tornado_server = httpserver.HTTPServer(RunServer.application, xheaders=True)
|
2021-02-06 08:58:58 +08:00
|
|
|
tornado_server.bind(port, host)
|
2023-03-10 23:59:40 +01:00
|
|
|
if os.getenv("PYTHON_DEV"):
|
2021-06-17 17:28:20 +08:00
|
|
|
tornado_server.start(1)
|
2021-09-23 18:24:24 +08:00
|
|
|
tornado.autoreload.start()
|
2021-06-17 17:28:20 +08:00
|
|
|
else:
|
|
|
|
|
tornado_server.start(0)
|
2021-02-06 08:58:58 +08:00
|
|
|
|
|
|
|
|
try:
|
2023-03-10 23:59:40 +01:00
|
|
|
print("Server is running on http://{}:{}".format(host, port))
|
2021-02-06 08:58:58 +08:00
|
|
|
ioloop.IOLoop.instance().current().start()
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
ioloop.IOLoop.instance().stop()
|
|
|
|
|
print('"Ctrl+C" received, exiting.\n')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-03-19 17:41:19 +01:00
|
|
|
timez = ZoneInfo("Asia/Shanghai")
|
2021-06-17 17:28:20 +08:00
|
|
|
scheduler = BackgroundScheduler(timezone=timez)
|
2023-03-19 17:35:39 +01:00
|
|
|
scheduler.add_job(Other().reset_top, trigger=CronTrigger.from_crontab("0 0 1 * *"))
|
2022-02-22 18:54:02 +08:00
|
|
|
scheduler.add_job(sync_douban, trigger=CronTrigger.from_crontab("1 1 1 * *"))
|
2023-03-06 22:16:00 +01:00
|
|
|
scheduler.add_job(entry_dump, trigger=CronTrigger.from_crontab("2 2 1 * *"))
|
2023-03-19 17:35:39 +01:00
|
|
|
scheduler.add_job(Other().import_ban_user, "interval", seconds=300)
|
2023-04-26 21:48:32 +02:00
|
|
|
scheduler.add_job(Other().fill_user_hash, "interval", seconds=60)
|
2023-03-24 18:38:34 +01:00
|
|
|
scheduler.add_job(Cloudflare().clear_fw, trigger=CronTrigger.from_crontab("0 0 */3 * *"))
|
2023-03-05 17:07:23 +01:00
|
|
|
scheduler.add_job(YYSub().run, trigger=CronTrigger.from_crontab("0 1 * * *"))
|
|
|
|
|
|
2021-02-07 09:32:23 +08:00
|
|
|
scheduler.start()
|
2023-03-11 13:32:18 +01:00
|
|
|
logging.info("Dumping database and ingesting data for Meilisearh...")
|
2023-01-31 19:54:09 +01:00
|
|
|
if not os.getenv("PYTHON_DEV"):
|
2023-03-06 22:16:00 +01:00
|
|
|
threading.Thread(target=entry_dump).start()
|
2023-03-10 23:59:40 +01:00
|
|
|
# meilisearch tasks
|
2023-03-11 13:18:47 +01:00
|
|
|
if os.getenv("MEILISEARCH"):
|
|
|
|
|
logging.info("%s Searching with Meilisearch. %s", "#" * 10, "#" * 10)
|
2023-03-19 17:41:19 +01:00
|
|
|
engine = SearchEngine()
|
2023-03-11 12:24:16 +01:00
|
|
|
threading.Thread(target=engine.run_import).start()
|
|
|
|
|
threading.Thread(target=engine.monitor_yyets).start()
|
|
|
|
|
threading.Thread(target=engine.monitor_douban).start()
|
|
|
|
|
threading.Thread(target=engine.monitor_comment).start()
|
2022-04-10 13:59:00 +08:00
|
|
|
|
2021-02-06 08:58:58 +08:00
|
|
|
options.define("p", default=8888, help="running port", type=int)
|
2023-03-10 23:59:40 +01:00
|
|
|
options.define("h", default="127.0.0.1", help="listen address", type=str)
|
2021-02-06 08:58:58 +08:00
|
|
|
options.parse_command_line()
|
|
|
|
|
p = options.options.p
|
|
|
|
|
h = options.options.h
|
2021-06-17 12:37:37 +08:00
|
|
|
banner = """
|
|
|
|
|
▌ ▌ ▌ ▌ ▀▛▘
|
|
|
|
|
▝▞ ▝▞ ▞▀▖ ▌ ▞▀▘
|
|
|
|
|
▌ ▌ ▛▀ ▌ ▝▀▖
|
|
|
|
|
▘ ▘ ▝▀▘ ▘ ▀▀
|
|
|
|
|
|
2023-12-29 17:38:21 +01:00
|
|
|
Lazarus came back from the dead. By @BennyThink
|
2021-06-17 12:37:37 +08:00
|
|
|
"""
|
|
|
|
|
print(banner)
|
2021-02-06 08:58:58 +08:00
|
|
|
RunServer.run_server(port=p, host=h)
|