forked from ONI-Wiki-zh/BotNotIncluded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
99 lines (80 loc) · 2.77 KB
/
main.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
import datetime
import os
import typing
import dateutil.parser
if 'GITHUB_ACTIONS' in os.environ:
with open('user-config.py', 'w') as f:
f.writelines([
"user_families_paths = ['site_families']\n",
f"usernames['oni']['zh'] = '{os.environ.get('BOT_NAME')}'\n",
"put_throttle = 0\n",
])
import pywikibot
import pywikibot.data.api
import utils
logger = utils.getLogger("ONI_ZH_Main")
def get_recent_pages(
site: pywikibot.Site,
recent_seconds: typing.Optional[int]) -> typing.List[pywikibot.Page]:
if not recent_seconds:
if "RC_IN_SECONDS" in os.environ:
recent_seconds = os.environ["RC_IN_SECONDS"]
recent_seconds = int(recent_seconds)
else:
recent_seconds = 7200
recent_page_ids = set()
recent_pages = []
curr_time = datetime.datetime.now(datetime.timezone.utc)
for record in site.recentchanges(bot=False, namespaces=[0, 4, 6, 12, 14]):
if record['type'] == 'log':
continue
record_time = dateutil.parser.isoparse(record['timestamp'])
from_now = (curr_time - record_time).total_seconds()
if from_now > recent_seconds:
break
if record['pageid'] not in recent_page_ids:
recent_pages.append(pywikibot.Page(site, title=record['title']))
recent_page_ids.add(record['pageid'])
return recent_pages
def main(recent_seconds: typing.Optional[int] = None):
import bot_format
import bot_update
import ImgHost.img_host as img_host
site = pywikibot.Site("zh", "oni")
retry = 0
while not site.logged_in() and retry < 5:
retry += 1
login_manager = pywikibot.data.api.LoginManager(
site=site,
user=f'{os.environ.get("BOT_NAME")}@GithubActions',
password=os.environ.get("BOT_PASS")
)
login_manager.login(retry=True)
site.login()
if not site.logged_in():
raise Exception("Not logged in")
pages = get_recent_pages(site, recent_seconds)
# Reformatting
logger.info("Start reformatting")
for p in pages:
logger.info(f"Processing {p.title()}")
try:
bot_format.format_page(p)
except Exception as e:
logger.warning(e)
if len(pages) == 0:
logger.info("No recent changes to reformat!")
# Update inter-lang status
logger.info("Start update inter-lang status")
oni_en = pywikibot.Site("en", "oni")
bot_update.bot_update(site, oni_en)
# Image host
logger.info("Start checking Image host")
img_host.download(site)
img_host.upload('ms')
img_host.create_css()
img_host.upload_css(site)
return pages
if __name__ == '__main__':
if os.environ.get('GITHUB_ACTIONS'):
main()