Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0d79c18

Browse files
ready to release
1 parent 0902f43 commit 0d79c18

File tree

7 files changed

+92
-23
lines changed

7 files changed

+92
-23
lines changed

‎Dockerfile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ FROM python:3.8-alpine
33
RUN apk update && apk add --no-cache tzdata ca-certificates
44
COPY requirements.txt /requirements.txt
55
RUN pip3 install --no-cache-dir -r /requirements.txt && rm /requirements.txt
6-
COPY ./yyetsbot /YYeTsBot/yyetsbot
6+
COPY . /YYeTsBot
77

88
ENV TZ=Asia/Shanghai
99
WORKDIR /YYeTsBot/yyetsbot

‎requirements.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ beautifulsoup4
44
tgbot-ping
55
redis
66
apscheduler
7-
pymongo
7+
pymongo
8+
tornado

‎web/index.html‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ <h2>在 <a style="text-decoration: none;color: deepskyblue" href="https://t.me/y
6262
<h2>大家都在看......</h2>
6363
</div>
6464
<h2>有问题联系 <a style="text-decoration: none;color: green" href="https://t.me/BennyThink">Benny小可爱</a></h2>
65-
<img id="ferris" alt="a happy crab is wearing a cowboy hat and holding a lasso. 200 success."
66-
src="img/200-wrangler-ferris.gif">
65+
6766
</div>
6867
</body>
6968
<script src="js/axios.min.js"></script>
File renamed without changes.

‎web/prepare/dump_kv.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/local/bin/python3
2+
# coding: utf-8
3+
4+
# YYeTsBot - dump_kv.py
5+
# 2/6/21 18:12
6+
#
7+
8+
__author__ = "Benny <benny.think@gmail.com>"
9+
10+
import threading
11+
import requests
12+
import json
13+
from concurrent.futures.thread import ThreadPoolExecutor
14+
15+
s = requests.Session()
16+
17+
with open("index.json", ) as f:
18+
ids = json.load(f)
19+
20+
chunk = [ids[x:x + 3000] for x in range(0, len(ids), 3000)]
21+
22+
23+
def download(c):
24+
print("running batch ", c[0])
25+
for i in c:
26+
data = s.get("https://yyets.dmesg.app/id={}".format(i)).json()
27+
with open(f"{i}.json", "w") as f:
28+
json.dump(data, f)
29+
30+
31+
if __name__ == '__main__':
32+
threads = []
33+
for part in chunk:
34+
# Create 9 threads counting 10-19, 20-29, ... 90-99.
35+
thread = threading.Thread(target=download, args=(part,))
36+
threads.append(thread)
37+
38+
# Start them all
39+
for thread in threads:
40+
thread.start()
41+
42+
# Wait for all to complete
43+
for thread in threads:
44+
thread.join()

‎web/prepare/load_from_kv.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/local/bin/python3
2+
# coding: utf-8
3+
4+
# YYeTsBot - load_from_kv.py
5+
# 2/6/21 18:27
6+
#
7+
8+
__author__ = "Benny <benny.think@gmail.com>"
9+
10+
import pymongo
11+
import json
12+
import os
13+
14+
mongo_client = pymongo.MongoClient()
15+
16+
data_files = [i for i in os.listdir("data/") if i.endswith(".json")]
17+
col = mongo_client["zimuzu"]["yyets"]
18+
for data_file in data_files:
19+
with open(os.path.join("data", data_file)) as f:
20+
d = json.load(f)
21+
views = int(d["data"]["info"]["views"])
22+
d["data"]["info"]["views"] = views
23+
col.insert_one(d)

‎web/server.py‎

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77

88
__author__ = "Benny <benny.think@gmail.com>"
99

10-
import json
11-
import socket
12-
from platform import uname
1310
import os
1411
import contextlib
1512
from http import HTTPStatus
@@ -20,11 +17,22 @@
2017
from tornado.concurrent import run_on_executor
2118

