2015-08-12 19:45:05 +00:00
|
|
|
import cgi
|
2015-08-12 18:34:49 +00:00
|
|
|
import httpclient
|
|
|
|
import json
|
|
|
|
|
2015-08-12 20:24:28 +00:00
|
|
|
## A bunch of glue for Nim applications to query PonyAPI.
|
|
|
|
|
2015-08-12 18:34:49 +00:00
|
|
|
type
|
|
|
|
Episode* = object of RootObj
|
2015-08-12 20:24:28 +00:00
|
|
|
## An episode of My Little Pony: Friendship is Magic
|
|
|
|
name*: string ## Episode name
|
|
|
|
air_date*: int ## Air date in unix time
|
|
|
|
season*: int ## season number of the episode
|
|
|
|
episode*: int ## the episode number in the season
|
|
|
|
is_movie*: bool ## does this record represent a movie?
|
2015-08-12 18:34:49 +00:00
|
|
|
|
2015-08-12 23:29:45 +00:00
|
|
|
when defined(isTesting):
|
|
|
|
const
|
|
|
|
API_ENDPOINT: string = "http://127.0.0.1:5000"
|
|
|
|
else:
|
|
|
|
const
|
|
|
|
API_ENDPOINT: string = "http://ponyapi.apps.xeserv.us"
|
2015-08-12 18:34:49 +00:00
|
|
|
|
|
|
|
proc getJson(endpoint: string): json.JsonNode =
|
2015-08-12 20:24:28 +00:00
|
|
|
## get the HTTP body for the API base endpoint catted with the specific endpoint
|
|
|
|
## requested.
|
2015-08-12 18:34:49 +00:00
|
|
|
var
|
2015-08-12 23:29:45 +00:00
|
|
|
data = httpclient.getContent(API_ENDPOINT & endpoint)
|
2015-08-12 18:34:49 +00:00
|
|
|
|
2015-08-12 20:24:28 +00:00
|
|
|
data.parseJson
|
2015-08-12 18:34:49 +00:00
|
|
|
|
2015-08-12 19:13:24 +00:00
|
|
|
proc newEpisodeFromNode(data: json.JsonNode): Episode =
|
2015-08-12 20:24:28 +00:00
|
|
|
## Convert a json node into an episode object
|
2015-08-12 19:13:24 +00:00
|
|
|
Episode(name: data["name"].getStr,
|
|
|
|
air_date: data["air_date"].getNum.int,
|
|
|
|
season: data["season"].getNum.int,
|
|
|
|
episode: data["episode"].getNum.int,
|
|
|
|
is_movie: data["is_movie"].getBVal)
|
|
|
|
|
2015-08-12 19:29:40 +00:00
|
|
|
proc newEpisodeListFromNode(data: json.JsonNode): seq[Episode] =
|
2015-08-12 20:24:28 +00:00
|
|
|
## Convert a json array into a sequence of episode objects
|
2015-08-12 19:29:40 +00:00
|
|
|
var ret: seq[Episode]
|
|
|
|
for item in data.items():
|
|
|
|
ret = ret & item.newEpisodeFromNode
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
2015-08-12 18:34:49 +00:00
|
|
|
proc newest*(): Episode =
|
2015-08-12 20:24:28 +00:00
|
|
|
## returns information on the newest episode of My Little Pony: Friendship is Magic.
|
2015-08-12 19:13:24 +00:00
|
|
|
getJson("/newest")["episode"].newEpisodeFromNode
|
2015-08-12 18:34:49 +00:00
|
|
|
|
2015-08-12 19:13:24 +00:00
|
|
|
proc random*(): Episode =
|
2015-08-12 20:24:28 +00:00
|
|
|
## returns information on a random episode.
|
2015-08-12 19:13:24 +00:00
|
|
|
getJson("/random")["episode"].newEpisodeFromNode
|
2015-08-12 18:34:49 +00:00
|
|
|
|
2015-08-12 19:13:24 +00:00
|
|
|
proc get_episode*(season, episode: int): Episode =
|
2015-08-12 20:24:28 +00:00
|
|
|
## return an arbitrary episode by season, episode pair.
|
2015-08-12 19:13:24 +00:00
|
|
|
getJson("/season/" & $season & "/episode/" & $episode)["episode"].newEpisodeFromNode
|
2015-08-12 18:34:49 +00:00
|
|
|
|
2015-08-12 19:29:40 +00:00
|
|
|
proc all_episodes*(): seq[Episode] =
|
2015-08-12 20:24:28 +00:00
|
|
|
## return all information on all episodes.
|
2015-08-12 19:29:40 +00:00
|
|
|
getJson("/all")["episodes"].newEpisodeListFromNode
|
|
|
|
|
|
|
|
proc get_season*(season: int): seq[Episode] =
|
2015-08-12 20:24:28 +00:00
|
|
|
## return all information on a single season.
|
2015-08-12 19:29:40 +00:00
|
|
|
getJson("/season/" & $season)["episodes"].newEpisodeListFromNode
|
|
|
|
|
2015-08-12 19:45:05 +00:00
|
|
|
proc search*(term: string): seq[Episode] =
|
2015-08-12 20:24:28 +00:00
|
|
|
## searches for episodes by the given query term.
|
2015-08-12 19:45:05 +00:00
|
|
|
getJson("/search?q=" & term.encodeURL())["episodes"].newEpisodeListFromNode
|
|
|
|
|
2015-08-12 18:34:49 +00:00
|
|
|
when isMainModule:
|
|
|
|
import unittest
|
|
|
|
|
2015-08-12 19:29:40 +00:00
|
|
|
echo "Running API tests"
|
|
|
|
|
2015-08-12 18:34:49 +00:00
|
|
|
suite "ponyapi tests":
|
|
|
|
var ep: Episode
|
|
|
|
|
|
|
|
test "Newest episode lookup":
|
|
|
|
try:
|
|
|
|
ep = newest()
|
|
|
|
|
|
|
|
except:
|
|
|
|
echo getCurrentExceptionMsg()
|
|
|
|
fail
|
|
|
|
|
|
|
|
test "stringify episode":
|
|
|
|
echo ep
|
2015-08-12 19:13:24 +00:00
|
|
|
|
|
|
|
test "random episode":
|
|
|
|
echo random()
|
|
|
|
|
|
|
|
test "get episode":
|
|
|
|
var myEp = get_episode(2, 14) # best episode
|
|
|
|
assert myEp.name == "The Last Roundup"
|
2015-08-12 19:29:40 +00:00
|
|
|
|
|
|
|
test "get all episodes":
|
|
|
|
try:
|
|
|
|
var all = all_episodes()
|
|
|
|
assert all.len > 100
|
|
|
|
|
|
|
|
except:
|
|
|
|
echo getCurrentExceptionMsg()
|
|
|
|
fail
|
|
|
|
|
|
|
|
test "get information on season 1":
|
|
|
|
try:
|
|
|
|
var eps = get_season(1)
|
|
|
|
|
|
|
|
assert eps.len == 26
|
|
|
|
|
|
|
|
except:
|
|
|
|
echo getCurrentExceptionMsg()
|
|
|
|
fail
|
2015-08-12 19:45:05 +00:00
|
|
|
|
|
|
|
test "search for pony":
|
|
|
|
try:
|
|
|
|
var eps = search("pony")
|
|
|
|
|
|
|
|
assert eps.len > 0
|
|
|
|
|
|
|
|
except:
|
|
|
|
echo getCurrentExceptionMsg()
|
|
|
|
fail
|
|
|
|
|