This repository has been archived by the owner on Nov 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
collector.py
executable file
Β·146 lines (120 loc) Β· 3.75 KB
/
collector.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
import argparse
import schedule
import re
import time
import sqlite3
import datetime
import cfscrape
import logging
from requests.adapters import HTTPAdapter
from urllib3.util import Retry
from random import randint
URL = "https://platesmania.com/aktivuserall?galak=0&start={}"
TOP_PAGES = 15
FORMAT = "%(asctime)s:%(levelname)s:%(module)s:%(message)s"
logging.basicConfig(level=logging.INFO, format=FORMAT)
logger = logging.getLogger("avstat")
retry_strategy = Retry(
total=10,
backoff_factor=2,
status_forcelist=[429, 500, 502, 503, 504],
)
def get_date():
date = datetime.datetime.now() + datetime.timedelta(days=-1)
date_str = datetime.datetime.strftime(date, "%Y-%m-%d")
return date_str
def parse_stat(page):
matches = re.findall(r"""
<tr>
<td>
<span>\d+</span>
</td>
<td>
<span><a\s+href=\'/user(\d+)\'>(.*?)</a></span>
</td>
<td>
<span>
<a\s+href=\'/gallery\.php\?usr=\d+\'>
([0-9\s]+)
</a>
</span>\s+(.*?)
</td>
</tr>
""", page, re.X)
retval = []
for match in matches:
badge = match[3]
added = 0
if badge:
result = re.search(r""">\+(\d+)<""", badge)
if result:
added = int(result.groups()[0])
retval.append({
"id": int(match[0]),
"name": match[1],
"total": int(match[2].replace(" ", "")),
"added": added
})
return retval
def add_record(db, user_id, name, total, added, date):
result = db.execute(
"SELECT id FROM users WHERE id = ?",
(user_id, ),
).fetchone()
if result is None:
db.execute("INSERT INTO users(id, name) VALUES(?,?)", (user_id, name))
result = db.execute(
"SELECT id FROM stat WHERE user_id = ? AND created = ? LIMIT 1",
(user_id, date),
).fetchone()
if not result:
db.execute(
"INSERT INTO stat(user_id, created, total, added) VALUES(?,?,?,?)",
(user_id, date, total, added),
)
else:
logger.warning(f"user_id={user_id} stat already exist")
def collect_stats():
retry_adapter = HTTPAdapter(max_retries=retry_strategy)
start_time = time.time()
db = sqlite3.connect("data/stat.db")
scraper = cfscrape.create_scraper()
scraper.mount("https://", retry_adapter)
scraper.mount("http://", retry_adapter)
date = get_date()
logger.info("previous date %s" % date)
for page in range(TOP_PAGES):
if page != 0:
time.sleep(randint(2, 10))
logger.info(f"loading page {page+1}")
response = scraper.get(URL.format(page)).text
stat = parse_stat(response)
for item in stat:
add_record(db, item["id"], item["name"], item["total"],
item["added"], date)
db.commit()
db.close()
logger.info("completed in %f seconds" % (time.time() - start_time))
def migrate():
db = sqlite3.connect("data/stat.db")
logger.info("apply migrations")
with open("schema.sql") as f:
db.executescript(f.read())
db.close()
def serve():
schedule.every().day.at("04:00").do(collect_stats)
migrate()
logger.info("started")
while True:
schedule.run_pending()
time.sleep(1)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("command", choices=["runonce", "serve"],
default="serve", const="serve", nargs="?")
args = parser.parse_args()
if args.command == "runonce":
collect_stats()
elif args.command == "serve":
serve()