From 6fc71221178d2965346b7510dcfcd0b8586406cd Mon Sep 17 00:00:00 2001 From: salo Date: Thu, 26 Mar 2015 15:44:43 -0500 Subject: [PATCH 1/2] lang_title --- wikipedia/wikipedia.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/wikipedia/wikipedia.py b/wikipedia/wikipedia.py index 04426e4..8213565 100644 --- a/wikipedia/wikipedia.py +++ b/wikipedia/wikipedia.py @@ -302,6 +302,39 @@ def __init__(self, title=None, pageid=None, redirect=True, preload=False, origin for prop in ('content', 'summary', 'images', 'references', 'links', 'sections'): getattr(self, prop) + def lang_title(self, lang_code): + query_params = { + 'prop': 'langlinks', + 'llurl': True, + 'lllang': 'ru', + 'pageids': self.pageid + } + request = _wiki_request(query_params) + pageid = list(request['query']['pages'])[0] + return request['query']['pages'][pageid]['langlinks'][0]['*'] + # print self.title + # print self.pageid + # old_api_url = API_URL + # global API_URL + # API_URL = 'http://' + lang_code.lower() + '.wikipedia.org/w/api.php' + # lang_page_title = page('Constant-velocity joint').title + # set_lang(lang_code) + # API_URL = old_api_url + # return lang_page_title + # query_params = { + # 'prop': 'langlinks', + # 'llurl': True, + # 'lllang': 'ru', + # 'titles': 'Constant-velocity_joint' + # } + # request = _wiki_request(query_params) + # # global API_URL + # # API_URL = 'http://' + lang_code.lower() + '.wikipedia.org/w/api.php' + # for t in request['query']['pages']: + # print request['query']['pages'][t]['langlinks'][0]['*'] + + + def __repr__(self): return stdout_encode(u''.format(self.title)) From 25270e97fba4769f1b1d47277d9c4847a178d254 Mon Sep 17 00:00:00 2001 From: salo Date: Wed, 17 Jun 2015 15:21:14 -0500 Subject: [PATCH 2/2] Wikipedia.py class page new feature: - Get the title in specified language code Returns None if lang code or title isn't found, otherwise returns a string with title. --- wikipedia/wikipedia.py | 52 +++++++++++++++--------------------------- 1 file changed, 19 insertions(+), 33 deletions(-) diff --git a/wikipedia/wikipedia.py b/wikipedia/wikipedia.py index 8213565..2afa530 100644 --- a/wikipedia/wikipedia.py +++ b/wikipedia/wikipedia.py @@ -302,39 +302,6 @@ def __init__(self, title=None, pageid=None, redirect=True, preload=False, origin for prop in ('content', 'summary', 'images', 'references', 'links', 'sections'): getattr(self, prop) - def lang_title(self, lang_code): - query_params = { - 'prop': 'langlinks', - 'llurl': True, - 'lllang': 'ru', - 'pageids': self.pageid - } - request = _wiki_request(query_params) - pageid = list(request['query']['pages'])[0] - return request['query']['pages'][pageid]['langlinks'][0]['*'] - # print self.title - # print self.pageid - # old_api_url = API_URL - # global API_URL - # API_URL = 'http://' + lang_code.lower() + '.wikipedia.org/w/api.php' - # lang_page_title = page('Constant-velocity joint').title - # set_lang(lang_code) - # API_URL = old_api_url - # return lang_page_title - # query_params = { - # 'prop': 'langlinks', - # 'llurl': True, - # 'lllang': 'ru', - # 'titles': 'Constant-velocity_joint' - # } - # request = _wiki_request(query_params) - # # global API_URL - # # API_URL = 'http://' + lang_code.lower() + '.wikipedia.org/w/api.php' - # for t in request['query']['pages']: - # print request['query']['pages'][t]['langlinks'][0]['*'] - - - def __repr__(self): return stdout_encode(u''.format(self.title)) @@ -708,6 +675,25 @@ def section(self, section_title): return self.content[index:next_index].lstrip("=").strip() + def lang_title(self, lang_code): + ''' + Get the title in specified language code + Returns None if lang code or title isn't found, otherwise returns a string with title. + ''' + query_params = { + 'prop': 'langlinks', + 'llurl': True, + 'lllang': lang_code, + 'pageids': self.pageid + } + request = _wiki_request(query_params) + pageid = list(request['query']['pages'])[0] + try: + title = request['query']['pages'][pageid]['langlinks'][0]['*'] + except Exception as e: + title = None + return title + @cache def languages():