Content deleted Content added
Posting Source, Please not there a bugs related to & in the display |
Many fixes, more commenting |
||
Line 9:
Command line options:
-file: Update
-ref: Update
-cat: Update artcile pages from the given category.
"""
import re, sys, httplib, time
import wikipedia, pagegenerators, login, config,
from urllib2 import urlparse
# Define global variables
writeDelay = 60 # seconds
readDelay = 15 #seconds
httpDebug = 0
userAgent = 'PDFbot (http://en.wikipedia.org/wiki/User:PDFbot)'
def whichURL(___location):
redirectCounter =
try:
while (redirectCounter > 0 and ___location is not None
(scheme, site, path, args, query, frag) = urlparse.urlparse(___location)
path = path + args + query
conn = httplib.HTTPConnection(site)
conn.set_debuglevel(
conn.putrequest('HEAD', path)
conn.putheader('User-Agent',
conn.endheaders()
Line 34 ⟶ 41:
redirectCounter -= 1
if(redirectCounter > 0 and ___location is not None
conn.close()
wikipedia.output( u'Redirecting to %s' % ___location )
Line 40 ⟶ 47:
content_length = response.msg.getheader('content-length')
content_type = response.msg.getheader('content-type')
response_code = response.status
conn.close()
return ( [site, path, content_length, content_type] )
Line 50 ⟶ 58:
a = float(size)
exponent = 0
while a >= 1000. :
a /= 1024.
exponent += 3
prefix = ['bytes', '[[Kibibyte|KiB]]', '[[Mebibyte|MiB]]', '[[Gibibyte|GiB]]']
# Truncate and remove trailing dot
Line 62 ⟶ 70:
def update_size_paramter(template_text):
___location = re.search(r'
prefix_text = re.search(r'(\{\{[^|]*\|[^|}]*)[^}]*\}\}', template_text
if (re.findall(r'=', template_text)):
Line 74 ⟶ 82:
___location = re.sub(r'&', r'&', ___location)
▲ (site, path, content_length, content_type ) = whichURL(___location)
# I should really put in 404 error handling code, but this has been working just fine.
▲ if (content_length is not None):
if (re.findall(r'pdf|octet-stream', content_type)):
return prefix_text + parameter_prefix + binary_notation(content_length) + '<!-- ' + content_type + ', ' + str(content_length) +" bytes -->}}"
return template_text▼
wikipedia.output(u'Unusual content_type: ' + content_type)
▲ return template_text
def process_article(site, pageName):▼
# If anything else return the template_text back
page = wikipedia.Page(site, pageName)▼
wikitext = page.get()
# Fix Casing (Reduces the number of possible expressions)
wikitext = re.sub(r'\{\{ *(Template:|template:|)(PDF|Pdf|pdf)', r'{{PDF', wikitext)
# State point. Count any changes as needing an update if they're after this line
# Convert
wikitext = re.sub(r'(\[http[^]]*\]) *\((\[\[[^|\]]*|)(PDF|pdf)(\]\]|)\)', r'{{PDFlink|\1}}', wikitext)
# Convert from the old style to the new style (ex: [http link] {{PDF}} or {{PDF}} [http link] )
wikitext = re.sub(r'(\(|)\{\{(PDFlink|PDF)\}\}(\)|) *(\[http[^]]*\])', r'{{\2|\4}}', wikitext)
wikitext = re.sub(r'("|)(\[http[^]]*\])("|)([^a-zA-Z(]*) *(\(|)\{\{(PDFlink|PDF)\}\}(\)|)', r'{{\
# Remove PDFlink
wikitext = re.sub(r'(format *= *)(\(|)\{\{PDF(|link)\}\}(\(|)', r'\1PDF', wikitext)
wikitext = re.sub(r'(\{\{(Cite|cite)[^}]*)(}}[^a-zA-Z]*)(\(|)\{\{(PDF|PDFlink)\}\}(\)|)', r'\1 |format=PDF\3', wikitext)
wikitext = re.sub(r'(\{\{.ite web[^}]*) *(\(|)\{\{(PDF|PDFlink)\}\}(\)|) *([^}]*\}\})', r'\1 |format=PDF \5', wikitext)
# Fix equal sign problem
m = re.findall(r'(\{\{PDF[^|}]*\|[^}]*\}\})', wikitext )▼
wikitext = re.sub(r'(PDF|PDFlink)\|(1=|)([^{|}]*=[^{|}]*)', r'\1|1=\3', wikitext)
state1 = wikitext
return▼
for s in m:
if (re.findall(r'
replacetext = update_size_paramter(s)
wikitext = re.sub(re.escape(s), replacetext, wikitext)▼
# Uncomment the bellow line to see the replacement text
# wikipedia.output(replacetext)
▲ wikitext = re.sub(re.escape(s), replacetext, wikitext)
EditMsg = 'Corrected use of {{[[Template:PDFlink|PDFlink]]}}'
else:
Line 118 ⟶ 132:
wikipedia.setAction(EditMsg)
# If the text has changed at all since
if (
wikipedia.output(u'
# Pause to reduce load on the servers▼
else:
time.sleep(readDelay)
def main():
site = wikipedia.getSite()
elif (arg.startswith('-file:')):▼
▲ else:
gen = pagegenerators.TextfilePageGenerator(arg[6:])
wikipedia.showHelp(u'pdfbot')▼
elif (arg.startswith('-cat:')):
cat = catlib.Category(site, arg[5:])
gen = pagegenerators.CategorizedPageGenerator(cat)
else:
▲ wikipedia.showHelp(u'pdfbot')
▲ return
wikipedia.output(u'
wikipedia.output(u'Write delay is %s seconds.\n' % writeDelay)
for page in
if (not re.findall(r'(User|Wikipedia|Image|MediaWiki|Template|Help|Category|Portal
process_article(
▲ # Pause to reduce load on the servers
▲ time.sleep(timer*60)
if __name__ == "__main__":
</nowiki></pre>
|