ponyapi/ponyapi.nim

173 lines
3.6 KiB
Nim
Raw Normal View History

2015-08-14 02:47:00 +00:00
import asyncdispatch
import episode
2015-08-14 03:51:03 +00:00
import future
2015-08-14 02:20:23 +00:00
import jester
import json
2015-08-17 21:47:34 +00:00
import os
2015-08-14 03:51:03 +00:00
import random
import stats
2015-08-14 02:20:23 +00:00
import strutils
2015-08-14 03:51:03 +00:00
import times
2015-08-14 02:20:23 +00:00
var
episodes: seq[Episode]
for line in lines "./fim.list":
var
ep: Episode
splitLine = line.split " "
timestr = splitLine[1]
seasonstr = splitLine[2]
episodestr = splitLine[3]
is_movie = seasonstr == "99"
name = splitLine[4 .. ^1].join " "
ep = Episode(name: name,
air_date: timestr.parseInt,
season: seasonstr.parseInt,
episode: episodestr.parseInt,
is_movie: is_movie)
episodes = episodes & ep
2015-08-14 03:51:03 +00:00
proc `%%`(ep: Episode): JsonNode =
## Pack an episode for PonyAPI clients.
2015-08-14 03:51:03 +00:00
%*
{
"episode": ep,
}
proc `%%`(eps: seq[Episode]): JsonNode =
## Pack a sequence of episodes to a JsonNode for PonyAPI clients.
2015-08-14 03:51:03 +00:00
%*
{
"episodes": eps,
}
2015-09-08 22:20:22 +00:00
proc `%%`(why: string): JsonNode =
## Make an error object
%*
{
"error": why
}
2015-09-08 22:20:22 +00:00
template httpReply(code, body: expr): expr =
## Make things a lot simpler for replies, etc.
resp code, myHeaders, pretty(%%body, 4)
2015-08-14 03:51:03 +00:00
let myHeaders = {
"Content-Type": "application/json",
"X-Powered-By": "Nim and Jester",
"X-Git-Hash": getEnv("GIT_REV"),
"X-Server-Epoch": $ getTime().toSeconds().int,
2015-08-14 03:51:03 +00:00
}
2015-08-14 02:47:00 +00:00
settings:
port = 5000.Port
bindAddr = "0.0.0.0"
routes:
get "/":
"http://github.com/Xe/PonyAPI".uri.redirect
get "/all":
stats.all.success.inc
2015-09-08 22:20:22 +00:00
httpReply Http200, episodes
2015-08-14 03:51:03 +00:00
get "/newest":
var
now = getTime()
ep: Episode
for episode in episodes:
var then = times.fromSeconds(episode.air_date)
if now < then:
ep = episode
break
stats.newest.success.inc
2015-09-08 22:20:22 +00:00
httpReply Http200, ep
2015-08-14 03:51:03 +00:00
get "/random":
stats.newest.success.inc
2015-09-08 22:20:22 +00:00
httpReply Http200, episodes.randomChoice()
2015-08-14 03:51:03 +00:00
2015-08-17 21:32:05 +00:00
get "/last_aired":
var
now = getTime()
ep: Episode
for epid, episode in pairs[Episode](episodes):
var then = times.fromSeconds(episode.air_date)
if now < then:
ep = episodes[epid-1]
break
stats.lastAired.success.inc
2015-09-08 22:20:22 +00:00
httpReply Http200, ep
2015-08-17 21:32:05 +00:00
2015-08-14 03:51:03 +00:00
get "/season/@snumber":
var
season: int = @"snumber".parseInt
eps: seq[Episode] = lc[x | (x <- episodes, x.season == season), Episode]
if eps.len == 0:
stats.seasonLookup.fails.inc
2015-09-08 22:20:22 +00:00
httpReply Http404, "No episodes found"
2015-08-14 03:51:03 +00:00
else:
stats.seasonLookup.success.inc
2015-09-08 22:20:22 +00:00
httpReply Http200, eps
2015-08-14 03:51:03 +00:00
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:
stats.episodeLookup.fails.inc
2015-09-08 22:20:22 +00:00
httpReply Http404, "Not found"
2015-08-14 03:51:03 +00:00
else:
stats.episodeLookup.success.inc
2015-09-08 22:20:22 +00:00
httpReply Http200, ep
2015-08-14 03:51:03 +00:00
get "/search":
var
query = @"q".toLower
2015-08-14 03:56:02 +00:00
if query == "":
stats.search.fails.inc
2015-09-08 22:20:22 +00:00
halt Http406, myHeaders, pretty(%%"Need to specify a query", 4)
2015-08-14 03:56:02 +00:00
var
eps: seq[Episode] =
lc[x | (x <- episodes, x.name.toLower.contains query), Episode]
2015-08-14 02:47:00 +00:00
2015-08-14 03:51:03 +00:00
if eps.len == 0:
stats.search.fails.inc
2015-09-08 22:20:22 +00:00
httpReply Http404, "No episodes found"
2015-08-14 03:51:03 +00:00
else:
stats.search.success.inc
2015-09-08 22:20:22 +00:00
httpReply Http200, eps
2015-08-14 02:47:00 +00:00
get "/_stats":
resp Http200, myHeaders, pretty(%*
[
stats.all,
stats.newest,
stats.random,
stats.lastAired,
stats.seasonLookup,
stats.episodeLookup,
stats.search,
], 4)
2015-08-14 02:47:00 +00:00
runForever()