-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
297 lines (254 loc) · 11.5 KB
/
Copy pathmain.py
File metadata and controls
297 lines (254 loc) · 11.5 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import db_operations as db
import requests
import json
from psycopg2 import sql
import re
import logging
PRINT = True
def retrieve_source(URL):
page = requests.get(URL)
return page.json()
def insert_film(id):
# title, episode_id, opening_crawl, director, producer, release_date, characters, planets, starships, vehicles, species, created, edited, url, film_id
json_person = retrieve_source(f'https://swapi.dev/api/films/{id}/')
row = [json_person[col] for col in json_person if(col not in ['characters', 'starships', 'vehicles', 'species', 'planets'])]
row.append(id)
conn, cursor = db.connect()
query = sql.SQL('''INSERT INTO swapi.films (title, episode_id, opening_crawl, director,
producer, release_date, created, edited, url, film_id) VALUES (%s, %s, %s, %s, %s,
%s, %s, %s, %s, %s)''')
cursor.execute(query, [r if(str(r) != 'unknown') else None for r in row])
db.close(conn, cursor)
def insert_planet(id):
# name, rotation_period, orbital_period, diameter, climate, gravity, terrain, surface_water, population, residents, films, created, edited, url, planet_id
json_person = retrieve_source(f'https://swapi.dev/api/planets/{id}/')
row = [json_person[col] for col in json_person if(col not in ['films', 'characters', 'starships', 'vehicles', 'species', 'residents'])]
row.append(id)
conn, cursor = db.connect()
query = sql.SQL('''INSERT INTO swapi.planets (name, rotation_period, orbital_period,
diameter, climate, gravity, terrain, surface_water, population, created, edited, url, planet_id) VALUES (%s, %s, %s, %s, %s,
%s, %s, %s, %s, %s, %s, %s, %s)''')
cursor.execute(query, [r if(str(r) != 'unknown') else None for r in row])
db.close(conn, cursor)
def insert_species(id):
# name, classification, designation, average_height, skin_colors, hair_colors, eye_colors, average_lifespan, homeworld, language, people, films, created, edited, url, species_id
json_person = retrieve_source(f'https://swapi.dev/api/species/{id}/')
row = [json_person[col] for col in json_person if(col not in ['films', 'people', 'starships', 'vehicles', 'species', 'residents'])]
row.append(id)
conn, cursor = db.connect()
query = sql.SQL('''INSERT INTO swapi.species (name, classification, designation, average_height, skin_colors,
hair_colors, eye_colors, average_lifespan, homeworld, language, created, edited, url, species_id) VALUES (%s, %s, %s, %s, %s,
%s, %s, %s, %s, %s, %s, %s, %s, %s)''')
cursor.execute(query, [r if(str(r) != 'unknown') else None for r in row])
db.close(conn, cursor)
def insert_vehicles(id):
# name, model, manufacturer, cost_in_credits, length, max_atmosphering_speed, crew, passengers, cargo_capacity, consumables, vehicle_class, pilots, films, created, edited, url, vehicle_id
json_person = retrieve_source(f'https://swapi.dev/api/vehicles/{id}/')
row = [json_person[col] for col in json_person if(col not in ['films', 'pilots'])]
row.append(id)
conn, cursor = db.connect()
query = sql.SQL('''INSERT INTO swapi.vehicles (name, model, manufacturer, cost_in_credits, length, max_atmosphering_speed, crew, passengers, cargo_capacity,
consumables, vehicle_class, created, edited, url, vehicle_id) VALUES (%s, %s, %s, %s, %s,
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)''')
cursor.execute(query, [r if(str(r) != 'unknown') else None for r in row])
db.close(conn, cursor)
def insert_person(id):
# [name, height, mass, hair_color, skin_color, eye_color, birth_year, gender, homeworld, created, edited, url, people_id]
json_person = retrieve_source(f'https://swapi.dev/api/people/{id}/')
row = [json_person[col] for col in json_person if(col not in ['films', 'starships', 'vehicles', 'species'])]
row.append(id)
conn, cursor = db.connect()
query = sql.SQL('''INSERT INTO swapi.people (name, height, mass, hair_color,
skin_color, eye_color, birth_year, gender, homeworld, created, edited, url, people_id) VALUES (%s, %s, %s, %s, %s,
%s, %s, %s, %s, %s, %s, %s, %s)''')
cursor.execute(query, [str(r).replace(',', '') if(str(r) != 'unknown') else None for r in row])
db.close(conn, cursor)
def insert_starships(id):
json_person = retrieve_source(f'https://swapi.dev/api/starships/{id}/')
row = [json_person[col] for col in json_person if(col not in ['films', 'starships', 'vehicles', 'pilots'])]
row.append(id)
conn, cursor = db.connect()
query = sql.SQL('''INSERT INTO swapi.starships (name, model, manufacturer, cost_in_credits, length, max_atmosphering_speed, crew, passengers,
cargo_capacity, consumables, hyperdrive_rating, MGLT, starship_class, created, edited, url, starship_id)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)''')
cursor.execute(query, [r if(str(r) != 'unknown') else None for r in row])
db.close(conn, cursor)
def insert_film_vehicles(film_id, vehicle_id):
conn, cursor = db.connect()
query = sql.SQL('''INSERT INTO swapi.film_vehicles (film_id, vehicles_id) VALUES (%s, %s)''')
try:
cursor.execute(query, (film_id, vehicle_id))
except:
pass
db.close(conn, cursor)
def insert_film_species(film_id, species_id):
conn, cursor = db.connect()
query = sql.SQL('''INSERT INTO swapi.film_species (film_id, species_id) VALUES (%s, %s)''')
try:
cursor.execute(query, (film_id, species_id))
except:
pass
db.close(conn, cursor)
def insert_film_planet(film_id, planet_id):
conn, cursor = db.connect()
query = sql.SQL('''INSERT INTO swapi.film_planety (film_id, planet_id) VALUES (%s, %s)''')
try:
cursor.execute(query, (film_id, planet_id))
except:
pass
db.close(conn, cursor)
def insert_film_starships(film_id, starships_id):
conn, cursor = db.connect()
query = sql.SQL('''INSERT INTO swapi.film_starships (film_id, starships_id) VALUES (%s, %s)''')
try:
cursor.execute(query, (film_id, starships_id))
except:
pass
db.close(conn, cursor)
def insert_people_starships(people_id, starships_id):
conn, cursor = db.connect()
query = sql.SQL('''INSERT INTO swapi.people_starships (people_id, starships_id) VALUES (%s, %s)''')
try:
cursor.execute(query, (people_id, starships_id))
except:
pass
db.close(conn, cursor)
def insert_people_planet(people_id, planet_id):
conn, cursor = db.connect()
query = sql.SQL('''INSERT INTO swapi.people_planet (people_id, planet_id) VALUES (%s, %s)''')
try:
cursor.execute(query, (people_id, planet_id))
except:
pass
db.close(conn, cursor)
def insert_people_films(people_id, film_id):
conn, cursor = db.connect()
query = sql.SQL('''INSERT INTO swapi.people_film (people_id, film_id) VALUES (%s, %s)''')
try:
cursor.execute(query, (people_id, film_id))
except:
pass
db.close(conn, cursor)
def get_all_relations(type_, id):
"""
Will return a list with all the relations of the SW object.
Args:
type_ (string): films/people/vehicles/starships/species/planets
id (int): id of object
Returns:
[type]: [description]
"""
j = retrieve_source('https://swapi.dev/api/'+ type_ + f'/{id}/')
r_list = []
for key in j:
if(type(j[key]) is list):
for item in j[key]:
r_list.append(item.split('/')[-3:-1])
return r_list
def insert_generic(type, id):
"""Inserts a generit SW object into the database.
Will check the objects type and call the appropriate function.
Args:
type (string)
id (int): id of object
"""
if(type == 'films'):
insert_film(id)
if(type == 'planets'):
insert_planet(id)
if(type == 'species'):
insert_species(id)
if(type == 'vehicles'):
insert_vehicles(id)
if(type == 'people'):
insert_person(id)
if(type=='starships'):
insert_starships(id)
def insert_relation(rel):
"""Given a list with a relation it will add to the database that relation.
note that both items must be in the database beforehand!
Args:
rel (list): list containing 2 relation-elements.
example:
[['people', 1],['films', 1]]
Returns:
nothing
"""
conn, cursor = db.connect()
table1, table2 = 0, 0
table = [0, 0]
if(rel[0][0]=='people'):
table1, table2 = rel[0], rel[1]
elif(rel[1][0]=='people'):
table1, table2 = rel[1], rel[0]
elif(rel[0][0]=='films'):
table1, table2 = rel[0], rel[1]
elif(rel[1][0]=='films'):
table1, table2 = rel[1], rel[0]
elif(rel[0][0]=='planet'):
table1, table2 = rel[0], rel[1]
elif(rel[1][0]=='planet'):
table1, table2 = rel[1], rel[0]
else:
return 0
table1[0] = re.sub(r's$', '', table1[0]) if(table1[0] not in ('species')) else 'species'
table2[0] = re.sub(r's$', '', table2[0]) if(table2[0] not in ('species')) else 'species'
table = re.sub(r'[\[\]\']', '', str(table1[0])) + '_' + re.sub(r'[\[\]\']', '', str(table2[0]))
query = sql.SQL('''INSERT INTO swapi.{table} ({table1}, {table2}) VALUES (%s, %s)''').format(
table=sql.Identifier(table),
table1=sql.Identifier(table1[0]+'_id'),
table2=sql.Identifier(table2[0]+'_id'))
try:
cursor.execute(query, (table1[1], table2[1]))
if(PRINT):
logging.info(f'Inserted relations between:{table1} and {table2}')
except:
if(PRINT):
logging.info(f'Error while inserting relation, {table} {table1[0]} {table2[0]}')
db.close(conn, cursor)
def six_degrees_from_luke():
"""[Main function that will populate the Postgres Database]
- This function will populate the Database starting with Luke Skywalker and then
- adding all his connections plus their connections and so on.
- This way we do not need to have access to the hosts database in order to scrape the whole data.
- This was done with that challenge in mind. There are more efficient ways to populate the database
- if we access the page with all the id's of the films/people/species/... in the SW universe.
-
- If you run this a second time then uncomment the first line of this function to reset the database.
-
- May the force be with you!s
"""
# db.delete_swapi_db()
db.create_swapi_db()
luke = ['people', 1]
pipeline = get_all_relations(luke[0], luke[1])
relations_pipeline = [(luke, rel) for rel in pipeline]
try:
insert_generic(luke[0], luke[1])
if(PRINT):
logging.info(f'Inserted: {luke[0]}/{luke[1]}')
except:
pass
inserted = [luke]
while(pipeline != []):
item = pipeline.pop(0)
try:
insert_generic(item[0],item[1])
if(PRINT):
logging.info(f'Inserted: {item[0]}/{item[1]}')
except :
logging.info(f'failed inserting {item[0]}, {item[1]}!')
inserted.append(item)
new_rel = get_all_relations(item[0], item[1])
for rel in new_rel:
if((([item[0], item[1]], rel) not in relations_pipeline) or (rel, [item[0], item[1]]) not in relations_pipeline):
relations_pipeline.append((item, rel))
for new in new_rel:
if((new in inserted) or (new in pipeline)):
continue
else:
pipeline.append(new)
for rel in relations_pipeline:
insert_relation(rel)
if __name__ == "__main__":
six_degrees_from_luke()