This repository was archived by the owner on Nov 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwolfpack.py
More file actions
240 lines (168 loc) · 7.89 KB
/
Copy pathwolfpack.py
File metadata and controls
240 lines (168 loc) · 7.89 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import xml.dom.minidom as minidom
import requests, json
with open("configs.json") as conf:
config = json.load(conf)
wolfram_app_id = config["wolfram_id"]
def build_request_url(input_string, options):
url = "http://api.wolframalpha.com/v2/query?appid=" + wolfram_app_id
url += "&input=" + input_string
url += "&format=plaintext"
for o in options:
url += "&includepodid=" + o
return url
def table_to_dict(table_as_string):
table_as_dict = {}
i = 0
ps = table_as_string.replace('\n', '|').split('|')
#print(ps)
while i < len(ps) - 1:
table_as_dict[ps[i].strip()] = ps[i+1].strip()
i += 2
return table_as_dict
def pod_to_plaintext(pod, plaintextField="plaintext"):
temp = pod.getElementsByTagName("subpod")[0]
propstring = temp.getElementsByTagName(plaintextField)[0].firstChild.nodeValue
return propstring
def process_cities(input_string, options=["Population:CityData", "Location:CityData", "EconomicProperties:CityData", "GeographicProperties:CityData", "BasicInformation:CityData", "NotablePeople:CityData"]):
# build request url
url = build_request_url(input_string, options)
print(url)
# do request
req = requests.get(url)
# initialize xml
dt = minidom.parseString(req.text)
collection = dt.documentElement
pods = collection.getElementsByTagName("pod")
for pod in pods:
# get population stats
if str(pod.getAttribute("title")) == "Populations":
propstring = pod_to_plaintext(pod)
population_stats = table_to_dict(propstring)
continue
# get location data
if str(pod.getAttribute("title")) == "Location":
location = pod_to_plaintext(pod)
continue
# get economic data
if str(pod.getAttribute("title")) == "Economic properties":
propstring = pod_to_plaintext(pod)
economic_properties = table_to_dict(propstring)
continue
if str(pod.getAttribute("title")) == "Geographic properties":
propstring = pod_to_plaintext(pod)
geographic_properties = table_to_dict(propstring)
continue
if str(pod.getAttribute("title")) == "Nickname":
nickname = pod_to_plaintext(pod)
continue
if "Notable people" in str(pod.getAttribute("title")):
notable_people = [p[:p.find('(')].strip() for p in pod_to_plaintext(pod).split('\n')]
return {"population" : population_stats, "location" : location, "economy" : economic_properties, "geography" : geographic_properties, "nickname" : nickname, "notable people" : notable_people}
def process_planet(input_string, options=["BasicPlanetOrbitalPropertiesEntityTriggered:PlanetData", "BasicPlanetPhysicalProperties:PlanetData", "PlanetAtmospheres:PlanetData", "Image:PlanetData"]):
# build request url
url = build_request_url(input_string, options)
print(url)
# do request
req = requests.get(url)
# initialize xml
dt = minidom.parseString(req.text)
collection = dt.documentElement
pods = collection.getElementsByTagName("pod")
for pod in pods:
# get orbital properties
if str(pod.getAttribute("title")) == "Orbital properties":
propstring = pod_to_plaintext(pod)
orbital_properties = table_to_dict(propstring)
continue
# get physical properties
if str(pod.getAttribute("title")) == "Physical properties":
propstring = pod_to_plaintext(pod)
physical_properties = table_to_dict(propstring)
continue
# get atmospheric data
if str(pod.getAttribute("title")) == "Atmosphere":
propstring = pod_to_plaintext(pod)
atmospheric_data = table_to_dict(propstring)
continue
# get image url
if str(pod.getAttribute("title")) == "Image":
image_url = pod_to_plaintext(pod, plaintextField="imagesource")
return {"orbital properties" : orbital_properties, "physical properties" : physical_properties, "atmospheric data" : atmospheric_data, "image url" : image_url}
def process_person(input_string, options=["BasicInformation:PeopleData", "Image:PeopleData", "NotableFacts:PeopleData", "PhysicalCharacteristics:PeopleData", "FamilialRelationships:PeopleData"]):
'''basic info, image, facts, physical caracteristics, family'''
# build request url
url = build_request_url(input_string, options)
print(url)
req = requests.get(url)
# xml parsing tools
dt = minidom.parseString(req.text)
collection = dt.documentElement
pods = collection.getElementsByTagName("pod")
#print(pods)
image_url = ""
facts = []
physical_characteristics = {}
family = {}
for pod in pods:
#print()
# get basic info
if(str(pod.getAttribute("title")) == "Basic information"):
propstring = pod_to_plaintext(pod)
basic_info = table_to_dict(propstring)
continue
if(str(pod.getAttribute("title")) == "Image"):
image_url = pod_to_plaintext(pod, plaintextField="imagesource")
continue
if(str(pod.getAttribute("title")) == "Notable facts"):
factstring = pod_to_plaintext(pod)
facts = factstring.split('\n')
continue
if(str(pod.getAttribute("title")) == "Physical characteristics"):
propstring = pod_to_plaintext(pod)
physical_characteristics = table_to_dict(propstring)
continue
if(str(pod.getAttribute("title")) == "Familial relationships"):
subs = pod.getElementsByTagName("subpod")
for sub in subs:
propstring = sub.getElementsByTagName("plaintext")[0].firstChild.nodeValue
#print(propstring)
members = [p.strip() for p in propstring.split('|')]
family[str(sub.getAttribute("title"))] = members
return {"basic information" : basic_info, "image url" : image_url, "facts" : facts, "physical characteristics" : physical_characteristics, "family" : family}
def process_animal(input_string, options=["ScientificName:SpeciesData", "Taxonomy:SpeciesData", "SpeciesDataPhysicalProperties"]):
# build request url
url = build_request_url(input_string, options)
print(url)
# do request
req = requests.get(url)
# Initialize xml parsing tools -- this turns the xml file into a tree that we can work with
dt = minidom.parseString(req.text)
collection = dt.documentElement
pods = collection.getElementsByTagName("pod")
scientific_name = ""
taxonomy = ""
biological_properties = {}
for pod in pods:
# get scientific name
if str(pod.getAttribute("title")) == "Scientific name":
scientific_name = pod_to_plaintext(pod)
continue
# Get Biological Properties
if str(pod.getAttribute("title")) == "Biological properties":
for sub in pod.getElementsByTagName("subpod"):
propstring = sub.getElementsByTagName("plaintext")[0].firstChild.nodeValue
#print(propstring)
biological_properties = {**biological_properties, **table_to_dict(propstring)}
continue
if str(pod.getAttribute("title")) == "Taxonomy":
temp = pod.getElementsByTagName("subpod")[0]
i = 0
taxstring = temp.getElementsByTagName("plaintext")[0].firstChild.nodeValue
ts = taxstring.replace('\n', '|').split('|')
taxonomy = []
for t in ts:
if i == 1:
taxonomy.append(t.strip())
i = (i+1) % 2
#print(taxonomy)
return {"scientific name" : scientific_name, "Taxonomy" : taxonomy, "biological properties" : biological_properties}