2219
enable_pretty_logging()
23-
client = pymongo.MongoClient()
24-
db = client["zimuzu"]
20+
21+
mongo_host = os.getenv("mongo") or "localhost"
22+
23+
24+
class Mongo:
25+
def __init__(self):
26+
self.client = pymongo.MongoClient(host=mongo_host, connect=False)
27+
self.db = self.client["zimuzu"]
28+
29+
def __del__(self):
30+
self.client.close()
2531

2632

2733
class BaseHandler(web.RequestHandler):
34+
mongo = Mongo()
35+
2836
def data_received(self, chunk):
2937
pass
3038

@@ -45,7 +53,7 @@ def get_resource_data(self):
4553
param = self.get_query_argument("id")
4654
with contextlib.suppress(ValueError):
4755
param = int(param)
48-
data = db["yyets"].find_one_and_update(
56+
data = self.mongo.db["yyets"].find_one_and_update(
4957
{"data.info.id": param},
5058
{'$inc': {'data.info.views': 1}},
5159
{'_id': False})
@@ -57,7 +65,7 @@ def search_resource(self):
5765
projection = {'_id': False,
5866
'data.info': True,
5967
}
60-
data = db["yyets"].find({
68+
data = self.mongo.db["yyets"].find({
6169
"$or": [
6270
{"data.info.cnname": {'$regex': f'.*{param}.*'}},
6371
{"data.info.enname": {'$regex': f'.*{param}.*'}},
@@ -88,7 +96,7 @@ def get_top_resource(self):
8896
'data.info': True,
8997
}
9098
if top_type == "all":
91-
data = db["yyets"].find({}, projection).sort("data.info.views", pymongo.DESCENDING).limit(10)
99+
data = self.mongo.db["yyets"].find({}, projection).sort("data.info.views", pymongo.DESCENDING).limit(10)
92100
else:
93101
data = []
94102
return dict(data=list(data))
@@ -105,7 +113,7 @@ class MetricsHandler(BaseHandler):
105113
@run_on_executor()
106114
def set_metrics(self):
107115
metrics_name = self.get_query_argument("type", "access")
108-
db['metrics'].find_one_and_update(
116+
self.mongo.db['metrics'].find_one_and_update(
109117
{'type': metrics_name}, {'$inc': {'count': 1}}
110118
)
111119
self.set_status(HTTPStatus.CREATED)
@@ -114,7 +122,7 @@ def set_metrics(self):
114122
@run_on_executor()
115123
def get_metrics(self):
116124
metrics_name = self.get_query_argument("type", "access")
117-
return db['metrics'].find_one({'type': metrics_name}, {'_id': False})
125+
return self.mongo.db['metrics'].find_one({'type': metrics_name}, {'_id': False})
118126

119127
@gen.coroutine
120128
def get(self):
@@ -138,23 +146,17 @@ class RunServer:
138146
(r'/(.*\.html|.*\.js|.*\.css|.*\.png|.*\.jpg|.*\.ico|.*\.gif|.*\.woff2)', web.StaticFileHandler,
139147
{'path': static_path}),
140148
]
141-
settings = {
142-
"cookie_secret": "5Li05DtnQewDZq1mDVB3HAAhFqUu2vD2USnqezkeu+M=",
143-
"xsrf_cookies": False,
144-
"autoreload": True,
145-
# 'template_path': '.',
146-
}
147149

148-
application = web.Application(handlers)
150+
application = web.Application(handlers, xheaders=True)
149151

150152
@staticmethod
151153
def run_server(port, host, **kwargs):
152154
tornado_server = httpserver.HTTPServer(RunServer.application, **kwargs)
153155
tornado_server.bind(port, host)
154-
tornado_server.start()
156+
tornado_server.start(0)
155157

156158
try:
157-
print('Server is running on http://{}:{}'.format("127.0.0.1", port))
159+
print('Server is running on http://{}:{}'.format(host, port))
158160
ioloop.IOLoop.instance().current().start()
159161
except KeyboardInterrupt:
160162
ioloop.IOLoop.instance().stop()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /