diff --git a/README.rst b/README.rst index 03b8107..cbfd005 100644 --- a/README.rst +++ b/README.rst @@ -53,6 +53,8 @@ To install Wikipedia, simply run: $ pip install wikipedia +After installation to define parameter 'WIKIPEDIA_PROJECT' with the path to the current project in system variables. + Wikipedia is compatible with Python 2.6+ (2.7+ to run unittest discover) and Python 3.3+. Documentation diff --git a/requirements.txt b/requirements.txt index 130b5ae..fe649fe 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ +numpy beautifulsoup4 requests>=2.0.0,<3.0.0 diff --git a/wikipedia/exceptions.py b/wikipedia/exceptions.py index 0295b1c..014bb67 100644 --- a/wikipedia/exceptions.py +++ b/wikipedia/exceptions.py @@ -78,3 +78,10 @@ def __init__(self, query): def __unicode__(self): return u"Searching for \"{0}\" resulted in a timeout. Try again in a few seconds, and make sure you have rate limiting set to True.".format(self.query) + + +class BadParameterException(WikipediaException): + """Exception raised when an incorrectly set parameter, such as language, query parameter and etc.""" + + def __unicode__(self): + return "The parameter is set incorrectly. {0}".format(self.error) \ No newline at end of file diff --git a/wikipedia/languages_tool.py b/wikipedia/languages_tool.py new file mode 100644 index 0000000..88e5731 --- /dev/null +++ b/wikipedia/languages_tool.py @@ -0,0 +1,24 @@ +import csv +import os + +from numpy.core.defchararray import lower + +from .exceptions import BadParameterException + +project_folder = "WIKIPEDIA_PROJECT" +file_csv = "{0}{1}".format(os.environ.get(project_folder, None), "\\wikipedia\\list_languages.csv") + + +def init_list_languages(): + try: + with open(file_csv, newline='') as file: + reader = csv.reader(file) + return list(reader) + except FileNotFoundError: + raise BadParameterException("Make sure that the system variable '{0}' is set, " + "the value of which must be the path to the current project".format(project_folder)) + + +def find_language(lst, param): + if lower(param) in lst: + return True diff --git a/wikipedia/list_languages.csv b/wikipedia/list_languages.csv new file mode 100644 index 0000000..d130c43 --- /dev/null +++ b/wikipedia/list_languages.csv @@ -0,0 +1,310 @@ +en +ceb +sv +de +fr +nl +ru +it +es +pl +war +vi +ja +zh +ar +pt +uk +fa +ca +sr +arz +no +id +ko +fi +hu +cs +sh +ro +zh-min-nan +eu +tr +ms +eo +hy +he +bg +da +ce +azb +sk +kk +min +hr +et +lt +be +el +sl +gl +simple +az +ur +nn +hi +th +ka +uz +la +cy +ta +vo +ast +mk +lv +tg +tt +mg +af +bn +oc +zh-yue +bs +sq +ky +new +tl +be-tarask +te +ml +br +nds +pms +su +sw +ht +lb +vec +jv +mr +sco +pnb +ga +szl +ba +is +my +fy +cv +lmo +an +pa +ne +wuu +yo +bar +io +gu +ku +als +ckb +kn +scn +bpy +ia +qu +mn +diq +bat-smg +or +si +cdo +nv +ilo +gd +yi +am +nap +bug +xmf +wa +sd +hsb +mai +map-bms +fo +mzn +li +eml +os +sah +ps +sa +frr +bcl +ace +zh-classical +mrj +mhr +hif +hak +roa-tara +pam +nso +km +hyw +se +rue +crh +bh +vls +mi +shn +nds-nl +nah +as +sc +vep +gan +myv +gor +ab +glk +bo +so +co +tk +fiu-vro +sn +lrc +kv +csb +ha +gv +udm +ie +ay +pcd +zea +kab +nrm +ug +lez +stq +kw +haw +frp +lfn +lij +mwl +gn +gom +rm +mt +lo +lad +koi +fur +olo +sat +dty +dsb +ang +ext +ln +bjn +cbk-zam +dv +ban +ksh +gag +pfl +pag +pi +tyv +av +zu +awa +bxr +xal +krc +pap +za +pdc +kaa +rw +arc +szy +to +nov +jam +tpi +kbp +kbd +ig +na +tet +wo +ki +tcy +inh +jbo +atj +roa-rup +bi +lbe +kg +ty +lg +mdf +srn +xh +gcr +fj +ltg +chr +sm +got +ak +kl +pih +om +cu +tn +tw +st +ts +rmy +bm +chy +nqo +rn +tum +ny +mnw +ss +ch +pnt +iu +ady +ks +ve +ee +ik +sg +ff +ti +dz +din +cr +ng +cho +kj +mh +ho +ii +aa +mus +hz +kr diff --git a/wikipedia/wikipedia.py b/wikipedia/wikipedia.py index 7ad50e1..4465642 100644 --- a/wikipedia/wikipedia.py +++ b/wikipedia/wikipedia.py @@ -6,9 +6,10 @@ from datetime import datetime, timedelta from decimal import Decimal +from wikipedia.languages_tool import init_list_languages, find_language from .exceptions import ( - PageError, DisambiguationError, RedirectError, HTTPTimeoutError, - WikipediaException, ODD_ERROR_MESSAGE) + PageError, DisambiguationError, RedirectError, HTTPTimeoutError, + WikipediaException, ODD_ERROR_MESSAGE, BadParameterException) from .util import cache, stdout_encode, debug import re @@ -17,6 +18,7 @@ RATE_LIMIT_MIN_WAIT = None RATE_LIMIT_LAST_CALL = None USER_AGENT = 'wikipedia (https://github.com/goldsmith/Wikipedia/)' +LIST_LANGUAGES = init_list_languages() def set_lang(prefix): @@ -28,12 +30,14 @@ def set_lang(prefix): .. note:: Make sure you search for page titles in the language that you have set. ''' - global API_URL - API_URL = 'http://' + prefix.lower() + '.wikipedia.org/w/api.php' - - for cached_func in (search, suggest, summary): - cached_func.clear_cache() + if find_language(LIST_LANGUAGES, prefix): + global API_URL + API_URL = 'http://' + prefix.lower() + '.wikipedia.org/w/api.php' + for cached_func in (search, suggest, summary): + cached_func.clear_cache() + else: + raise BadParameterException('The entered "language" parameter "{0}" is not supported by Wikipedia'.format(prefix)) def set_user_agent(user_agent_string): ''' @@ -97,6 +101,8 @@ def search(query, results=10, suggestion=False): 'limit': results, 'srsearch': query } + if not query: + raise BadParameterException('The entered "query" parameter should not be empty') if suggestion: search_params['srinfo'] = 'suggestion'