-
Notifications
You must be signed in to change notification settings - Fork 4
/
search_type.py
94 lines (65 loc) · 2.85 KB
/
search_type.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf-8 -*-
"""search_type.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1GjBNkarjfwIzmCChUyTEUAX498pnzri9
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: ")
"""**type_choices** lists the types of SEEK resources that can be searched for.
**response** holds the user's choice of type
"""
type_choices = ["assays",
"content_blobs",
"data_files",
"documents",
"events",
"models",
"organisms",
"people",
"presentations",
"programmes",
"projects",
"publications",
"sample_types",
"sops",
"studies"]
print ("Enter the type of object:")
response = None
while response not in type_choices:
response = input("Please enter one of: " + ', '.join(type_choices))
print (response)
"""The **payload** indicates that the parameters to the HTTP call will be **q=** *term* and **search_type=** *response*
The notebook then makes a HTTP GET using the search using the **headers** and the **payload** parameters
The result of the **GET** is within **r**.
If the request failed then raise an error
"""
payload = {'q' : term, 'search_type' : response}
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)))