ponyapi/ponyapi.nim

186 lines
4.0 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
2016-07-03 04:53:36 +00:00
import httpcore
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
}
proc `%`(why: string): JsonNode =
%%why
2015-09-08 22:20:22 +00:00
template httpReply(code, body: expr): expr =
## Make things a lot simpler for replies, etc.
if request.headers.getOrDefault("X-API-Options") == "bare" or @"options" == "bare":
# New "bare" reply format, easier to scrape, etc.
2016-07-03 04:53:36 +00:00
resp code, "application/json", pretty(%body, 4)
else:
2016-07-03 04:53:36 +00:00
resp code, "application/json", pretty(%%body, 4)
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
2015-11-29 00:57:07 +00:00
if ep.season == 0:
stats.newest.fails.inc
2016-01-16 16:07:44 +00:00
halt Http404, "No new episode found, hiatus?"
2015-11-29 00:57:07 +00:00
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
2016-07-03 04:53:36 +00:00
httpReply Http200, episodes[epochTime().int mod len(episodes)]
2015-08-14 03:51:03 +00:00
2015-08-17 21:32:05 +00:00
get "/last_aired":
var
now = getTime()
2015-08-17 21:32:05 +00:00
ep: Episode
for epid, episode in pairs[Episode](episodes):
var then = times.fromSeconds(episode.air_date)
if now < then:
ep = episodes[epid-1]
break
2015-08-17 21:32:05 +00:00
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
2015-11-07 16:22:25 +00:00
if @"format" == "irccmd":
let
irccmd = "/cs episode del $1 $2\n/cs episode add $1 $2 $3 $4" % [$ep.season, $ep.episode, $ep.air_date, ep.name]
echo irccmd
2015-08-14 03:51:03 +00:00
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
2016-07-03 04:53:36 +00:00
halt Http406, 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":
2016-07-03 04:53:36 +00:00
resp Http200, pretty(%*
[
stats.all,
stats.newest,
stats.random,
stats.lastAired,
stats.seasonLookup,
stats.episodeLookup,
stats.search,
], 4)
2015-10-15 18:50:13 +00:00
when isMainModule:
runForever()
else:
quit "This should not be called outside of being the main module"