-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest_logic.py
More file actions
69 lines (56 loc) · 1.69 KB
/
request_logic.py
File metadata and controls
69 lines (56 loc) · 1.69 KB
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
import requests
import json
import sqlite3
import datetime
from db import Edit, DB, LastRun
URL = "https://www.cubawiki.com.ar/api.php"
PROPS = "userid|user|title|ids|comment|timestamp"
WEEKI_START = "2025-08-22T03:00:00"
def get_recent_edit(con, weeky_start):
params = {
"action": "query",
"list": "recentchanges",
"format": "json",
"rcprop": PROPS,
"rctype": "edit",
"rcend": weeky_start
}
if con:
params.update(con)
req = requests.get(URL, params)
return json.loads(req.content)
def get_recent_edits(con, weeky_start):
while True:
# print(f"Getting edits with {con}")
res = get_recent_edit(con, weeky_start)
changes = res["query"]["recentchanges"]
# print(f"Got {len(changes)} edits!!")
yield(changes)
con = res.get("continue", None)
if not con:
return
db = DB("CubaWeeki.db")
db.make_tables_if_not_exists()
def get_db_edits():
return db.all(Edit)
def do_fetch_run():
last = db.get_last(LastRun)
timestamp = last.timestamp if last else WEEKI_START
WEEKY_START = datetime.datetime.fromisoformat(timestamp) + datetime.timedelta(seconds=1)
print(f"fetching edits since {WEEKY_START}")
res = []
for e in get_recent_edits(None, WEEKY_START):
res.extend(e)
edits = []
if res:
for r in res:
edit = Edit(**r)
db.add(edit)
edits.append(edit)
last = LastRun(timestamp=res[0]["timestamp"])
print(f"added {len(res)} edits, saving last run: {last.timestamp}")
db.add(last)
db.commit()
return edits
if __name__ == "__main__":
do_fetch_run()