|
| 1 | +import os |
| 2 | +import re |
| 3 | +import json |
| 4 | +import requests |
| 5 | +import configparser |
| 6 | + |
| 7 | +from bs4 import BeautifulSoup |
| 8 | + |
| 9 | +GSWIKI_USER = os.environ.get('GSWIKI_USER') |
| 10 | +GSWIKI_PW = os.environ.get('GSWIKI_PW') |
| 11 | + |
| 12 | +HTML_FILTERS = { |
| 13 | + 'div': ['navbox','navbox-styles','spoken-wikipedia', 'noprint', 'hatnote', 'rt-tooltip', 'reflist'], |
| 14 | + 'span': ['mw-ext-cite-error'], |
| 15 | + 'table': ['noprint','ombox'], |
| 16 | + 'ol': ['breadcrumb-nav-container', 'references'], |
| 17 | + 'sup': ['reference'] |
| 18 | +} |
| 19 | +SECTION_FILTERS = [ 'Siehe auch', 'See also', 'Weblinks', 'Anmerkungen', 'Notes' ] |
| 20 | +REGEX_FILTERS = { |
| 21 | + 'p': '→.*ersion' |
| 22 | +} |
| 23 | + |
| 24 | +def filterHtml(soup): |
| 25 | + for figure in soup.find_all('figure'): |
| 26 | + figure.decompose() |
| 27 | + |
| 28 | + for tag, classes in HTML_FILTERS.items(): |
| 29 | + for className in classes: |
| 30 | + for div in soup.find_all(tag, {'class': className}): |
| 31 | + div.decompose() |
| 32 | + |
| 33 | + for tag, regex in REGEX_FILTERS.items(): |
| 34 | + for element in soup.find_all(tag): |
| 35 | + if(re.search(regex, str(element)) != None): |
| 36 | + element.decompose() |
| 37 | + |
| 38 | + return soup |
| 39 | + |
| 40 | +def fetchFromWiki(url, titles, loginRequired): |
| 41 | + if(loginRequired == True): |
| 42 | + session = loginToWiki(url) |
| 43 | + else: |
| 44 | + session = requests.Session() |
| 45 | + |
| 46 | + articles = {} |
| 47 | + for title in titles: |
| 48 | + sections = fetchSections(url, title, session.cookies) |
| 49 | + print("fetching {} sections for article {}".format(len(sections), title)) |
| 50 | + for section in [ { 'index' : 0, 'line': 'Intro', 'linkAnchor' : '', 'anchor' : '' } ] + sections : |
| 51 | + if section['index'] == '' or section['line'] in SECTION_FILTERS: |
| 52 | + continue |
| 53 | + |
| 54 | + query = { |
| 55 | + 'action': 'parse', |
| 56 | + 'page': title, |
| 57 | + 'format': 'json', |
| 58 | + 'prop':'text', |
| 59 | + 'disabletoc': True, |
| 60 | + 'disablelimitreport': True, |
| 61 | + 'disableeditsection': True, |
| 62 | + 'section': section['index'] |
| 63 | + } |
| 64 | + section_html = requests.get(url,params=query,cookies=session.cookies).json()['parse']['text']['*'] |
| 65 | + section_soup = BeautifulSoup(section_html, 'lxml') |
| 66 | + articles[title + '#' + section['anchor']] = filterHtml(section_soup).get_text() |
| 67 | + |
| 68 | + return articles |
| 69 | + |
| 70 | + |
| 71 | +def fetchSections(url, title, cookies=None): |
| 72 | + query = { |
| 73 | + 'action':'parse', |
| 74 | + 'page':title, |
| 75 | + 'format':'json', |
| 76 | + 'prop':'sections' |
| 77 | + } |
| 78 | + sectionsResponse = requests.get(url,params=query, cookies=cookies) |
| 79 | + toplevelSections = [ section for section in sectionsResponse.json()['parse']['sections'] if section['toclevel'] == 1 ] |
| 80 | + return toplevelSections |
| 81 | + |
| 82 | +def loginToWiki(url): |
| 83 | + session = requests.Session() |
| 84 | + |
| 85 | + tokenQuery = { 'action': 'query', 'meta': 'tokens', 'type': 'login', 'format': 'json' } |
| 86 | + token = session.get(url, params=tokenQuery).json()['query']['tokens']['logintoken'] |
| 87 | + loginData = { |
| 88 | + 'lgname': GSWIKI_USER, |
| 89 | + 'lgpassword': GSWIKI_PW, |
| 90 | + 'lgtoken': token, |
| 91 | + 'action': 'login', |
| 92 | + 'format': 'json' |
| 93 | + } |
| 94 | + response = session.post(url, data=loginData, headers={ 'Content-Type' : 'application/x-www-form-urlencoded' }) |
| 95 | + #TODO: error handling in case of login failure |
| 96 | + return session |
| 97 | + |
| 98 | +def fetch_articles(toc): |
| 99 | + articles = [] |
| 100 | + for wiki in toc: |
| 101 | + url = wiki['host'] + wiki['api_path'] |
| 102 | + wikiArticles = fetchFromWiki(url, wiki['titles'], wiki['login']) |
| 103 | + |
| 104 | + articles.append( { |
| 105 | + 'wiki': wiki['name'], |
| 106 | + 'url': wiki['host'], |
| 107 | + 'lang': wiki['lang'], |
| 108 | + 'articles': wikiArticles |
| 109 | + } ) |
| 110 | + return articles |
| 111 | + |
0 commit comments