slight improvements and some retrieval endpoints
This commit is contained in:
parent
9b1dbb6f74
commit
cddb4db941
160
code/app.lua
160
code/app.lua
@ -1,13 +1,14 @@
|
|||||||
local lapis = require("lapis")
|
local lapis = require("lapis")
|
||||||
local config = require("lapis.config").get()
|
local config = require("lapis.config").get()
|
||||||
local to_json = require("lapis.util").to_json
|
local to_json = require("lapis.util").to_json
|
||||||
|
local json_params = require("lapis.application").json_params
|
||||||
local db = require("lapis.db")
|
local db = require("lapis.db")
|
||||||
|
|
||||||
local app = lapis.Application()
|
local app = lapis.Application()
|
||||||
|
|
||||||
local autoload = require("lapis.util").autoload
|
local autoload = require("lapis.util").autoload
|
||||||
local models = autoload("models")
|
local models = autoload("models")
|
||||||
|
local handlers = autoload("handlers")
|
||||||
|
|
||||||
app:get("/", function(self)
|
app:get("/", function(self)
|
||||||
-- return "Welcome to Lapis " .. require("lapis.version")
|
-- return "Welcome to Lapis " .. require("lapis.version")
|
||||||
@ -16,154 +17,15 @@ app:get("/", function(self)
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
app:match("/spl/:station", function(self)
|
app:match("/spl/:station", function(self)
|
||||||
-- print( to_json(self.params))
|
return handlers.Splhandler(self)
|
||||||
|
|
||||||
-- artist_name=%a&
|
|
||||||
-- track_name=%t&
|
|
||||||
-- release_name=%T
|
|
||||||
-- label=%L&
|
|
||||||
-- year=%Y&
|
|
||||||
-- country=%O&
|
|
||||||
-- info_url=%U1&
|
|
||||||
-- img_url=%U2&
|
|
||||||
-- time=%h&
|
|
||||||
-- date=%d&
|
|
||||||
|
|
||||||
-- artist_name=%a&track_name=%t&release_name=%Tlabel=%L&year=%Y&country=%O&info_url=%U1&img_url=%U2&time=%h&date=%d&
|
|
||||||
|
|
||||||
-- ----- --
|
|
||||||
-- setup --
|
|
||||||
-- ----- --
|
|
||||||
|
|
||||||
local station_id = self.params.station
|
|
||||||
|
|
||||||
local station = models.Stations:find({ station = station_id })
|
|
||||||
if not station then
|
|
||||||
print("unknown station: " .. station_id)
|
|
||||||
else
|
|
||||||
print("This is: " .. station["name"])
|
|
||||||
end
|
|
||||||
|
|
||||||
local artist = nil
|
|
||||||
local artist_name = self.params.artist_name
|
|
||||||
|
|
||||||
local track = nil
|
|
||||||
local track_name = self.params.track_name
|
|
||||||
local track_info_url = self.params.info_url
|
|
||||||
local track_img_url = self.params.img_url
|
|
||||||
|
|
||||||
local release = nil
|
|
||||||
local release_name = self.params.release_name
|
|
||||||
local release_year = self.params.year
|
|
||||||
local release_country = self.params.country
|
|
||||||
local release_label = self.params.label
|
|
||||||
|
|
||||||
local airtime = self.params.time
|
|
||||||
local airdate = self.params.date
|
|
||||||
|
|
||||||
-- we have to split the `hr:min:sec` and `month/day/year` strings from SPL
|
|
||||||
local function Split(s, delimiter)
|
|
||||||
local result = {}
|
|
||||||
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
|
|
||||||
table.insert(result, match)
|
|
||||||
end
|
|
||||||
return result
|
|
||||||
end
|
|
||||||
|
|
||||||
local split_airtime = Split(airtime, ":")
|
|
||||||
local airtime_hr, airtime_min, airtime_sec = split_airtime[1], split_airtime[2], split_airtime[3]
|
|
||||||
local split_airdate = Split(airdate, "/")
|
|
||||||
local airdate_month, airdate_day, airdate_year = split_airdate[1], split_airdate[2], split_airdate[3]
|
|
||||||
local airtime_stamp = os.time{year=airdate_year, month=airdate_month, day=airdate_day, hour=airtime_hr, min=airtime_min, sec=airtime_sec}
|
|
||||||
|
|
||||||
|
|
||||||
-- ------- --
|
|
||||||
-- queries --
|
|
||||||
-- ------- --
|
|
||||||
|
|
||||||
artist = models.Artists:find({ name=artist_name })
|
|
||||||
if not artist then
|
|
||||||
print("new artist: " .. artist_name)
|
|
||||||
artist = models.Artists:create({
|
|
||||||
name = artist_name
|
|
||||||
})
|
|
||||||
else
|
|
||||||
print("old artist.")
|
|
||||||
end
|
|
||||||
|
|
||||||
-- make a unique name/identifier for the track using a combination of artist_name and track_name
|
|
||||||
-- in lower-case and spaces removed
|
|
||||||
local unique_track_name = artist_name .. track_name -- prepend artist
|
|
||||||
unique_track_name = unique_track_name:lower() -- all to lowercase
|
|
||||||
unique_track_name = unique_track_name:gsub("%s+", "") -- remove spaces
|
|
||||||
|
|
||||||
track = models.Tracks:find({ unique_name=unique_track_name })
|
|
||||||
if not track then
|
|
||||||
print("new track: " .. track_name)
|
|
||||||
track = models.Tracks:create({
|
|
||||||
name = track_name,
|
|
||||||
unique_name = unique_track_name,
|
|
||||||
year = tonumber(release_year),
|
|
||||||
info_url = track_info_url,
|
|
||||||
img_url = track_img_url
|
|
||||||
})
|
|
||||||
else
|
|
||||||
print("old track.")
|
|
||||||
end
|
|
||||||
|
|
||||||
-- track_artist
|
|
||||||
local track_artist = models.Trackartists:find({ track=track["id"], artist=artist["id"] })
|
|
||||||
if not track_artist then
|
|
||||||
print("new track artist")
|
|
||||||
track_artist = models.Trackartists:create({
|
|
||||||
track=track["id"],
|
|
||||||
artist=artist["id"]
|
|
||||||
})
|
|
||||||
end
|
|
||||||
|
|
||||||
-- airtime
|
|
||||||
local airtime = models.Airtimes:find({
|
|
||||||
airtime = db.format_date(airtime_stamp),
|
|
||||||
tracks = track["id"],
|
|
||||||
stations = station["id"]
|
|
||||||
})
|
|
||||||
if not airtime then
|
|
||||||
airtime = models.Airtimes:create({
|
|
||||||
airtime = db.format_date(airtime_stamp),
|
|
||||||
tracks = track["id"],
|
|
||||||
stations = station["id"]
|
|
||||||
})
|
|
||||||
else
|
|
||||||
print("already have this airtime! wtf??")
|
|
||||||
end
|
|
||||||
|
|
||||||
-- release
|
|
||||||
if release_name:len() > 0 then
|
|
||||||
release = models.Releases:find({ name=release_name })
|
|
||||||
if not release then
|
|
||||||
print("new release: " .. release_name)
|
|
||||||
release = models.Releases:create({
|
|
||||||
name = release_name,
|
|
||||||
year = tonumber(release_year),
|
|
||||||
country = release_country,
|
|
||||||
label = release_label
|
|
||||||
})
|
|
||||||
|
|
||||||
else
|
|
||||||
print("old release.")
|
|
||||||
end
|
|
||||||
|
|
||||||
local track_release = models.Trackreleases:find({ track=track["id"], release=release["id"] })
|
|
||||||
if not track_release then
|
|
||||||
track_release = models.Trackreleases:create({ track=track["id"], release=release["id"] })
|
|
||||||
end
|
|
||||||
|
|
||||||
else
|
|
||||||
print("no release, skipping")
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
app:match("/station/:station", json_params(function(self)
|
||||||
|
return handlers.Stationhandler(self)
|
||||||
|
end))
|
||||||
|
|
||||||
|
app:match("/track/:track", json_params(function(self)
|
||||||
|
return handlers.Trackhandler(self)
|
||||||
|
end))
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|||||||
175
code/handlers/splhandler.lua
Normal file
175
code/handlers/splhandler.lua
Normal file
@ -0,0 +1,175 @@
|
|||||||
|
local db = require("lapis.db")
|
||||||
|
|
||||||
|
local autoload = require("lapis.util").autoload
|
||||||
|
local models = autoload("models")
|
||||||
|
|
||||||
|
function Splhandler(self)
|
||||||
|
-- print( to_json(self.params))
|
||||||
|
|
||||||
|
-- TODO: add auth-token
|
||||||
|
|
||||||
|
-- artist_name=%a&
|
||||||
|
-- track_name=%t&
|
||||||
|
-- release_name=%T
|
||||||
|
-- label=%L&
|
||||||
|
-- year=%Y&
|
||||||
|
-- country=%O&
|
||||||
|
-- info_url=%U1&
|
||||||
|
-- img_url=%U2&
|
||||||
|
-- time=%h&
|
||||||
|
-- date=%d&
|
||||||
|
|
||||||
|
-- artist_name=%a&track_name=%t&release_name=%Tlabel=%L&year=%Y&country=%O&info_url=%U1&img_url=%U2&time=%h&date=%d&
|
||||||
|
|
||||||
|
-- ----- --
|
||||||
|
-- setup --
|
||||||
|
-- ----- --
|
||||||
|
|
||||||
|
local station_id = self.params.station
|
||||||
|
|
||||||
|
local station = models.Stations:find({ station = station_id })
|
||||||
|
if not station then
|
||||||
|
print("unknown station: " .. station_id)
|
||||||
|
else
|
||||||
|
print("This is: " .. station["name"])
|
||||||
|
end
|
||||||
|
|
||||||
|
local artist = nil
|
||||||
|
local artist_name = self.params.artist_name -- mandatory
|
||||||
|
|
||||||
|
local track = nil
|
||||||
|
local track_name = self.params.track_name -- mandatory
|
||||||
|
local track_info_url = nil
|
||||||
|
if self.params.info_url then
|
||||||
|
track_info_url = self.params.info_url
|
||||||
|
end
|
||||||
|
local track_img_url = nil
|
||||||
|
if self.params.img_url then
|
||||||
|
track_img_url = self.params.img_url
|
||||||
|
end
|
||||||
|
|
||||||
|
local release = nil
|
||||||
|
local release_name = nil
|
||||||
|
if self.params.release_name then
|
||||||
|
release_name = self.params.release_name
|
||||||
|
end
|
||||||
|
local release_year = nil
|
||||||
|
if self.params.year then
|
||||||
|
release_year = tonumber(self.params.year)
|
||||||
|
end
|
||||||
|
local release_country = nil
|
||||||
|
if self.params.country then
|
||||||
|
release_country = self.params.country
|
||||||
|
end
|
||||||
|
local release_label = nil
|
||||||
|
if self.params.label then
|
||||||
|
release_label = self.params.label
|
||||||
|
end
|
||||||
|
|
||||||
|
local airtime = self.params.time -- mandatory
|
||||||
|
local airdate = self.params.date -- mandatory
|
||||||
|
|
||||||
|
-- we have to split the `hr:min:sec` and `month/day/year` strings from SPL
|
||||||
|
local function Split(s, delimiter)
|
||||||
|
local result = {}
|
||||||
|
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
|
||||||
|
table.insert(result, match)
|
||||||
|
end
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
|
local split_airtime = Split(airtime, ":")
|
||||||
|
local airtime_hr, airtime_min, airtime_sec = split_airtime[1], split_airtime[2], split_airtime[3]
|
||||||
|
local split_airdate = Split(airdate, "/")
|
||||||
|
local airdate_month, airdate_day, airdate_year = split_airdate[1], split_airdate[2], split_airdate[3]
|
||||||
|
local airtime_stamp = os.time{year=airdate_year, month=airdate_month, day=airdate_day, hour=airtime_hr, min=airtime_min, sec=airtime_sec}
|
||||||
|
|
||||||
|
|
||||||
|
-- ------- --
|
||||||
|
-- queries --
|
||||||
|
-- ------- --
|
||||||
|
|
||||||
|
artist = models.Artists:find({ name=artist_name })
|
||||||
|
if not artist then
|
||||||
|
print("new artist: " .. artist_name)
|
||||||
|
artist = models.Artists:create({
|
||||||
|
name = artist_name
|
||||||
|
})
|
||||||
|
else
|
||||||
|
print("old artist.")
|
||||||
|
end
|
||||||
|
|
||||||
|
-- make a unique name/identifier for the track using a combination of artist_name and track_name
|
||||||
|
-- in lower-case and spaces removed
|
||||||
|
local unique_track_name = artist_name .. track_name -- prepend artist
|
||||||
|
unique_track_name = unique_track_name:lower() -- all to lowercase
|
||||||
|
unique_track_name = unique_track_name:gsub("%s+", "") -- remove spaces
|
||||||
|
|
||||||
|
track = models.Tracks:find({ unique_name=unique_track_name })
|
||||||
|
if not track then
|
||||||
|
print("new track: " .. track_name)
|
||||||
|
track = models.Tracks:create({
|
||||||
|
name = track_name,
|
||||||
|
unique_name = unique_track_name,
|
||||||
|
year = release_year,
|
||||||
|
info_url = track_info_url,
|
||||||
|
img_url = track_img_url
|
||||||
|
})
|
||||||
|
else
|
||||||
|
print("old track.")
|
||||||
|
end
|
||||||
|
|
||||||
|
-- track_artist
|
||||||
|
local track_artist = models.Trackartists:find({ track=track["id"], artist=artist["id"] })
|
||||||
|
if not track_artist then
|
||||||
|
print("new track artist")
|
||||||
|
track_artist = models.Trackartists:create({
|
||||||
|
track=track["id"],
|
||||||
|
artist=artist["id"]
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
-- airtime
|
||||||
|
local airtime = models.Airtimes:find({
|
||||||
|
airtime = db.format_date(airtime_stamp),
|
||||||
|
track = track["id"],
|
||||||
|
station = station["id"]
|
||||||
|
})
|
||||||
|
if not airtime then
|
||||||
|
airtime = models.Airtimes:create({
|
||||||
|
airtime = db.format_date(airtime_stamp),
|
||||||
|
track = track["id"],
|
||||||
|
station = station["id"]
|
||||||
|
})
|
||||||
|
else
|
||||||
|
print("already have this airtime! wtf??")
|
||||||
|
end
|
||||||
|
|
||||||
|
-- release
|
||||||
|
if release_name:len() > 0 then
|
||||||
|
release = models.Releases:find({ name=release_name })
|
||||||
|
if not release then
|
||||||
|
print("new release: " .. release_name)
|
||||||
|
release = models.Releases:create({
|
||||||
|
name = release_name,
|
||||||
|
year = release_year,
|
||||||
|
country = release_country,
|
||||||
|
label = release_label
|
||||||
|
})
|
||||||
|
|
||||||
|
else
|
||||||
|
print("old release.")
|
||||||
|
end
|
||||||
|
|
||||||
|
local track_release = models.Trackreleases:find({ track=track["id"], release=release["id"] })
|
||||||
|
if not track_release then
|
||||||
|
track_release = models.Trackreleases:create({ track=track["id"], release=release["id"] })
|
||||||
|
end
|
||||||
|
|
||||||
|
else
|
||||||
|
print("no release, skipping")
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
return Splhandler
|
||||||
63
code/handlers/stationhandler.lua
Normal file
63
code/handlers/stationhandler.lua
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
local db = require("lapis.db")
|
||||||
|
local to_json = require("lapis.util").to_json
|
||||||
|
local autoload = require("lapis.util").autoload
|
||||||
|
local preload = require("lapis.db.model").preload
|
||||||
|
|
||||||
|
local models = autoload("models")
|
||||||
|
|
||||||
|
|
||||||
|
function Stationhandler(self)
|
||||||
|
local station_id = self.params.station
|
||||||
|
|
||||||
|
local station = models.Stations:find({ station = station_id })
|
||||||
|
if not station then
|
||||||
|
print("unknown station: " .. station_id)
|
||||||
|
else
|
||||||
|
print("This is: " .. station["name"])
|
||||||
|
end
|
||||||
|
|
||||||
|
local artist = nil
|
||||||
|
local release = nil
|
||||||
|
local return_dict = {}
|
||||||
|
local limit = 10
|
||||||
|
local offset = 0
|
||||||
|
|
||||||
|
-- local airtimes = models.Airtimes:paginated("where station = ? order by airtime desc", station["id"], {
|
||||||
|
-- per_page = 10,
|
||||||
|
-- fields = "track, station, airtime",
|
||||||
|
-- prepare_results = function(airtime)
|
||||||
|
-- models.Tracks:include_in(airtime, { id="track" })
|
||||||
|
-- models.Stations:include_in(airtime, { id="station" })
|
||||||
|
-- return airtime
|
||||||
|
-- end
|
||||||
|
-- })
|
||||||
|
|
||||||
|
-- airtimes = airtimes:get_page(1)
|
||||||
|
|
||||||
|
local airtimes = db.query([[
|
||||||
|
SELECT airtime, track.name AS track, track.year, track.info_url, track.img_url,
|
||||||
|
artist.name as artist,
|
||||||
|
release.name AS release, release.country, release.label AS label
|
||||||
|
FROM airtimes
|
||||||
|
INNER JOIN
|
||||||
|
tracks AS track ON (airtimes.track = track.id)
|
||||||
|
INNER JOIN
|
||||||
|
track_artists as track_artist ON (track_artist.track = track.id)
|
||||||
|
INNER JOIN
|
||||||
|
artists as artist ON (track_artist.artist = artist.id)
|
||||||
|
INNER JOIN
|
||||||
|
track_releases as track_release ON (track_release.track = track.id)
|
||||||
|
INNER JOIN
|
||||||
|
releases as release ON (track_release.release = release.id)
|
||||||
|
WHERE station = ?
|
||||||
|
ORDER BY airtime
|
||||||
|
DESC LIMIT ?
|
||||||
|
OFFSET ?
|
||||||
|
]], station["id"], limit, offset*limit
|
||||||
|
)
|
||||||
|
|
||||||
|
return { json = airtimes }
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
return Stationhandler
|
||||||
56
code/handlers/trackhandler.lua
Normal file
56
code/handlers/trackhandler.lua
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
local db = require("lapis.db")
|
||||||
|
local to_json = require("lapis.util").to_json
|
||||||
|
local autoload = require("lapis.util").autoload
|
||||||
|
local preload = require("lapis.db.model").preload
|
||||||
|
|
||||||
|
local models = autoload("models")
|
||||||
|
|
||||||
|
|
||||||
|
function Trackhandler(self)
|
||||||
|
local track_id = self.params.track
|
||||||
|
|
||||||
|
local track = models.Tracks:find({ id = track_id })
|
||||||
|
if not track then
|
||||||
|
self:write({"Not Found", status = 404})
|
||||||
|
end
|
||||||
|
|
||||||
|
local hidden_fields = {
|
||||||
|
"unique_name",
|
||||||
|
"created_at",
|
||||||
|
"updated_at"
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, field in ipairs(hidden_fields) do
|
||||||
|
track[field] = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local artists = {}
|
||||||
|
local airtimes = nil
|
||||||
|
|
||||||
|
local artists = db.query([[
|
||||||
|
SELECT artist.name AS artist_name
|
||||||
|
FROM track_artists
|
||||||
|
INNER JOIN
|
||||||
|
artists as artist ON (track_artists.artist = artist.id)
|
||||||
|
WHERE track_artists.track = ?
|
||||||
|
]], track["id"])
|
||||||
|
|
||||||
|
|
||||||
|
track["artist"] = artists[1]["artist_name"]
|
||||||
|
|
||||||
|
local airtimes = db.query([[
|
||||||
|
SELECT airtime
|
||||||
|
FROM airtimes
|
||||||
|
WHERE track = ?
|
||||||
|
]], track["id"])
|
||||||
|
|
||||||
|
track["airtimes"] = {}
|
||||||
|
for i, airtime in ipairs(airtimes) do
|
||||||
|
track["airtimes"][i] = airtime.airtime
|
||||||
|
end
|
||||||
|
|
||||||
|
return { json = track }
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
return Trackhandler
|
||||||
@ -7,7 +7,7 @@ local Airtimes = Model:extend("airtimes", {
|
|||||||
timestamp = true,
|
timestamp = true,
|
||||||
relations = {
|
relations = {
|
||||||
{"track", belongs_to = "Tracks"},
|
{"track", belongs_to = "Tracks"},
|
||||||
{"stations", has_one = "Stations"}
|
{"station", has_one = "Stations"}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user