]> code.communitydata.science - covid19.git/blob - translations/src/wikidata_api_calls.py
0f706807f5cc4b38a872c6fbb2ea01db3402dfde
[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 requests
5 import sys
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
30
31 def run_sparql_query(q):
32     results = requests.get("https://query.wikidata.org/bigdata/namespace/wdq/sparql?query={q}&format=json")
33     

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