-
Notifications
You must be signed in to change notification settings - Fork 9
/
antidelete.py
executable file
·144 lines (129 loc) · 4.61 KB
/
antidelete.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# (C) 2013-2020 various people
#
# http://deletionpedia.org/
#
# Script to rescue articles from Wikipedia
# Multilingual, additions for other languages welcome
#
# Available under the terms of the GNU General Public License v2 or later
#
import sys
import re
import datetime
from wikipedia import *
from locale import setlocale, LC_TIME
class Antidelete:
def __init__(self, lang, patterns):
self.lang = lang
self.frm = Site(lang, 'wikipedia')
self.to = Site(lang, 'deleted')
self.patterns = patterns
def fetch(self):
# There are various patterns
print self.lang, self.patterns
if 'fn_day' in self.patterns:
self.fetch_days()
else:
self.parse_list(self.patterns['title'])
def fetch_days(self):
for i in range(7):
self.fetch_day(i)
def fetch_day(self, days_ago = 1):
'''Fetch articles on the list of a specific day'''
day = datetime.date.today() - datetime.timedelta(days_ago)
if 'locale' in self.patterns:
setlocale(LC_TIME, 'de_DE') #self.patterns['locale'])
else:
setlocale(LC_TIME, 'en_US.utf8')
pagename = self.patterns['fn_day'](day)
fn_title = 'fn_title' in self.patterns and self.patterns['fn_title'] or None
self.parse_list(pagename, title_process = fn_title)
def parse_list(self, pagename, title_process = None):
'''Parse page with list of articles to be deleted'''
p = Page(self.frm, pagename)
s = p.get()
re_article = re.compile(self.patterns['regexp'])
for l in s.splitlines():
m = re_article.match(l)
if m:
title = m.group(1)
if title_process:
# if title != 'Info': #Swedish...
title = title_process(title)
self.recover_article(title)
def recover_article(self, title):
print "Recovering: " + title[:100]
if 'Talk' in title:
print 'no talk pages yet'
return
page = Page(self.frm, title)
try:
article_text = page.get()
except IsRedirectPage:
print 'IsRedirectPage?', title
return
except NoPage:
print 'PROBABLY deleted already...', title
return
if not 'porn' in article_text and not 'xxx' in article_text:
dp_page = Page(self.to, title)
update_page = False
try:
if dp_page.get() != article_text:
update_page = True
else:
print 'PAGE already rescued'
except pywikibot.exceptions.NoPage:
update_page = True
if update_page:
if self.patterns['test'] in article_text:
msg = 'inclusion power'
else:
article_text = "{{survived}}"
msg = 'survived on Wikipedia'
dp_page.put(article_text, msg)
if __name__ == '__main__':
patterns = {}
patterns['sv'] = {
'title': u'Wikipedia:Sidor_föreslagna_för_radering',
'regexp': u'{{Wikipedia:Sidor föreslagna för radering/(.*)}}',
'test': '{{sffr}}', # not very talkative
}
patterns['de'] = {
'fn_day': lambda d: u'Wikipedia:Löschkandidaten/' + d.strftime('%d. %B %Y'),
'locale': 'de_DE',
'regexp': '== \[\[(.+)\]\]',
'test': u'{{Löschantragstext',
}
patterns['en'] = {
'test': 'Article for deletion',
'regexp': '{{Wikipedia:Articles for deletion/(.*)}}',
'fn_day': lambda d: 'Wikipedia:Articles_for_deletion/Log/' + d.strftime('%Y_%B_%e'),
'fn_title': lambda t: t.replace(' (2nd nomination)', ''),
}
patterns['fi'] = {
'test': 'oistoäänestys}}',
'regexp': '{{/(.*)}}',
'title': u'Wikipedia:Poistoäänestykset'
}
patterns['fr'] = {
'test': 'uppression}}',
'regexp': '[*]{{L[|](.*)}}',
'title': u'Wikipédia:Pages_à_supprimer'
}
patterns['nl'] = {
'test': '{{wiu|',
'regexp': '[*]\[\[(.+)\]\]',
'fn_day': lambda d: "Wikipedia:Te beoordelen pagina's/Toegevoegd " + d.strftime('%Y%m%d')
}
patterns['pt'] = {
'test': 'Páginas para eliminar',
'regexp': '{{Wikipédia:Páginas para eliminar/(.*)}}',
'title': u'Páginas_para_eliminar'
}
for lang in ['de', 'en', 'fi', 'nl', 'fr', 'sv', 'pt']:
ad = Antidelete(lang, patterns[lang])
ad.fetch()