-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathartnosml.py
257 lines (234 loc) · 9.8 KB
/
artnosml.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
import re
import sys
import pywikibot
import codecs
import urllib.parse
from datetime import datetime
from time import strftime
i18 = {
'pl': {
'head': 'pl.wikipedia - Ostatnie nowe artykuły - tools.wikimedia.pl',
'heading': 'Ostatnie nowe artykuły',
'line1': 'Strona przedstawia numerację ostatnio utworzonych 100 artykułów z głównej przestrzeni nazw.',
'legend': 'Rodzaj: <b>A</b> (artykuł), <b>R</b> (przekierowanie), <b>M</b> (przeniesiony)',
'lastupdate': 'Ostatnia aktualizacja: ',
'refresh': 'Strona uaktualniana co 5 minut. Wyświetlanie od najnowszych.',
'hnumber': 'Numer artykułu',
'htype': 'Rodzaj',
'hdate': 'Data',
'htime': 'Czas',
'htitle': 'Tytuł',
'htarget': 'Cel'},
'tr': {
'head': 'tr.wikipedia - Madde sayacı - son 100 madde',
'heading': 'Son yeni maddeler',
'line1': u"Sayfa, belirtilen dil sürümündeki son 100 makalenin makale ID'lerini gösterir",
'legend': 'Tür: <b>A</b> (madde), <b>R</b> (yönlendirme)',
'lastupdate': 'Son güncellenme: ',
'refresh': 'Sayfa 5 dakikada bir yenilenir. En yeni, en başta gösterilir.',
'hnumber': 'Madde no',
'htype': 'Tür',
'hdate': 'Tarih',
'htime': 'Saat',
'htitle': 'Başlık',
'htarget': 'Hedef'},
'szl': {
'head': 'szl.wikipedia - Ostatnie nowe artykuły - tools.wikimedia.pl',
'heading': 'Ostatnie nowe artykuły',
'line1': 'Strona przedstawia numerację ostatnio utworzonych 100 artykułów z głównej przestrzeni nazw.',
'legend': 'Rodzaj: <b>A</b> (artykuł), <b>R</b> (przekierowanie), <b>M</b> (przeniesiony)',
'lastupdate': 'Ostatnia aktualizacja: ',
'refresh': 'Strona uaktualniana co 5 minut. Wyświetlanie od najnowszych.',
'hnumber': 'Numer artykułu',
'htype': 'Rodzaj',
'hdate': 'Data',
'htime': 'Czas',
'htitle': 'Tytuł',
'htarget': 'Cel'},
'csb': {
'head': 'csb.wikipedia - Ostatnie nowe artykuły - tools.wikimedia.pl',
'heading': 'Ostatnie nowe artykuły',
'line1': 'Strona przedstawia numerację ostatnio utworzonych 100 artykułów z głównej przestrzeni nazw.',
'legend': 'Rodzaj: <b>A</b> (artykuł), <b>R</b> (przekierowanie), <b>M</b> (przeniesiony)',
'lastupdate': 'Ostatnia aktualizacja: ',
'refresh': 'Strona uaktualniana co 5 minut. Wyświetlanie od najnowszych.',
'hnumber': 'Numer artykułu',
'htype': 'Rodzaj',
'hdate': 'Data',
'htime': 'Czas',
'htitle': 'Tytuł',
'htarget': 'Cel'}
}
def header(lang):
# generate html file header
header = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n'
header += '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" lang="pl" dir="ltr">\n'
header += ' <head>\n'
header += ' <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />\n'
header += ' <meta http-equiv="refresh" content="300">\n'
header += ' <title>' + i18[lang]['head'] + '</title>\n'
header += ' <link rel="stylesheet" type="text/css" href="/~masti/modern.css" />\n'
header += ' </head>\n'
header += '<body>\n'
header += '\n'
header += ' <!-- heading -->\n'
header += '\n'
header += ' <div id="mw_header">\n'
header += ' <h1 id="firstHeading">' + i18[lang]['heading'] + '</h1>\n'
header += ' </div>\n'
header += '\n'
header += ' <div id="mw_main">\n'
header += ' <div id="mw_contentwrapper">\n'
header += '\n'
header += ' <!-- content -->\n'
header += ' <div id="mw_content">\n'
header += '\n'
header += ' <p>' + i18[lang]['line1'] + '<br />\n'
header += ' <small>' + i18[lang]['legend'] + '</small><br />\n'
header += ' <small>' + i18[lang]['refresh'] + '</small><br />\n'
header += ' </p>\n'
# add creation time
header += ' <p>' + i18[lang]['lastupdate'] + '<b>' + strftime('%A %d %B %Y %X %Z') + '</b></p>\n'
header += '\n'
#
header += ' <center>\n'
header += ' <table class="wikitable" style="width:85%">\n'
header += ' <tr>\n'
header += ' <th>' + i18[lang]['hnumber'] + '</th>\n'
header += ' <th>' + i18[lang]['hdate'] + '</th>\n'
header += ' <th>' + i18[lang]['htime'] + '</th>\n'
header += ' <th>' + i18[lang]['htype'] + '</th>\n'
header += ' <th>' + i18[lang]['htitle'] + '</th>\n'
header += ' <th>' + i18[lang]['htarget'] + '</th>\n'
header += ' </tr>\n'
return (header)
def footer(lang):
# generate html file footer
footer = ' </table>\n'
footer += ' </center> \n'
footer += '\n'
footer += ' </div><!-- mw_content -->\n'
footer += ' </div><!-- mw_contentwrapper -->\n'
footer += '\n'
footer += ' </div><!-- main -->\n'
footer += '\n'
footer += ' <div class="mw_clear"></div>\n'
footer += '\n'
footer += ' <!-- personal portlet -->\n'
footer += ' <div class="portlet" id="p-personal">\n'
footer += ' <div class="pBody">\n'
footer += ' <ul>\n'
footer += ' <li><a href="http://pl.wikipedia.org">wiki</a></li>\n'
footer += ' <li><a href="/">tools</a></li>\n'
footer += ' <li><a href="/~masti/">masti</a></li>\n'
footer += ' </ul>\n'
footer += ' </div>\n'
footer += ' </div>\n'
footer += '<div class="stopka">layout by <a href="../~beau/">Beau</a></div>\n'
footer += '<!-- Matomo -->\n'
footer += '<script type="text/javascript">\n'
footer += ' var _paq = _paq || [];\n'
footer += ' /* tracker methods like "setCustomDimension" should be called before "trackPageView" */\n'
footer += u" _paq.push(['trackPageView']);\n"
footer += u" _paq.push(['enableLinkTracking']);\n"
footer += ' (function() {\n'
footer += ' var u="//s.wikimedia.pl/";\n'
footer += u" _paq.push(['setTrackerUrl', u+'piwik.php']);\n"
footer += u" _paq.push(['setSiteId', '5']);\n"
footer += u" var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];\n"
footer += u" g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);\n"
footer += ' })();\n'
footer += '</script>\n'
footer += '<noscript><p><img src="//s.wikimedia.pl/piwik.php?idsite=5&rec=1" style="border:0;" alt="" /></p></noscript>\n'
footer += '<!-- End Matomo Code -->\n'
footer += '</body></html>'
return (footer)
def outputRow(logline, lang):
# creates one output row
s = re.sub(u'\n', '', logline)
# print s
try:
anum, adtime, atype, atitle, atarget = s.split(u';')
adate, atime = adtime.split()
except:
return (None)
# encode URLs for title and target
utitle = 'https://' + lang + '.wikipedia.org/wiki/' + urllib.parse.quote(atitle)
# print utitle
if atarget == '':
utarget = ''
else:
utarget = 'https://' + lang + '.wikipedia.org/wiki/' + urllib.parse.quote(atarget)
# create output
result = '\t\t\t<tr>\n'
result += '\t\t\t\t<td>' + anum + '</td>\n'
result += '\t\t\t\t<td>' + adate + '</td>\n'
result += '\t\t\t\t<td>' + atime + '</td>\n'
result += '\t\t\t\t<td>' + atype + '</td>\n'
site = pywikibot.Site(lang, fam='wikipedia')
page = pywikibot.Page(site, atitle)
result += '\t\t\t\t<td>' + linkcolor(page, lang) + '</td>\n'
if page.exists():
if page.isRedirectPage():
try:
tpage = page.getRedirectTarget()
result += '\t\t\t\t<td>' + linkcolor(tpage, lang) + '</td>\n'
except pywikibot.exceptions.CircularRedirect:
tpage = None
result += '\t\t\t\t<td></td>\n'
else:
result += '\t\t\t\t<td></td>\n'
else:
result += '\t\t\t\t<td></td>\n'
result += '\t\t\t</tr>\n'
# pywikibot.output(result)
return (result)
def linkcolor(page, lang):
# return html link for page
# <a href="PAGE TITLE URL" style="color:#308050">' + PAGE TITLE + '</a>
if page.exists():
if page.isRedirectPage():
return (u'<a href="https://' + lang + '.wikipedia.org/wiki/' + urllib.parse.quote(
page.title()) + '" style="color:#308050">' + page.title() + '</a>')
elif page.isDisambig():
return (u'<a href="https://' + lang + '.wikipedia.org/wiki/' + urllib.parse.quote(
page.title()) + '" style="color:#800000">' + page.title() + '</a>')
else:
return (u'<a href="https://' + lang + '.wikipedia.org/wiki/' + urllib.parse.quote(
page.title()) + '">' + page.title() + '</a>')
else:
return (u'<a href="https://' + lang + '.wikipedia.org/w/index.php?title=' + urllib.parse.quote(
page.title()) + '&action=edit&redlink=1" style="color:#CC2200">' + page.title() + '</a>')
def main(*args):
for arg in sys.argv:
if arg.startswith('-lang:'):
lang = arg[6:]
site = pywikibot.Site(lang, fam='wikipedia')
artlist = []
result = ''
logfile = 'masti/ircbot/artnos' + lang + '.log'
resultfile = 'masti/ircbot/artykuly' + lang + '.html'
try:
file = codecs.open(logfile, "r", "utf-8")
artlist = file.readlines()
file.close()
except FileNotFoundError:
artlist = []
arts = artlist[-100:]
# print artlist
result = header(lang)
for a in reversed(arts):
r = outputRow(a, lang)
if r:
result += r
result += footer(lang)
file = codecs.open(resultfile, 'w', 'utf-8')
# printout log
# pywikibot.output(result)
file.write(result)
file.close()
if __name__ == '__main__':
main()