#!/usr/bin/python """ MIT License Copyright (c) 2016 Vit Baisa Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ import os import sys import argparse import urllib2 import urllib import datetime import json import time import datetime import gzip from justext import core as justext from lxml.etree import XMLSyntaxError, ParserError class MissingPage(Exception): pass class EmptyHTML(Exception): pass class EmptyJusText(Exception): pass LATEST = 'https://dumps.wikimedia.org/%swiki/latest/%swiki-latest-all-titles-in-ns0.gz' API_HTML = 'https://%s.wikipedia.org/w/api.php?action=parse&page=%s&format=json' API_JSON = 'https://%s.wikipedia.org/w/api.php?action=query&prop=revisions&titles=%s&format=json' last_api_request = datetime.datetime.now() last_api_parse = datetime.datetime.now() logf = None def html2prevert(s): try: html_root = justext.preprocess(html_text=s, encoding='utf-8') return justext.make_paragraphs(html_root) except (ParserError, XMLSyntaxError): return [] def api_wait(last): global wait_interval global logf n = datetime.datetime.now() interval = (n-last).seconds + ((n-last).microseconds / 1.0e6) if interval < wait_interval: time.sleep(wait_interval - interval) def process_article(langcode, title, linksf): global last_api_parse global logf api_wait(last_api_parse) last_api_parse = datetime.datetime.now() resp = urllib2.urlopen(API_HTML % (langcode, title)) data = json.load(resp) if 'error' in data: raise MissingPage() p = data['parse'] html = p['text']['*'] if html.strip(): pars = html2prevert(html.encode('utf-8')) # justext decodes! else: print >>logf, '\tempty HTML parse returned by API' raise EmptyHTML() if not pars: print >>logf, '\tempty prevert returned by jusText' raise EmptyJusText() outp = [] for par in pars: parx = justext.html_escape(par['text']) outp.append(parx) revid = p['revid'] langlinks_len = len(p['langlinks']) #links = '\v'.join([d['*'].replace('"', '') for d in p['links']]) categories = '\v'.join([d['*'].replace('"', '') for d in p['categories']]) if linksf and p['externallinks']: linksf.write('### %s\n' % title) for line in p['externallinks']: linksf.write(line + '\n') linksf.write('\n') s = '' chars = 0 for p in outp: s += '
\n' s += p s += '\n
\n' chars += len(p) print >>logf, '\t%d chars' % chars header = '