-
Notifications
You must be signed in to change notification settings - Fork 1
/
ms-artnos.py
196 lines (178 loc) · 6.97 KB
/
ms-artnos.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import re
import pywikibot
import codecs
import urllib.parse
from datetime import datetime
from time import strftime
def header():
#generate html file header
header = u'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n'
header += u'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" lang="pl" dir="ltr">\n'
header += u' <head>\n'
header += u' <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />\n'
header += u' <meta http-equiv="refresh" content="300">\n'
header += u' <title>Licznik artykułów pl.wikipedia - ostatnie 100 pozycji - tools.wikimedia.pl</title>\n'
header += u' <link rel="stylesheet" type="text/css" href="/~masti/modern.css" />\n'
header += u' </head>\n'
header += u'<body>\n'
header += u'\n'
header += u' <!-- heading -->\n'
header += u'\n'
header += u' <div id="mw_header">\n'
header += u' <h1 id="firstHeading">Ostatnie nowe artykuły</h1>\n'
header += u' </div>\n'
header += u'\n'
header += u' <div id="mw_main">\n'
header += u' <div id="mw_contentwrapper">\n'
header += u'\n'
header += u' <!-- content -->\n'
header += u' <div id="mw_content">\n'
header += u'\n'
header += u' <p>Strona przedstawia numerację ostatnio utworzonych 100 artykułów z głównej przestrzeni nazw.<br />\n'
header += u' <small>Rodzaj: <b>A</b> (artykuł), <b>R</b> (przekierowanie)</small><br />\n'
header += u' <small>Strona uaktualniana co 5 minut. Wyświetlanie od najnowszych</small><br />\n'
header += u' </p>\n'
# add creation time
header += u' <p>Ostatnia aktualizacja: <b>' + strftime('%A %d %B %Y %X %Z') + u'</b></p>\n'
header += u'\n'
#
header += u' <center>\n'
header += u' <table class="wikitable" style="width:85%">\n'
header += u' <tr>\n'
header += u' <th>Numer artykułu</th>\n'
header += u' <th>Data</th>\n'
header += u' <th>Czas</th>\n'
header += u' <th>Rodzaj</th>\n'
header += u' <th>Tytuł</th>\n'
header += u' <th>Cel</th>\n'
header += u' </tr>\n'
return(header)
def footer():
#generate html file footer
footer = u' </table>\n'
footer += u' </center> \n'
footer += u'\n'
footer += u' </div><!-- mw_content -->\n'
footer += u' </div><!-- mw_contentwrapper -->\n'
footer += u'\n'
footer += u' </div><!-- main -->\n'
footer += u'\n'
footer += u' <div class="mw_clear"></div>\n'
footer += u'\n'
footer += u' <!-- personal portlet -->\n'
footer += u' <div class="portlet" id="p-personal">\n'
footer += u' <div class="pBody">\n'
footer += u' <ul>\n'
footer += u' <li><a href="http://pl.wikipedia.org">wiki</a></li>\n'
footer += u' <li><a href="/">tools</a></li>\n'
footer += u' <li><a href="/~masti/">masti</a></li>\n'
footer += u' </ul>\n'
footer += u' </div>\n'
footer += u' </div>\n'
footer += u'<div class="stopka">layout by <a href="../~beau/">Beau</a></div>\n'
footer += u'</body></html>'
return(footer)
def outputRow(logline):
#creates one output row
s = re.sub(u'\n',u'',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 = urllib.parse.quote_plus((u'//pl.wikipedia.org/wiki/' + atitle))
if atarget == u'':
utarget = u''
else:
utarget = urllib.parse.quote_plus((u'//pl.wikipedia.org/wiki/' + atarget))
# create output
result = u'\t\t\t<tr>\n'
result += u'\t\t\t\t<td>' + anum +'</td>\n'
result += u'\t\t\t\t<td>' + adate +'</td>\n'
result += u'\t\t\t\t<td>' + atime +'</td>\n'
result += u'\t\t\t\t<td>' + atype +'</td>\n'
page = pywikibot.Page(pywikibot.Site(), atitle)
if page.exists():
astyle = u''
if page.isRedirectPage():
astyle = u' style="color:#308050">'
try:
tpage = page.getRedirectTarget()
ttitle = tpage.title()
except:
tpage = None
ttitle = u''
if tpage:
if tpage.exists():
if tpage.isRedirectPage():
tstyle = u' style="color:#308050">'
else:
tstyle = u''
else:
tstyle = u' style="color:#CC2200">'
else:
tstyle = u''
else:
astyle = u' style="color:#CC2200">'
if atype == u'R':
tpage = pywikibot.Page(pywikibot.Site(), atarget)
ttitle = tpage.title()
if tpage.exists():
if tpage.isRedirectPage():
tstyle = u' style="color:#308050">'
else:
tstyle = u''
else:
tstyle = u' style="color:#CC2200">'
urlatitle = urllib.parse.quote_plus((u'//pl.wikipedia.org/wiki/' + atitle))
urlatarget = urllib.parse.quote_plus((u'//pl.wikipedia.org/wiki/' + atarget))
#result += u'\t\t\t\t<td><a href="'+ utitle + u'">' + atitle + u'</a></td>\n'
if atype == u'R':
if page.exists():
result += u'\t\t\t\t<td><a href="'+ utitle + u'" style="color:#308050">' + atitle + u'</a></td>\n'
else:
result += u'\t\t\t\t<td><a href="'+ utitle + u'" style="color:#CC2200">' + atitle + u'</a></td>\n'
else:
if page.exists():
result += u'\t\t\t\t<td><a href="'+ utitle + u'">' + atitle + u'</a></td>\n'
else:
result += u'\t\t\t\t<td><a href="'+ utitle + u'" style="color:#CC2200">' + atitle + u'</a></td>\n'
if atarget == u'':
result += u'\t\t\t\t<td></td>\n'
elif atarget == u'BŁĄD PRZEKIEROWANIA':
result += u'\t\t\t\t<td>BŁĄD PRZEKIEROWANIA</td>\n'
else:
redir = pywikibot.Page(pywikibot.Site(), atarget)
utarget = urllib.parse.quote_plus((u'//pl.wikipedia.org/wiki/' + atarget))
if redir.exists():
result += u'\t\t\t\t<td><a href="'+ utarget + u'">' + atarget + u'</a></td>\n'
else:
result += u'\t\t\t\t<td><a href="'+ utarget + u'" style="color:#CC2200">' + atarget + u'</a></td>\n'
result += u'\t\t\t</tr>\n'
#pywikibot.output(result)
return(result)
def main(*args):
artlist = []
result = u''
file = codecs.open("ircbot/artnos.log", "r", "utf-8")
artlist = file.readlines()
file.close()
arts = artlist[-100:]
#print artlist
result = header()
for a in reversed(arts):
r = outputRow(a)
if r:
result += outputRow(a)
result += footer()
file = codecs.open('masti/artykuly.html', 'w', 'utf-8')
# printout log
pywikibot.output(result)
file.write(result)
file.close()
if __name__ == '__main__':
main()