157 lines
4.1 KiB
Lua
157 lines
4.1 KiB
Lua
local lapis = require("lapis")
|
|
local config = require("lapis.config").get()
|
|
local to_json = require("lapis.util").to_json
|
|
local db = require("lapis.db")
|
|
|
|
local app = lapis.Application()
|
|
|
|
local autoload = require("lapis.util").autoload
|
|
local models = autoload("models")
|
|
|
|
|
|
app:get("/", function(self)
|
|
-- return "Welcome to Lapis " .. require("lapis.version")
|
|
return config.greeting .. " from port " .. config.postgres.password
|
|
|
|
end)
|
|
|
|
app:match("/spl/:station", function(self)
|
|
-- print( to_json(self.params))
|
|
|
|
-- artist_name=%a&
|
|
-- track_name=%t&
|
|
-- release_name=%T
|
|
-- label=%L&
|
|
-- year=%Y&
|
|
-- country=%O&
|
|
-- info_url=%U1&
|
|
-- img_url=%U2&
|
|
-- time=%h&
|
|
|
|
-- artist_name=%a&track_name=%t&release_name=%Tlabel=%L&year=%Y&country=%O&info_url=%U1&img_url=%U2&time=%h&
|
|
|
|
-- ----- --
|
|
-- 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_spl = self.params.time
|
|
|
|
-- we have to split the `hr:min:sec` string 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_spl, ":")
|
|
local airtime_hr, airtime_min, airtime_sec = split_airtime[1], split_airtime[2], split_airtime[3]
|
|
local airtime_stamp = os.time{year=os.date("%Y"), month=os.date("%m"), day=os.date("%d"), 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:create({
|
|
airtime = db.format_date(airtime_stamp),
|
|
tracks = track["id"],
|
|
stations = station["id"]
|
|
})
|
|
|
|
-- 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)
|
|
|
|
return app
|