-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwebcite.py
283 lines (250 loc) · 10.5 KB
/
webcite.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/python
# -*- coding: utf-8 -*-
#############################################
# Copyright (C) 2018 Z (User:ZxxZxxZ)
# Reza1615 (User:Reza1615)
# Huji (User:Huji)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#############################################
from __future__ import absolute_import, unicode_literals
import json
import re
import requests
import webcitation
import pywikibot
from pywikibot import pagegenerators
from pywikibot.bot import (
SingleSiteBot, ExistingPageBot, NoRedirectPageBot)
from datetime import datetime
class WebCiteBot(
SingleSiteBot,
ExistingPageBot,
NoRedirectPageBot
):
deadlink = []
def __init__(self, generator, **kwargs):
# Edit summary
self.availableOptions.update({
'summary': u'ربات: [[وپ:پایدار|بایگانی منابع برخط]]',
})
# Call constructor of the super class
super(WebCiteBot, self).__init__(site=True, **kwargs)
# Assign the generator to the bot
self.generator = generator
# Save edits without asking
self.options['always'] = True
# Global counter for all archived links
self.counter = 0
# Maximum number of links that can be archived each day
self.availableOptions.update({
'maxlinks': 99,
})
def persianDigits(self, s):
return str(s).replace(u'0', u'۰') \
.replace(u'1', u'۱') \
.replace(u'2', u'۲') \
.replace(u'3', u'۳') \
.replace(u'4', u'۴') \
.replace(u'5', u'۵') \
.replace(u'6', u'۶') \
.replace(u'7', u'۷') \
.replace(u'8', u'۸') \
.replace(u'9', u'۹')
def persianDate(self, date=False):
monthNames = [
u"ژانویه",
u"فوریه",
u"مارس",
u"آوریل",
u"مه",
u"ژوئن",
u"ژوئیه",
u"اوت",
u"سپتامبر",
u"اکتبر",
u"نوامبر",
u"دسامبر"
]
if date is False:
date = datetime.today()
y = date.year
m = monthNames[date.month - 1]
d = date.day
d = self.persianDigits(d)
y = self.persianDigits(y)
return '%s %s %s' % (d, m, y)
def archive(self, URL):
pywikibot.output("Requesting URL %s" % URL)
try:
archiveURL = webcitation.capture(URL)
except Exception:
pywikibot.output(u"\03{red}Archive attempt failed!\03{default}")
return False
pywikibot.output(u"\03{lightgreen}Successfully archived at " +
u"%s\03{default}" % archiveURL)
self.counter += 1
return archiveURL
def find_archive(self, URL):
query = 'https://archive.org/wayback/available?url=' + URL
res = requests.get(query)
if res.status_code == 200:
j = json.loads(res.text)
section = 'archived_snapshots'
item = 'closest'
if section in j and item in j[section]:
if j[section][section]['status'] == '200':
archive_url = j[section][item]['url']
archive_ts = j[section][item]['timestamp']
archive_ts = datetime.strptime(archive_ts, "%Y%m%d%H%M%S")
now = datetime.now()
archive_age = now - archive_ts
if archive_age.days < 365:
pywikibot.output(u"\03{lightgreen}Found archive at " +
u"%s\03{default}" % archive_url)
self.counter += 1
return [archive_url, archive_ts]
return False
def treat_page(self):
if (self.current_page.namespace() != 0):
pywikibot.output("Not an article; skipped...")
return True
text = self.current_page.text
failures = []
# Persian Citations
persianDate = self.persianDate()
faCitationPattern = r'\{\{\s*یادکرد(?:\{\{.*?\}\}|.)*?\}\}'
faArchivePattern = r'\| *(پیوند بایگانی|نشانی بایگانی) *= *[^ |}]'
faBlankPattern = r'\| *' + \
r'(پیوند بایگانی|نشانی بایگانی|تاریخ بایگانی)' + \
r' *= *(?=[|}])'
faUrlPattern = r'\| *(نشانی|پیوند) *= *([^|]+) *\|'
faCitations = set(re.findall(faCitationPattern, text, re.S))
if not faCitations:
pywikibot.output(
u"\03{lightpurple}No Persian citations!\03{default}"
)
else:
for citation in faCitations:
if not re.findall(faArchivePattern, citation, re.S):
# Not archived already
url = re.findall(faUrlPattern, citation, re.S)
if url and url[0][1].strip() != "":
# Try archiving it
if self.counter + 1 > self.getOption('maxlinks'):
pywikibot.output(
u"\03{red}WebCitation quota is used up!" +
u"\03{default}"
)
continue
found = self.find_archive(url[0][1])
if found:
arc = found[0]
persianDate = self.persianDate(found[1])
else:
arc = self.archive(url[0][1])
if not arc:
# Archiving failed
failures.append(url[0][1])
continue
# Remove any blank archiveurl parameters
newCitation = re.sub(faBlankPattern, "", citation)
# Add archiveurl and archivedate
newParams = (
"| پیوند بایگانی = %s | تاریخ بایگانی = %s }}"
) % (arc, persianDate)
newCitation = re.sub("}}$", newParams, newCitation)
text = text.replace(citation, newCitation)
# English Citations
englishDate = datetime.today().strftime("%-d %B %Y")
enCitationPattern = r'\{\{ *[Cc]ite(?:\{\{.*?\}\}|.)*?\}\}'
enArchivePattern = r'\| *(archiveurl|archive-url) *= *[^ |}]'
enBlankPattern = (
r'\| *' +
r'(archiveurl|archive-url|archivedate|archive-date)' +
r' *= *(?=[|}])'
)
enUrlPattern = r'\| *(url) *= *([^|]+) *\|'
enCitations = set(re.findall(enCitationPattern, text, re.S))
if not enCitations:
pywikibot.output(
u"\03{lightpurple}No English citations!\03{default}"
)
else:
for citation in enCitations:
if not re.findall(enArchivePattern, citation, re.S):
# Not archived already
url = re.findall(enUrlPattern, citation, re.S)
if url and url[0][1].strip() != "":
# Try archiving it
if self.counter + 1 > self.getOption('maxlinks'):
pywikibot.output(
u"\03{red}WebCitation quota is used up!" +
u"\03{default}"
)
continue
found = self.find_archive(url[0][1])
if found:
arc = found[0]
englishDate = found[1].strftime("%-d %B %Y")
else:
arc = self.archive(url[0][1])
if not arc:
failures.append(url[0][1])
# Archiving failed
continue
# Remove any blank archiveurl parameters
newCitation = re.sub(enBlankPattern, "", citation)
# Add archiveurl and archivedate
newParams = (
"| archive-url = %s | archive-date = %s }}"
) % (arc, englishDate)
newCitation = re.sub("}}$", newParams, newCitation)
text = text.replace(citation, newCitation)
self.put_current(text, summary=self.getOption('summary'))
talk_page = self.current_page.toggleTalkPage()
talk_text = talk_page.text
talk_text += """
== بایگانی منابع برخط ==
{{شد}} بایگانی همه منابع برخط انجام شد."""
if len(failures) > 0:
talk_text += '\nموارد زیر ناموفق بود:'
for f in failures:
talk_text += '\n* [' + f + ' ]'
talk_text += '\n~~~~'
talk_page.text = talk_text
talk_page.save(summary=self.getOption('summary'))
def main(*args):
options = {}
local_args = pywikibot.handle_args(args)
genFactory = pagegenerators.GeneratorFactory()
for arg in local_args:
# Catch the pagegenerators options
if genFactory.handleArg(arg):
continue # nothing to do here
# Take the remaining options as booleans.
else:
pywikibot.output('Unknown argument(s)!')
gen = genFactory.getCombinedGenerator()
if gen:
gen = pagegenerators.PreloadingGenerator(gen)
bot = WebCiteBot(gen, **options)
bot.run()
return True
else:
pywikibot.bot.suggest_help(missing_generator=True)
return False
if __name__ == "__main__":
main()