-
Notifications
You must be signed in to change notification settings - Fork 1
/
relicicons.py
executable file
·77 lines (65 loc) · 2.28 KB
/
relicicons.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# (C) Drono, 2021
#
from __future__ import absolute_import
import pywikibot
import os
import hashlib
class RelicIconEditor(object):
def __init__(self, *args):
self.site = pywikibot.Site()
self.edited = 0
self.comment = "Drono-bot: Upload icon"
self.file = "./scripts/userscripts/lotrowiki/icons"
self.iconDir = "./scripts/userscripts/data/lotro-relics/relicIcons/"
if not os.path.isfile(self.file):
pywikibot.error('File "'+self.file+'" does not exist!')
sys.exit(1)
if not os.path.isdir(self.iconDir):
pywikibot.error('Directory for icon files "'+self.iconDir+
'" does not exists!')
sys.exit(1)
def edit_page(self, page, filename):
try:
pagename = page.title(underscore=True, with_ns=False)
page.download()
if hashfile(pagename) == hashfile(filename):
pywikibot.output("No difference.")
os.remove(pagename)
return
os.remove(pagename)
except pywikibot.exceptions.NoPageError:
pywikibot.output("Page does not exists.")
result = page.upload(source=filename,
ignore_warnings="exists",
comment=self.comment)
if result:
pywikibot.output("File "+pagename+" uploaded seccesfuly")
self.edited += 1
else:
pywikibot.output("Error uploading "+filename)
def run(self):
self.site.login()
with open(self.file) as f:
for line in f:
icon_id, icon_name = line.strip().split(" ",1)
page_name = icon_name+".png"
page = pywikibot.FilePage(self.site, page_name)
self.edit_page(page, self.iconDir+icon_id)
pywikibot.output('Changed '+str(self.edited)+' icons.')
def hashfile(path, blocksize = 65536):
afile = open(path, 'rb')
hasher = hashlib.md5()
buf = afile.read(blocksize)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(blocksize)
afile.close()
return hasher.hexdigest()
def main(*args):
app = RelicIconEditor(*args)
app.run()
if __name__ == '__main__':
main()