File: music.rb

package info (click to toggle)
gnoemoe 2.2.0%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,968 kB
  • ctags: 2,990
  • sloc: ansic: 23,956; sh: 8,882; ruby: 300; makefile: 174; xml: 86
file content (171 lines) | stat: -rw-r--r-- 4,985 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
=begin  
  music.rb
    Copyright (c) 2004 by Jesse van den Kieboom <jesse@icecrew.nl>
    Copyright (c) 2004/07/29 by Simon Gijsen <simon@gijsen.org>
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
	
	Defines:
	  rb/rhythmbox - show which song rb is currenly playing, control rb
	  xmms/beep - show which song xmms is playing, control xmms (thanks to Simon Gijsen)
=end

def register_functions
  $scripts.register("rhythmbox", "use: /rhythmbox <prefix>|next|prev|play|pause\nsends the song Rhythmbox is currently playing to the world prefixed by <prefix> or performs the action specified if pause, play, next or prev is specified")
  $scripts.register("rb", "alias for rhythmbox", "rhythmbox")
  $scripts.register("xmms", "use: /xmms <prefix>|prev|play|pause|stop|next\nsends the song XMMS is currently playing to the world prefixed by <prefix> or performs the action specified if prev, play, pause, stop or next is specified")
  $scripts.register("beep", "alias for xmms", "xmms")
end

def getOgginfo(filename)
  if (!File.exists?("/usr/bin/ogginfo"))
    return nil
  end
  
  info = `/usr/bin/ogginfo "#{filename}" 2> /dev/null`
  artist = nil
  title = nil
  
  if (!info.empty?)
    info.split("\n").each {|line|
      line = line.strip
      if (line[/^artist=/i])
        artist = line[7..-1]
      elsif (line[/^title=/i])
        title = line[6..-1]
      end
    }
    
    return "#{artist} - #{title}"
  else
    return nil
  end
end

def getID3info(filename)
  if (!File.exists?("/usr/bin/id3info"))
    nil
  end

  info = `/usr/bin/id3info "#{filename}" 2> /dev/null`.split("\n")
  artist = nil
  title = nil
  
  if (info.length == 1 && info[0].empty? == 0)
    return nil
  end
  
  info.each {|line|
    line = line.strip
    if (line.index(/TPE[1|2]/) != nil)
      artist = line[line.index(":") + 1..-1].strip
    elsif (line.index("TIT2") != nil)
      title = line[line.index(":") + 1..-1].strip
    end
  }
  
  if (title && artist)
    return "#{artist} - #{title}"
  else
    return nil
  end
end

def getMP3info(filename)
  if ((info = getID3info(filename)))
    return info
  end
  
  if (!File.exists?("/usr/bin/mp3info"))
    return nil
  end
  
  info = `/usr/bin/mp3info -p "%a - %t" "#{filename}" 2> /dev/null`
  
  if (!info.empty?)  
    return info
  else
    return nil
  end
end

def rhythmbox(argstr)
  isopen = !(`/bin/ps -ae | /bin/grep rhythmbox`.empty?)

  if (!isopen)
    $world.writeln("Script error: rhythmbox is not running!")
    return
  end
  
  if (argstr == "next")
    `/usr/bin/rhythmbox --next`
  elsif (argstr == "prev")
    `/usr/bin/rhythmbox --previous`
  elsif (argstr == "play" || argstr == "pause")
    `/usr/bin/rhythmbox --play-pause`
  else      
    path = `/usr/bin/rhythmbox --print-playing-path 2> /dev/null`
    info = nil
  
    if (!path.empty?)
      filename = path.split("\n")[0].gsub(/%20/, " ")[7..-1]
      
      if (filename[-3..-1] == "mp3" && (info = getMP3info(filename)))
      elsif (filename[-3..-1] == "ogg" && (info = getOgginfo(filename)))
      else
        info = `/usr/bin/rhythmbox --print-playing 2> /dev/null`
      
        if (!info.empty?)
          info = info.split("\n")[0]
        else
          info = nil
        end 
      end
    
      if (info)
        $world.sendln(argstr + " :luistert op dit moment naar [ #{info} ]")
      else
        $world.writeln("Script error: cannot retrieve rhythmbox file play")
      end  
    
    else
      $world.writeln("Script error: can not execute /usr/bin/rhythmbox!")
    end
  end
end

def xmms(argstr)
  case argstr
    when "prev", "play", "pause", "stop", "next"
      io = IO.popen("/usr/bin/xmms-shell -e " + argstr + " 2>&1")
      line = io.gets
      io.close
    else
      line = `/usr/bin/xmms-shell -e current-track 2>&1`
  end

  if (line[0..18] === "XMMS is not running")
    $world.writeln("Script error: " + line[0..-2])
  elsif(line[0..3] === "sh: ")
    $world.writeln("Script error: " + line[12..-2])
  elsif(line[0..12] === "Current song:")
    index = line.index('.') + 2
    $world.sendln(argstr + " emote luistert op dit moment naar [ " + line[index..-2] + " ]")
  else
    line2 = `/usr/bin/xmms-shell -e current-track 2>&1`
    index = line2.index('.') + 2
    $world.writeln(line[0..-2] + " [ " + line2[index..-2] + " ]")
  end
end