-
Notifications
You must be signed in to change notification settings - Fork 4
/
db_update.py
65 lines (52 loc) · 2.77 KB
/
db_update.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
from pywikisource import WikiSourceApi
import json
import datetime as dt
from pytz import timezone
from models import Contest, Book, IndexPage, Session, User
from dateutil import parser
def run():
session = Session()
contests = session.query(Contest).all()
user_agent = "IndicWikisourceContest/1.1 (Development; https://example.org/IndicWikisourceContest/;) pywikisource/0.0.5"
for contest in contests:
# print(contest.name)
if dt.datetime.today() > contest.end_date:
contest.status = False
if contest.status == False:
continue
elif contest.status == True:
ws = WikiSourceApi(contest.lang, user_agent)
books = session.query(Book).filter_by(cid=contest.cid).all()
for book in books:
try:
page_list = ws.createdPageList(book.name)
# print(page_list)
for page in page_list:
ipage = IndexPage(book_name=book.name, page_name=page)
response = ws.pageStatus(page)
# print(response)
if response['proofread'] is not None:
user = session.query(User).filter_by(user_name=response["proofread"]["user"], cid=contest.cid).first()
if not user:
user = User(user_name=response["proofread"]["user"], cid=contest.cid)
session.add(user)
ipage.proofreader_username = user.user_name
proofread_time = parser.parse(response["proofread"]["timestamp"])
ipage.proofread_time = proofread_time.strftime('%Y-%m-%d %H:%M:%S')
ipage.p_revision_id = response["proofread"]["revid"]
if response['validate'] is not None:
user = session.query(User).filter_by(user_name=response["validate"]["user"], cid=contest.cid).first()
if not user:
user = User(user_name=response["validate"]["user"], cid=contest.cid)
session.add(user)
ipage.validator_username = user.user_name
validate_time = parser.parse(response["validate"]["timestamp"])
ipage.validate_time = validate_time.strftime('%Y-%m-%d %H:%M:%S')
ipage.v_revision_id = response["validate"]["revid"]
session.add(ipage)
except Exception as e:
print(f"Error in {contest.name} contest: {e}")
session.commit()
session.close()
if __name__ == "__main__":
run()