-
Notifications
You must be signed in to change notification settings - Fork 4
/
search.py
66 lines (42 loc) · 2.17 KB
/
search.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
60
61
62
63
64
65
# -*- coding: utf-8 -*-
"""search.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1v78qzCEkXQHXfzHgLSbxkMqIbmioGuav
Import the libraries so that they can be used within the notebook
* **requests** is used to make HTTP calls
* **json** is used to encode and decode strings into JSON
* **string** is used to perform text manipulation and checking
"""
import requests
import json
import string
"""The **remove_non_printable** function ensures that the returned text does not contain any unprintable characters"""
def remove_non_printable(text):
return ''.join(i for i in text if i in string.printable)
"""The **base_url** holds the URL to the SEEK instance that will be used in the notebook
**headers** holds the HTTP headers that will be sent with every HTTP call
* **Content-type: application/vnd.api+json** - indicates that any data sent will be in JSON API format
* **Accept: application/vnd.api+json** - indicates that the notebook expects any data returned to be in JSON API format
* **Accept-Charset: ISO-8859-1** - indicates that the notebook expects any text returned to be in ISO-8859-1 character set
"""
base_url = 'http://www.fairdomhub.org/'
headers = {"Content-type": "application/vnd.api+json",
"Accept": "application/vnd.api+json",
"Accept-Charset": "ISO-8859-1"}
"""**term** holds the search term entered by the user"""
term = input("Enter the search term: ")
"""The **payload** indicates that the parameters to the HTTP call will be **?q=** *term*
The notebook then makes a HTTP GET using the search using the **headers** and the **payload** parameter
The result of the **GET** is within **r**.
If the request failed then raise an error
"""
payload = {'q' : term}
r = requests.get(base_url + 'search', headers=headers, params=payload)
r.raise_for_status()
"""For every entry in the returned JSON, print its **type** and **title**."""
for entry in r.json()['data'] :
title = entry['attributes']['title']
entry_type = entry['type']
print ("Type: {0} Title: {1}".format(remove_non_printable(entry_type),
remove_non_printable(title)))