-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathutils.py
More file actions
30 lines (26 loc) · 781 Bytes
/
Copy pathutils.py
File metadata and controls
30 lines (26 loc) · 781 Bytes
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
import requests
import ujson as json
try:
from swapi import exceptions
except:
import exceptions
def query(query):
headers = {'User-Agent': 'swapi-python'}
response = requests.get(query, headers=headers)
if response.status_code != 200:
raise exceptions.ResourceDoesNotExist('Resource does not exist')
return response
def all_resource_urls(query):
''' Get all the URLs for every resource '''
urls = []
next = True
while next:
response = requests.get(query)
json_data = json.loads(response.content)
for resource in json_data['results']:
urls.append(resource['url'])
if bool(json_data['next']):
query = json_data['next']
else:
next = False
return urls