summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@helsinki.at>2011-03-16 01:47:03 (GMT)
committerChristian Pointner <equinox@helsinki.at>2011-03-16 01:47:03 (GMT)
commitd3960b443bd20216d096815db6e10e2b7f6c53b9 (patch)
tree67322d836cdf6d1e771a2f9861cb6ef2c5308e42
parent74cce8f8317cacc9f07404236710e68f279b3773 (diff)
insert music into tempstorage
-rwxr-xr-xnopsyncd/qlistener.lua21
-rwxr-xr-xnopsyncd/tcpserver.lua10
-rw-r--r--nopsyncd/tempstorage.lua27
3 files changed, 47 insertions, 11 deletions
diff --git a/nopsyncd/qlistener.lua b/nopsyncd/qlistener.lua
index e5cd4ab..1e02df2 100755
--- a/nopsyncd/qlistener.lua
+++ b/nopsyncd/qlistener.lua
@@ -20,6 +20,7 @@
--
local queue_name = "/rhnop"
+local last_cart = nil
mq = require "luamq"
tempstorage = require "tempstorage"
@@ -30,9 +31,13 @@ function handle_now(timestamp, nowcart, nowlen)
if results == nil then
io.stderr:write("can't fetch cart info: " .. err .. "\n")
else
- -- TODO: insert into tempstorage
- print(timestamp .. " Info: '" .. results.TITLE .. "' von '" .. results.ARTIST .. "' aus '" .. results.ALBUM .. "'")
- pipe.signal()
+-- print(timestamp .. " Info: '" .. results.TITLE .. "' von '" .. results.ARTIST .. "' aus '" .. results.ALBUM .. "'")
+ local ret, err = tempstorage:insertMusic(timestamp, nowcart, nowlen, results.TITLE, results.ARTIST, results.ALBUM)
+ if ret == nil then
+ io.stderr:write("can't insert music info: " .. err .. "\n")
+ else
+ pipe.signal()
+ end
end
end
@@ -41,8 +46,11 @@ function handle_message(msg)
if not timestamp or not nowcart or not nowlen or not nextcart or not nextlen then
io.stderr:write("ignoring malformed message\n")
else
- -- TODO sanity checks: is now info new or just next?
- handle_now(timestamp, nowcart, nowlen)
+ -- TODO better sanity checks: is now info new or just next?
+ if last_cart ~= nowcart then
+ last_cart = nowcart
+ handle_now(timestamp, nowcart, nowlen)
+ end
-- TODO handle next info
end
end
@@ -59,7 +67,8 @@ function main_loop()
io.stderr:write("creation of tempstorage failed: " .. err .. "\n")
os.exit(1)
end
-
+ last_cart = assert(tempstorage:getLastCart())
+
local ret, err = rddb:init()
if ret == nil then
io.stderr:write("opening rivendell db failed: " .. err .. "\n")
diff --git a/nopsyncd/tcpserver.lua b/nopsyncd/tcpserver.lua
index 3f7c70a..d298eed 100755
--- a/nopsyncd/tcpserver.lua
+++ b/nopsyncd/tcpserver.lua
@@ -86,11 +86,11 @@ end
function main_loop()
local pipefd = pipe.getreadfd()
- local ret, err = tempstorage:init()
- if ret == nil then
- io.stderr:write("creation of tempstorage failed: " .. err .. "\n")
- os.exit(1)
- end
+ -- local ret, err = tempstorage:init()
+ -- if ret == nil then
+ -- io.stderr:write("creation of tempstorage failed: " .. err .. "\n")
+ -- os.exit(1)
+ -- end
server = init_server()
diff --git a/nopsyncd/tempstorage.lua b/nopsyncd/tempstorage.lua
index f034647..37f3422 100644
--- a/nopsyncd/tempstorage.lua
+++ b/nopsyncd/tempstorage.lua
@@ -49,6 +49,33 @@ function tempstorage:init()
return true
end
+function tempstorage:getLastCart()
+ local cur, err = self.con:execute("SELECT cart from (SELECT cart,timestamp,MAX(timestamp) AS tsmax FROM now) where timestamp = tsmax")
+ if cur == nil then
+ return nil, err
+ end
+
+ local cart = cur:fetch()
+ if cart == nil then cart = 0 end
+ return cart
+end
+
+function tempstorage:insertMusic(timestamp, cart, len, title, artist, album)
+ cart = tonumber(cart)
+ if cart < 400000 or cart > 450000 then
+ poolnum = 0
+ else
+ poolnum = math.floor(cart/1000) - 399
+ end
+ -- TODO escape "' in strings"
+ local cur, err = self.con:execute("INSERT into now VALUES(" .. timestamp .. ", " .. cart .. ", " .. len .. ", 'Musikpool " .. poolnum .. "', '" .. title .. "', '" .. artist .."', '" .. album .. "', 1)")
+ if cur == nil then
+ return nil, err
+ end
+
+ return true
+end
+
function tempstorage:close()
if self.con then
self.con:close()