f2fac889c3af2d4847f1a3462b7f0681dddbe9f9
[covid19.git] / transliterations / src / wikidata_api_calls.py
1 # File defines functions for making api calls to find translations and transliterations for key terms.
2 import mwapi
3 import requests
4 import sys
5 import time
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",params={"query":q,"format":"json"})
33     time.sleep(2)
34     return results
35

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