Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadwiki.py
More file actions
Latest commit
executable file
·160 lines (121 loc) · 4.76 KB
/
Copy pathreadwiki.py
File metadata and controls
executable file
·160 lines (121 loc) · 4.76 KB
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
#!/usr/bin/env python3
frommwclientimportSite
fromrequestsimportReadTimeout
importconfig
importlogging
log=logging.getLogger(__name__)
classReadWiki:
classWikiChange:
def__init__(self):
self.hasData=False
self.revision=None
self.change=None
self.revId=-1
self.message=''
defset_data(self, _revision, _change):
self.hasData=True
self.revision=_revision
self.change=_change
self.revId=self.change["revid"]
defset_message(self, _message):
self.message=_message
defcan_be_send(self):
"""
various checks to see if the change might be spam or other unwanted stuff
"""
# show_params.append("!log") does not work? re-patch
ifself.change["type"] =="log":
log.debug('exclude log')
returnFalse
ifnotself.hasData:
log.debug('HAS NO DATA')
returnFalse
returnTrue
defget_message(self):
ifself.message!='':
returnself.message
ifnotself.hasData:
raiseException('NO MESSAGE AND NO DATA TO PROCESS!')
ifself.change["type"] =="new":
# new pages have no diff, just link to the revision
link="https://{}{}?oldid={}".format(
config.WIKI_SITE,
config.WIKI_VIEW_PATH,
self.change["revid"],
)
else:
link="https://{}{}?type=revision&diff=next&oldid={}".format(
config.WIKI_SITE,
config.WIKI_VIEW_PATH,
self.change["old_revid"],
)
_message="{} - {} by {}".format(
self.revision["pagetitle"], link, self.revision["user"]
)
ifself.revision["comment"] !="":
_message+=" - {}".format(self.revision["comment"])
iflen(_message) >279:
_message=_message[:275] +'...'
self.revId=self.change["revid"]
self.message=_message
returnself.message
def__init__(self):
self.site=None
try:
self.site=Site(config.WIKI_SITE, path=config.WIKI_API_PATH)
exceptReadTimeoutasex:
log.debug("ReadWiki Read TimeOut: %s", ex)
# all other exceptions may fly
#except Exception as ex:
# log.error('ReadWiki.Site could not be initialized: {}'.format(ex))
defget_filter(self):
show_params=list()
# WARNING:mwclient.client:Unrecognized value for parameter "rcshow": !log
#show_params.append("!log")
ifconfig.IGNORE_MINOR_CHANGES:
show_params.append("!minor")
ifconfig.IGNORE_BOTS:
show_params.append("!bot")
return'|'.join(show_params)
defget_changes(self):
""" Iterates over the recent changes made to the wiki. """
ifnotself.site:
return
# maybe support more complex queries?
show=self.get_filter()
counter_ignored=0
counter_blacklist=0
counter_revision=0
total_changes=0
forchangeinself.site.recentchanges(show=show):
total_changes+=1
log.debug("got a change %s", change)
change_obj=ReadWiki.WikiChange()
try:
revisions=self.site.revisions([change["revid"]])
ifnotrevisions:
counter_revision+=1
continue
revision=revisions[0]
ifrevision["user"] inconfig.USER_NAME_BLACK_LIST:
counter_blacklist+=1
continue
change_obj.set_data(revision, change)
ifnotchange_obj.can_be_send():
log.debug('was ignored %s', change_obj.revId)
counter_ignored+=1
continue
exceptIndexErrorasexc:
log.error('recentchanges index ERROR: %s', "{}: {}".format(type(exc).__name__, exc))
change_obj.set_message("{}: {}".format(type(exc).__name__, exc))
yieldchange_obj
log.debug(
'changes | ignored: %s | blacklisted: %s | no revision: %s | of total %s / %s',
counter_ignored, counter_blacklist, counter_revision,
(counter_ignored+counter_blacklist+counter_revision),
total_changes
)
if__name__=='__main__':
readWiki=ReadWiki()
forwikiChangeinreadWiki.get_changes():
print(wikiChange.get_message())