ViewVC Help
View File | Revision Log | View Changeset | Root Listing
root/Oni2/ValBot/check_interwiki_links.py
Revision: 1151
Committed: Mon Mar 8 14:15:52 2021 UTC (4 years, 7 months ago) by iritscen
Content type: text/x-python
File size: 4878 byte(s)
Log Message:
Adding scripts and instructions for using Pywikibot on OniGalore.

File Contents

# Content
1 import os
2
3 from urllib.parse import urljoin
4
5 import pywikibot
6 import re
7
8 from pywikibot.bot import QuitKeyboardInterrupt
9 from pywikibot import pagegenerators
10 from pywikibot.tools.formatter import color_format
11 from pywikibot.comms.http import fetch
12 from pywikibot.specialbots import UploadRobot
13 from bs4 import BeautifulSoup
14
15 # Parallel arrays based on https://wiki.oni2.net/Special:Interwiki
16 interwiki_prefixes = ('acronym', 'cache', 'commons', 'dictionary', 'google', 'metawikimedia', 'mw', 'wikibooks', 'wikidata', 'wikimedia', 'wikinews', 'wikipedia', 'wikiquote', 'wikisource', 'wikispecies', 'wikiversity', 'wikivoyage', 'wikt', 'wiktionary', 'wp')
17
18 interwiki_urls = ('http://www.acronymfinder.com/~/search/af.aspx?string=exact&Acronym=', 'http://www.google.com/search?q=cache:', 'https://commons.wikimedia.org/wiki/', 'http://www.dict.org/bin/Dict?Database=*&Form=Dict1&Strategy=*&Query=', 'http://www.google.com/search?q=', 'https://meta.wikimedia.org/wiki/', 'https://www.mediawiki.org/wiki/', 'https://en.wikibooks.org/wiki/', 'https://www.wikidata.org/wiki/', 'https://foundation.wikimedia.org/wiki/', 'https://en.wikinews.org/wiki/', 'https://en.wikipedia.org/wiki/', 'https://en.wikiquote.org/wiki/', 'https://wikisource.org/wiki/', 'https://species.wikimedia.org/wiki/', 'https://en.wikiversity.org/wiki/', 'https://en.wikivoyage.org/wiki/', 'https://en.wiktionary.org/wiki/', 'https://en.wiktionary.org/wiki/', 'https://en.wikipedia.org/wiki/')
19
20 pages_checked = 0
21 iw_found = 0
22 problems_found = 0
23
24 # Searches the given page text for interwiki links
25 def scan_for_iw_links(page_text):
26 global pages_checked
27 global iw_found
28 global problems_found
29 pages_checked = pages_checked + 1
30 cur = 0
31
32 for prefix in interwiki_prefixes:
33 # Isolate strings that start with "[[prefix:" and end with "|" or "]"
34 iw_link = "\[\[" + prefix + ":[^|\]]*(\||\])"
35 for match in re.finditer(iw_link, page_text):
36 # Extract just the page title from this regex match
37 s = match.start() + 2 + len(prefix) + 1
38 e = match.end() - 1
39
40 # Sometimes we used a space char. instead of a '_', so fix that before querying
41 page_title = page_text[s:e].replace(' ', '_')
42
43 # Construct full URL for the particular wiki
44 iw_url = interwiki_urls[cur] + page_title
45 pywikibot.output('Found {0} link {1}'.format(prefix, page_title))
46 iw_found = iw_found + 1
47
48 # Adjust URL if this is a foreign-language WP link
49 if re.match("^[a-zA-Z]{2}:", page_title):
50 lang_code = page_title[0:2] + "."
51 # "wp:" is the Wikipedia: namespace, not a language
52 if lang_code != "wp." and lang_code != "WP.":
53 iw_url = iw_url.replace('en.', lang_code)
54 iw_url = iw_url.replace(page_title[0:3], '')
55
56 # Test the URL
57 #pywikibot.output('Testing URL {}'.format(iw_url))
58 response = fetch(iw_url)
59
60 # Redirects are followed automatically by fetch() and treated as "200"s, so the
61 # way we tell that a redirect occurred is by checking the history
62 if response.history != []:
63 pywikibot.output('WARNING: Initially got {}.'.format(response.history))
64 problems_found = problems_found + 1
65 elif response.status_code != 200:
66 #pywikibot.output('WARNING: Got response code {}.'.format(response.status_code)) # commented out because fetch() already prints such a msg
67 problems_found = problems_found + 1
68 cur = cur + 1
69
70 def main(*args):
71 cat_name = ''
72 page_name = ''
73
74 local_args = pywikibot.handle_args(args)
75 genFactory = pagegenerators.GeneratorFactory()
76
77 for arg in local_args:
78 if arg.startswith('-cat:'):
79 cat_name = arg[5:]
80 elif arg.startswith('-page:'):
81 page_name = arg[6:]
82
83 site = pywikibot.Site()
84
85 # This line of code enumerates the methods in the 'page' class
86 #pywikibot.stdout(format(dir(page)))
87
88 if cat_name != '':
89 cat_obj = pywikibot.Category(site, cat_name)
90 generator = pagegenerators.CategorizedPageGenerator(cat_obj, recurse=True)
91 for page in pagegenerators.PreloadingGenerator(generator, 100):
92 pywikibot.stdout('Checking page {0}'.format(page.title()))
93 scan_for_iw_links(page.text)
94 elif page_name != '':
95 page = pywikibot.Page(site, page_name)
96 pywikibot.stdout('Checking page {0}'.format(page.title()))
97 scan_for_iw_links(page.text)
98
99 global pages_checked
100 global iw_found
101 global problems_found
102 pywikibot.stdout('Checked {0} page(s) and found {1} interwiki link(s) with {2} problem(s).'.format(pages_checked, iw_found, problems_found))
103
104 if __name__ == '__main__':
105 main()