def get_wiki_info(lang, project): '''\ Get the mainpage title and local namespace map. ''' url = MW_API_URL.format(lang=lang, project=project) params = {'action': 'query', 'meta': 'siteinfo', 'format': 'json', 'siprop': 'general|namespaces'} resp = urllib2.urlopen(url + urlencode(params)) data = json.loads(resp.read()) mainpage = data['query']['general']['mainpage'].replace(' ', '_') namespaces = [ns_info['*'].replace(' ', '_') for ns_id, ns_info in data['query']['namespaces'].iteritems() if ns_id is not 0] return {'mainpage': mainpage, 'namespaces': namespaces}
def get_query(params, lang, project, extractor, default_val): '''\ Generic function for MediaWiki queries. ''' url = MW_API_URL.format(lang=lang, project=project) resp = urllib2.urlopen(url + urlencode(params)) data = json.loads(resp.read()) ret = {} for page_id, page_info in data['query']['pages'].iteritems(): if int(page_id) < 0: ret[page_info['title']] = default_val else: try: ret[page_info['title']] = extractor(page_info) except Exception: ret[page_info['title']] = default_val return ret
def get_wiki_info(lang, project): '''\ Get the mainpage title and local namespace map. ''' url = MW_API_URL.format(lang=lang, project=project) params = { 'action': 'query', 'meta': 'siteinfo', 'format': 'json', 'siprop': 'general|namespaces' } resp = urllib2.urlopen(url + urlencode(params)) data = json.loads(resp.read()) mainpage = data['query']['general']['mainpage'].replace(' ', '_') namespaces = [ ns_info['*'].replace(' ', '_') for ns_id, ns_info in data['query']['namespaces'].iteritems() if ns_id is not 0 ] return {'mainpage': mainpage, 'namespaces': namespaces}