2ck

weblist.rb

2ck
Aug 4th, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.20 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require 'xmmsclient'
  4. require 'jcode'
  5. #require 'unicode'
  6. require "event-loop"
  7. module Xmms
  8.     class Client
  9.         def add_to_event_loop
  10.             @io = IO.for_fd(io_fd)
  11.  
  12.             @io.on_readable { io_in_handle }
  13.             @io.on_writable { io_out_handle }
  14.  
  15.             EventLoop.on_before_sleep do
  16.                 if io_want_out
  17.                     @io.monitor_event(:writable)
  18.                 else
  19.                     @io.ignore_event(:writable)
  20.                 end
  21.             end
  22.         end
  23.     end
  24. end
  25.  
  26. $playlist_name = "Radio"
  27. $stderr = File.open("list.log", "w")
  28. $KCODE = 'UTF-8'
  29. $xc = Xmms::Client.new("weblist")
  30. $xc.connect(ENV["XMMS_PATH"])
  31. $xc.add_to_event_loop
  32. $xc.on_disconnect { EventLoop.quit }
  33. $pl = Xmms::Playlist.new($xc, $playlist_name)
  34. $updating = false
  35. if not $xc
  36.     exit "Could not get connection to XMMS2 daemon"
  37. end
  38. if not $pl
  39.     exit "Could not get playlist #{$playlist_name}"
  40. end
  41. def update
  42.     while $updating
  43.     end
  44.     do_update
  45. end
  46.  
  47. def do_update
  48.     $updating = true
  49.     $stdout = File.open("/var/www/tracklist.html", "w:UTF-8")
  50.     $stderr.puts "In update()"
  51.     puts "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">"
  52.     puts "<HTML>"
  53.     puts "<HEAD>"
  54.     puts "<meta http-equiv=\"Content-Type\"  content=\"text/html; charset=utf-8\" />"
  55.     puts "<TITLE>Track Listing</TITLE>"
  56.     puts "<link rel=\"icon\" type=\"image/png\" href=\"/icon.png\" />"
  57.     puts "<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\"radio.css\" />"
  58.     puts "</HEAD>"
  59.     puts "<body>"
  60.     puts "<div id=\"chname\"><table width=\"700\" border=\"0\"><tr><td align=\"center\"><h1 class=\"blue\">YOUR HEADER HERE</h1></td></tr></table></div>"
  61.     if $radio_inactive == true
  62.         puts "<em style=\"font-size:30px\">Sorry but the radio is currently inactive. Please try again some other time.</em></body></html>"
  63.         $stdout.close
  64.         return
  65.     end
  66.     puts "<div id=\"playinc\">"
  67.     puts "<table width=\"700\" border=\"0\">"
  68.  
  69.     current_time = Time.new
  70.     track_time = current_time
  71.     entries = $pl.entries.wait.value
  72.  
  73.     playing_entry = 0
  74.     if (val = $pl.current_pos.wait.value)
  75.         playing_entry = val[:position]
  76.     end
  77.  
  78.     current_entry = playing_entry - 5
  79.  
  80.     list_header_fmt = "<tr><td width=\"15%%\" class=\"boldblue\">%s</td><td width=\"25%%\" class=\"boldblue\">Artist</td><td width=\"25%%\" class=\"boldblue\">Title</td><td width=\"5%%\" class=\"boldblue\"></td></tr><tr><td colspan=\"5\"><img src=\"/img3/red.gif\" height=\"1\" width=\"100%%\" alt=\"\" /></td></tr>"
  81.     puts list_header_fmt % "Played At"
  82.     #PRINT LOOP
  83.     entries[current_entry,10].each do |id|
  84.         if not playing_entry
  85.             puts "</table></div></body></html>"
  86.             $stdout.close
  87.             return
  88.         end
  89.         infos = $xc.medialib_get_info(id).wait.value
  90.         artist, title, url =
  91.             [:artist, :title, :url].map do |field|
  92.             value = infos[field].to_a
  93.             if not value.nil?
  94.                 top_source = value[0]
  95.                 top_source[1].to_s.
  96.                     gsub(%r{&},'&amp;').
  97.                     gsub(%r{<},'&lt;').
  98.                     gsub(%r{>},'&gt;')
  99.             end
  100.             end
  101.         duration, times_played, laststarted =
  102.             [:duration, :timesplayed, :laststarted].map do |field|
  103.             value = infos[field].to_a
  104.             if not value.nil?
  105.                 value.flatten[1]
  106.             end
  107.             end
  108.         if current_entry <= playing_entry
  109.             if times_played > 0
  110.                 track_time = Time.at(laststarted);
  111.             end
  112.         end
  113.  
  114.         if current_entry == playing_entry
  115.             puts "<meta http-equiv=\"refresh\" content=\"#{duration/1000}\"/>"
  116.             print "<tr style=color:red>"
  117.         else
  118.             print "<tr>"
  119.         end
  120.         print "<td>#{track_time.strftime("%T")}</td>"
  121.         print "<td>#{artist}</td><td>#{title}</td>"
  122.         print "</tr>\n"
  123.  
  124.         if playing_entry == current_entry
  125.             puts list_header_fmt % "Playing At"
  126.         end
  127.         current_entry += 1
  128.         track_time += duration/1000
  129.     end
  130.     puts "</table>"
  131.     puts "</div>"
  132.     puts "<em>Current time is: #{current_time}</em><br /><br />"
  133.     puts "</body></HTML>"
  134.     $stdout.close
  135. ensure
  136.     $stderr.puts "Leaving update()"
  137.     $updating = false
  138. end
  139.  
  140. $radio_inactive = false
  141. update
  142. $xc.broadcast_playlist_changed.notifier do |res|
  143.     $stderr.puts "Playlist changed"
  144.     if res[:name] == $playlist_name
  145. #        if res[:type] == Xmms::Playlist::CLEAR
  146. #            $radio_inactive = true
  147. #        end
  148.         update
  149.     end
  150.     true
  151. end
  152. $xc.broadcast_playback_status.notifier do |res|
  153.     $stderr.puts "Playlist playback status changed"
  154.     if (res == Xmms::Client::STOP) or (res == Xmms::Client::PAUSE)
  155.         $radio_inactive = true
  156.     else
  157.         $radio_inactive = false
  158.     end
  159.     update
  160.     true
  161. end
  162. $xc.broadcast_playlist_current_pos.notifier do |res|
  163.     $stderr.puts "Playlist position changed"
  164.     update
  165.     true
  166. end
  167. trap("SIGINT") { EventLoop.quit }
  168. pid = fork { EventLoop.run }
  169. Process.detach(pid)
Advertisement
Add Comment
Please, Sign In to add comment