implement remaining API calls
This commit is contained in:
parent
162052d17b
commit
a3ea1ed922
85
ponyapi.nim
85
ponyapi.nim
|
@ -1,7 +1,10 @@
|
||||||
import asyncdispatch
|
import asyncdispatch
|
||||||
|
import future
|
||||||
import jester
|
import jester
|
||||||
import json
|
import json
|
||||||
|
import random
|
||||||
import strutils
|
import strutils
|
||||||
|
import times
|
||||||
|
|
||||||
type
|
type
|
||||||
Episode* = object of RootObj
|
Episode* = object of RootObj
|
||||||
|
@ -46,11 +49,28 @@ proc `%`(ep: Episode): JsonNode =
|
||||||
proc `%`(eps: seq[Episode]): JsonNode =
|
proc `%`(eps: seq[Episode]): JsonNode =
|
||||||
var ret = newJArray()
|
var ret = newJArray()
|
||||||
|
|
||||||
for ep in episodes:
|
for ep in eps:
|
||||||
add ret, %ep
|
add ret, %ep
|
||||||
|
|
||||||
ret
|
ret
|
||||||
|
|
||||||
|
proc `%%`(ep: Episode): JsonNode =
|
||||||
|
%*
|
||||||
|
{
|
||||||
|
"episode": ep,
|
||||||
|
}
|
||||||
|
|
||||||
|
proc `%%`(eps: seq[Episode]): JsonNode =
|
||||||
|
%*
|
||||||
|
{
|
||||||
|
"episodes": eps,
|
||||||
|
}
|
||||||
|
|
||||||
|
let myHeaders = {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"X-Powered-By": "Nim and Jester",
|
||||||
|
}
|
||||||
|
|
||||||
settings:
|
settings:
|
||||||
port = 5000.Port
|
port = 5000.Port
|
||||||
bindAddr = "0.0.0.0"
|
bindAddr = "0.0.0.0"
|
||||||
|
@ -60,12 +80,63 @@ routes:
|
||||||
"http://github.com/Xe/PonyAPI".uri.redirect
|
"http://github.com/Xe/PonyAPI".uri.redirect
|
||||||
|
|
||||||
get "/all":
|
get "/all":
|
||||||
let headers = {"Content-Type": "application/json"}
|
resp Http200, myHeaders, pretty(%%episodes, 4)
|
||||||
var rep = %*
|
|
||||||
{
|
|
||||||
"episodes": episodes,
|
|
||||||
}
|
|
||||||
|
|
||||||
resp Http200, headers, pretty(rep, 4)
|
get "/newest":
|
||||||
|
var
|
||||||
|
now = getTime()
|
||||||
|
ep: Episode
|
||||||
|
|
||||||
|
for episode in episodes:
|
||||||
|
var then = times.fromSeconds(episode.air_date)
|
||||||
|
|
||||||
|
if now < then:
|
||||||
|
ep = episode
|
||||||
|
break
|
||||||
|
|
||||||
|
resp Http200, myHeaders, pretty(%%ep, 4)
|
||||||
|
|
||||||
|
get "/random":
|
||||||
|
resp Http200, myHeaders, pretty(%%episodes.randomChoice(), 4)
|
||||||
|
|
||||||
|
get "/season/@snumber":
|
||||||
|
var
|
||||||
|
season: int = @"snumber".parseInt
|
||||||
|
eps: seq[Episode] = lc[x | (x <- episodes, x.season == season), Episode]
|
||||||
|
|
||||||
|
if eps.len == 0:
|
||||||
|
resp Http404, myHeaders, $ %* { "error": "No episodes found" }
|
||||||
|
else:
|
||||||
|
resp Http200, myHeaders, pretty(%%eps, 4)
|
||||||
|
|
||||||
|
get "/season/@snumber/episode/@epnumber":
|
||||||
|
var
|
||||||
|
season: int = @"snumber".parseInt
|
||||||
|
enumber: int = @"epnumber".parseInt
|
||||||
|
ep: Episode
|
||||||
|
|
||||||
|
for episode in episodes:
|
||||||
|
if episode.season == season:
|
||||||
|
if episode.episode == enumber:
|
||||||
|
ep = episode
|
||||||
|
|
||||||
|
if ep.air_date == 0:
|
||||||
|
resp Http404, myHeaders, $ %* {"error": "No such episode"}
|
||||||
|
else:
|
||||||
|
resp Http200, myHeaders, pretty(%%ep, 4)
|
||||||
|
|
||||||
|
get "/search":
|
||||||
|
var
|
||||||
|
query = @"q".toLower
|
||||||
|
eps: seq[Episode]
|
||||||
|
|
||||||
|
for episode in episodes:
|
||||||
|
if episode.name.toLower.contains query:
|
||||||
|
eps = eps & episode
|
||||||
|
|
||||||
|
if eps.len == 0:
|
||||||
|
resp Http404, myHeaders, $ %* { "error": "No episodes found" }
|
||||||
|
else:
|
||||||
|
resp Http200, myHeaders, pretty(%%eps, 4)
|
||||||
|
|
||||||
runForever()
|
runForever()
|
||||||
|
|
|
@ -7,4 +7,4 @@ license = "MIT"
|
||||||
bin = "ponyapi"
|
bin = "ponyapi"
|
||||||
|
|
||||||
[Deps]
|
[Deps]
|
||||||
Requires: "nim >= 0.10.0, jester#head"
|
Requires: "nim >= 0.10.0, jester#head, random#head"
|
||||||
|
|
Loading…
Reference in New Issue