
-- wmp.lua haxed by mids from pu's vlc.lua
-- wmp.lua spams WMP now playing info to BCDC++

-- -- -- -- INSTRUCTIONS -- -- -- --
--
-- Install the windows media player output plugin included in the rar.
-- In the configuration menu for the plugin, if you choose a different output directory for the xml file, 
-- modify the spamfile variable below (remember to escape the backslashes in the file path).
--
-- Copy this wmp.lua file into the /scripts directory of your install of BCDC++ and add the line 
--                  "dofile( DC():GetAppPath() .. "scripts\\wmp.lua" )"
-- (without the quotes) to the bottom of your startup.lua file with all the other user-specific scripts.
--
-- Type "/wmp" in your chat box to spam your now playing into BCDC++.
--
--

-- -- -- -- ISSUES -- -- -- --
-- 
-- The plugin supplied leaves the entry in the now_playing.xml file even after WMP has been shut down
-- 
-- If anyone can find a plugin which doesn't do this give me a shout at mids@twofo.co.uk and I'll implement it 
-- if I can be arsed.
--
-- Or just don't spam when WMP isn't running.
--
--


-- -- -- -- CODE -- -- -- --

--local xmllocation =  "C:\\Program Files (x86)\\Windows Media Player\\Plugins\\Now Playing\\now_playing.xml"
--local spamfile = DC():GetAppPath() .. "\\scripts\\now_playing.xml"

local spamfile = "C:\\Program Files (x86)\\Windows Media Player\\Plugins\\Now Playing\\now_playing.xml"

dcpp:setListener("ownChatOut","wmplayer",function(hub,message,ret)
   if string.find(message,"^/wmp%s*") then
        --os.execute("copy \""..xmllocation.."\" \""..DC():GetAppPath().."\\scripts\"")
       
		wmp(hub)
        
		--os.remove ( DC():GetAppPath() .. "\\scripts\\now_playing.xml")
      return 1
   end
end)



function wmp(hub)
    
    local fileContents = {}
	local filelength = 0
	local bitrate = 0
    file = io.open(spamfile,"r")

   if file then
      for line in file:lines() do table.insert(fileContents,line) end
      file:close()

		local started = string.find(fileContents[4], "<title>")
		local ended = string.find(fileContents[4], "<", started + 7)
		local filename = string.sub(fileContents[4], (started + 7), (ended-1))

		local filetypestart = string.find(fileContents[8], "<kind>")
		local filetypeend = string.find(fileContents[8], "</kind")
		local filetype = string.sub(fileContents[8], (filetypestart+6), (filetypeend-1))

		if (string.find(fileContents[12], "<time>")) then
			local filelengthstart = string.find(fileContents[12], "<time>")
			local filelengthend = string.find(fileContents[12], "</time")
			filelength = string.sub(fileContents[12], (filelengthstart+6), (filelengthend-1))
		else
			filelength = 0
		end

		if (string.find(fileContents[13], "<bitrate>")) then
			local bitratestart = string.find(fileContents[13], "<bitrate>")
			local bitrateend = string.find(fileContents[13], "</bitrate")
			bitrate = string.sub(fileContents[13], (bitratestart+9), (bitrateend-1))
		else
			bitrate = 0
		end

		filelengthhours = math.floor(filelength/3600)
		filelengthmins = math.floor((filelength/60)-(filelengthhours*60))
		filelengthsecs = math.floor(filelength-(filelengthhours*3600)-(filelengthmins*60) )		
		
		if(filelengthmins<10) then
			filelengthmins	= "0"..filelengthmins	
		end    
		
		if(filelengthsecs<10) then
			filelengthsecs	= "0"..filelengthsecs	
		end    
								
		hub:sendChat("WMP 12 playing \""..filename.."." ..filetype.."\" ("..filelengthhours..":"..filelengthmins..":"..filelengthsecs..")".." at "..bitrate.."kbps")

   else
      hub:addLine("now_playing.xml not present.")
   end  --if found file
end  --end function


DC():PrintDebug("  ** Loaded wmp.lua **")
