-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
94 lines (84 loc) · 2.42 KB
/
main.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
94
import requests
import os
from dotenv import load_dotenv
import json
from flask import Flask, render_template, request, send_file
import random
app = Flask(__name__)
load_dotenv()
apikey = os.getenv('API_KEY')
@app.route('/')
def home_page():
"""
Displays the homepage for starwars app
"""
return render_template('home.html')
@app.route('/char', methods=['GET', 'POST'])
def character_page():
"""
Displays the the character page for SWAPI
"""
character_id = request.args.get('character-id')
limit = 1
if not character_id:
character_id = '1'
response = requests.get(f"https://swapi.py4e.com/api/people/{character_id}/")
character_data = response.json()
try:
films = [requests.get(film).json()['title'] for film in character_data['films']]
except KeyError:
print('KeyError: Films Not Found')
films = ['N/A']
try:
character_name = character_data['name']
except KeyError:
print('KeyError: Character Not Found')
character_name = 'star wars'
response = requests.get(
f"https://tenor.googleapis.com/v2/search?q={character_name}&key={apikey}&limit={limit}")
gifs = json.loads(response.content).get('results')
context = {
"character": character_data,
'films': films,
'gifs': gifs
}
return render_template('char.html', **context)
@app.route('/films')
def film_page():
"""
Displays the film page for SWAPI
"""
number = random.randint(1, 7)
response = requests.get(f"https://swapi.py4e.com/api/films/{number}/")
data = response.json()
context = {
"film": data
}
return render_template('films.html', **context)
@app.route('/planets')
def planet_page():
"""
Displays the planet page for SWAPI
"""
number = random.randint(1, 62)
response = requests.get(f"https://swapi.py4e.com/api/planets/{number}/")
data = response.json()
context = {
"planet": data,
}
return render_template('planets.html', **context)
@app.route('/vehicles')
def vehicle_page():
"""
Displays the vehicle page for SWAPI
"""
number = random.randint(1, 7)
response = requests.get(f"https://swapi.py4e.com/api/vehicles/{number}/")
data = response.json()
context = {
"vehicle": data,
}
return render_template('vehicles.html', **context)
if __name__ == '__main__':
app.config['ENV'] = 'development'
app.run(debug=True, port=3000)