-
Notifications
You must be signed in to change notification settings - Fork 0
/
CloudFunctionsWikipediaQuery.py
59 lines (46 loc) · 2.2 KB
/
CloudFunctionsWikipediaQuery.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#####################################################################
# #
# SkillsFuture IBM Cloud Function Example #
# This example is used to show how to get data from Wikipedia #
# and return it to Watson Assistant. #
# #
# input JSON: { "query": "Legend of Zelda"} #
# #
# WL IBM - 27 Aug 2019 #
# #
#####################################################################
import sys
import json
import requests
#our function to build the results for top 3
def WikiQueryResultsBuilder(querydata):
maxtotalsearchresults = 3
counter = 1
results = "Wikipedia Search Results:<br>"
for a_query in querydata:
if counter <= maxtotalsearchresults:
title = a_query["title"]
pageid = a_query["pageid"]
results = results + '<a href="https://en.wikipedia.org/?curid=' + str(pageid) + '">' + str(counter) + ": " + title + '</a><br>'
counter = counter + 1
#invalidate our results if no results
if len(querydata) == 0:
results = ""
return results
def main(dict):
#create defaults for our variables
query = ""
#first, lets deconstruct the input variable
if "query" in dict:
query = dict["query"]
#the action below will get data from data.gov.sg, save it as responseData
response = requests.get('https://en.wikipedia.org/w/api.php?action=query&list=search&utf8=&format=json&srsearch=' + query)
responsedata = response.json()
querydata = responsedata["query"]["search"]
#this will extract the forecast for the area, for example, Changi
resultsquery = WikiQueryResultsBuilder(querydata)
#craft our return data. the return data must be JSON
results = {
"results" : resultsquery,
}
return results