2015-08-09 19:48:49 +00:00
|
|
|
import os
|
2015-08-09 20:15:43 +00:00
|
|
|
import random
|
2015-08-09 20:35:47 +00:00
|
|
|
from flask import Flask, abort, jsonify, request
|
2015-08-09 19:48:49 +00:00
|
|
|
|
2015-08-09 20:15:43 +00:00
|
|
|
# An Episode is constructed as such:
|
|
|
|
# data Episode = Episode
|
|
|
|
# { name :: String
|
|
|
|
# , air_date :: Int
|
|
|
|
# , season :: Int
|
|
|
|
# , episode :: Int
|
|
|
|
# , is_movie :: Bool
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# Any instance of season 99 should be interpreted as a movie.
|
|
|
|
episodes = []
|
|
|
|
|
|
|
|
# First, open the input file and read in episodes
|
|
|
|
with open("./fim.list", "r") as f:
|
|
|
|
for line in f:
|
|
|
|
airdate, s, e, name = line.split(' ', 4)[1:]
|
|
|
|
|
|
|
|
episode = {
|
|
|
|
"name": name[:-1],
|
|
|
|
"air_date": int(airdate),
|
|
|
|
"season": int(s),
|
|
|
|
"episode": int(e),
|
|
|
|
"is_movie": s == "99"
|
|
|
|
}
|
|
|
|
|
|
|
|
episodes.append(episode)
|
|
|
|
|
|
|
|
# Now, initialize Flask
|
2015-08-09 19:48:49 +00:00
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
def hello():
|
|
|
|
return "Welcome to the Pony API!"
|
|
|
|
|
2015-08-09 20:15:43 +00:00
|
|
|
@app.route("/all")
|
|
|
|
def all_episodes():
|
|
|
|
return jsonify(episodes=episodes)
|
|
|
|
|
|
|
|
@app.route("/season/<number>")
|
|
|
|
def season(number):
|
|
|
|
retEpisodes = []
|
|
|
|
|
|
|
|
for episode in episodes:
|
|
|
|
if str(episode["season"]) == number:
|
|
|
|
retEpisodes.append(episode)
|
|
|
|
|
|
|
|
try:
|
|
|
|
assert len(retEpisodes) > 0
|
|
|
|
except:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
return jsonify(episodes=retEpisodes)
|
|
|
|
|
2015-08-09 20:20:19 +00:00
|
|
|
@app.route("/season/<snumber>/episode/<epnumber>")
|
|
|
|
def show_episode(snumber, epnumber):
|
|
|
|
for episode in episodes:
|
|
|
|
if str(episode["season"]) == snumber and str(episode["episode"]) == epnumber:
|
|
|
|
return jsonify(episode=episode)
|
|
|
|
|
|
|
|
abort(404)
|
|
|
|
|
2015-08-09 20:15:43 +00:00
|
|
|
@app.route("/random")
|
|
|
|
def show_random_ep():
|
|
|
|
return jsonify(episode=random.choice(episodes))
|
|
|
|
|
2015-08-09 19:48:49 +00:00
|
|
|
@app.route("/search")
|
|
|
|
def search():
|
2015-08-09 20:35:47 +00:00
|
|
|
retEpisodes = []
|
2015-08-09 20:37:08 +00:00
|
|
|
term = request.args.get("q", "").lower()
|
2015-08-09 20:35:47 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
assert term != ""
|
|
|
|
except:
|
|
|
|
abort(406)
|
|
|
|
|
|
|
|
for episode in episodes:
|
|
|
|
if term in episode["name"].lower():
|
|
|
|
retEpisodes.append(episode)
|
|
|
|
|
|
|
|
try:
|
|
|
|
assert len(retEpisodes) > 0
|
|
|
|
except:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
return jsonify(episodes=retEpisodes)
|
2015-08-09 20:15:43 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(host="0.0.0.0", debug=True)
|