]> code.communitydata.science - covid19.git/blob - translations/src/wikidata_api_calls.py
022a9d40354a4a1c823f8c925045c3946556299c
[covid19.git] / translations / src / wikidata_api_calls.py
1 # File defines functions for making api calls to find translations and transliterations for key terms.
2
3 import mwapi
4 import sys
5 sys.path.append("..")
6 from defaults import user_agent
7
8 def get_wikidata_api():
9     session = mwapi.Session(host="https://wikidata.org/w/api.php", user_agent=user_agent)
10     return session
11
12 def search_wikidata(session, term, *args, **kwargs):
13     search_results = session.get(action='query',
14                                  list='search',
15                                  srsearch=term,
16 #                                 srqiprofile='popular_inclinks_pv',
17                                  srlimit='max',
18                                  srnamespace=0,
19                                  *args,
20                                  **kwargs)
21
22
23     query = search_results.get('query', None)
24     results = query.get('search', None)
25
26     if results is None:
27         raise mwapi.session.APIError(f"No results for query: {term}")
28
29     return results

Community Data Science Collective || Want to submit a patch?