[go: up one dir, main page]

File: lastfmradio

package info (click to toggle)
zomg 0.3.10-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 384 kB
  • ctags: 44
  • sloc: sh: 1,195; ansic: 192; xml: 100; makefile: 89
file content (282 lines) | stat: -rw-r--r-- 9,075 bytes parent folder | download
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# lastfmradio: ZOMG last.fm radio functions
#   Copyright (C) 2005, 2006, 2007, 2008  Clint Adams

# 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 3 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

lastfm_get_np() {
  local lastfm_session="$1" debug="$2"
  local -a np
  local station artist album track trackduration trackprogress

  np=(
  ${(f)"$(wget -q --output-document=- http://${baseurl}${basepath}/np.php\?session=${lastfm_session}\&debug=0)"}
  )

  if [[ $debug -eq 1 ]]; then
    print ">NP<"
    print "$np"
    print ">NP<"
  fi

  station=${${(M)np:#station=*}#station=}
  artist=${${(M)np:#artist=*}#artist=}
  album=${${(M)np:#album=*}#album=}
  track=${${(M)np:#track=*}#track=}
  trackduration=${${(M)np:#trackduration=*}#trackduration=}
  trackprogress=${${(M)np:#trackprogress=*}#trackprogress=}

  (( timeleft = trackduration - trackprogress ))

  print ""
  print "Station: ${station}"
  print "Artist: ${artist}"
  print "Album: ${album}"
  print "Track: ${track}"
  print "Length: ${trackduration} seconds"
  print ""

  if (( timeleft == 0 )); then (( timeleft = 10 )); fi
}

lastfm_quit() {
  if [[ -z "$BUFFER" ]]; then
    BUFFER="q"
    zle .accept-line
  fi
}

lastfm_tune() {
  local lastfm_session="$1" station="$2"
  local tunestatus

  print -n "Tuning to $station... "

  tunestatus=$(wget -q --output-document=- "http://${baseurl}${basepath}/adjust.php?session=${lastfm_session}&url=${station}&debug=0")

  print "$tunestatus"
}

lastfm_radio_getplaylist() {
  local session="$1"
  local i

  wget -q -O ~/.zomg/radio_playlist.xspf "http://ws.audioscrobbler.com/radio/xspf.php?sk=${session}&discovery=0&desktop=1.3.0.58" || return 99
  zomghelper-xspf ~/.zomg/radio_playlist.xspf > ~/.zomg/radio_playlist || return 98

  unset zomg_xspf_location zomg_xspf_title zomg_xspf_id zomg_xspf_album
  unset zomg_xspf_creator zomg_xspf_duration zomg_xspf_image

  for i in ${(f)"$(<~/.zomg/radio_playlist)"}
  do
    case "$i" in
      ((#b)([^=]##)\[([0-9]##)\]=(*))
      eval zomg_xspf_$match[1]\[$match[2]\]="${(q)match[3]}"
      ;;
      ((#b)([^=]##)=(*))
      eval zomg_xspf_$match[1]="${(q)match[2]}"
      ;;
      (*)
      ;;
    esac
  done

  if [[ -z $zomg_xspf_location ]]; then
    print Empty playlist, quitting.
    return 99
  fi

  print Parsed radio playlist $zomg_xspf_playlist_title by $zomg_xspf_playlist_creator.
  for i in {1..${#zomg_xspf_location}}
  do
    print $i. $zomg_xspf_creator[$i] / $zomg_xspf_title[$i]
  done
}

lastfm_playfive() {
  local lastfm_session="$1"

  lastfm_radio_getplaylist "$lastfm_session" || exit 99

  for currenttrack in {1..${#zomg_xspf_location}}
  do

  audioscrobbler_construct_np_query "$LASTFM_USER" "$sessionid" \
   "$zomg_xspf_creator[$currenttrack]" "$zomg_xspf_title[$currenttrack]" \
   "$zomg_xspf_album[$currenttrack]" "" \
   ${${zomg_xspf_duration[$currenttrack]}/000(#e)/} "$EPOCHSECONDS" && \
    audioscrobbler_np_submit ${(j::)reply} "$LASTFM_USER"

  print "Now playing $zomg_xspf_creator[$currenttrack] / $zomg_xspf_title[$currenttrack] / $zomg_xspf_album[$currenttrack]"

  starttime=$EPOCHSECONDS
#  "$mp3streamer[@]" $zomg_xspf_location[$currenttrack] >/dev/null 2>&1 &
  "$mp3streamer[@]" $zomg_xspf_location[$currenttrack]
  endtime=$EPOCHSECONDS

  skip=0
      (( endtime - starttime <= (${${zomg_xspf_duration[$currenttrack]}/000(#e)/} / 2.0 ) )) &&
      (( endtime - starttime < 240 )) && skip=1

      (( ${${zomg_xspf_duration[$currenttrack]}/000(#e)/} < 30 )) && skip=1

      if [[ $skip -eq 1 ]];
      then
	print "SKIPPING"
	skip=0
      else
  audioscrobbler_constructquery "$sessionid" \
   "$zomg_xspf_creator[$currenttrack]" "$zomg_xspf_title[$currenttrack]" \
   "$zomg_xspf_album[$currenttrack]" "" \
   "${${zomg_xspf_duration[$currenttrack]}/000(#e)/}" "$starttime" \
   L"$zomg_xspf_lastfm_trackauth[$currenttrack]" &&
	audioscrobbler_submit ${(j::)reply} "$LASTFM_USER" || { submit=0; audioscrobbler_cache "$reply[2]" }
      fi

      done

}

lastfm_radio() {
  local station="$1"
  local passwordmd5=${${="$(print -n $LASTFM_PASSWORD | md5sum)"}[1]}
  local -a handshake
  local lastfm_session streamurl radiocmd streamstate
  local baseurl basepath

  handshake=( ${(f)"$(wget -q --output-document=- http://ws.audioscrobbler.com/radio/handshake.php\?version=1.3.0.58\&platform=linux\&username=${LASTFM_USER}\&passwordmd5=${passwordmd5}\&language=en)"} )

  lastfm_session=${${(M)handshake:#session=*}#session=}
  streamurl=${${(M)handshake:#stream_url=*}#stream_url=}
  baseurl=${${${(M)handshake:#base_url=*}#base_url=}:-ws.audioscrobbler.com}
  basepath=${${${(M)handshake:#base_path=*}#base_path=}:-/radio}

  if [[ -n "$lastfm_session" && "$lastfm_session" != FAILED ]]; then
    print "Connecting to Last.fm session ${lastfm_session}"
    print "at $streamurl"
  else
    print "Handshake failed"
    exit 47

  fi

  if [[ -n "$station" && "$station" == lastfm://* ]]; then

    lastfm_tune $lastfm_session $station

  fi

  unset HISTFILE
  HISTSIZE=512
  SAVEHIST=512
  setopt histignorealldups
  history -ap ~/.zomg/lastfm_history

  if [[ -x =mpg123 ]]; then
    mp3streamer=(mpg123 -b 2048 -q)
  else
    mp3streamer=(mplayer -quiet -noconsolecontrols -cache 2048 -cache-min 1)
  fi

  lastfm_playfive "$lastfm_session"
  print "Either tune to a new station or type 'p' to play a few more songs."

  zle -N lastfm_quit
  bindkey '^D' lastfm_quit
  autoload -U compinit
  compinit
  bindkey '^I' _history-complete-older

  while true
  do
    [[ $#jobstates -eq 1 ]] && streamstate="" || streamstate="[broken] "

    radiocmd=""
    vared -hp "${streamstate}last.fm $LASTFM_USER $timeleft> " radiocmd
    print -s $radiocmd

    case "$radiocmd" in
      (h)
      print "h: help"
#      print "n: show what's playing now"
      print "q: quit"
#      print "s: skip"
      print "p: play another five songs from the same station"
      print "t lastfm://<station> : tune to another radio station"
      print "tu <lastfm user> : shortcut to tune to lastfm://user/<user>/personal"
      print "tl <lastfm user> : shortcut to tune to lastfm://user/<user>/loved"
      print "tn <lastfm user> : shortcut to tune to lastfm://user/<user>/neighbours"
#      print "love: mark this track as loved"
#      print "ban: ban this track"
#      print "discovery <on/off>: turn discovery mode on or off"
#      print "rtp: turn scrobbling on"
#      print "nortp: turn scrobbling off"
      ;;
#      (n)
#      lastfm_get_np "${lastfm_session}" 0
#      ;;
#      (ndebug)
#      lastfm_get_np "${lastfm_session}" 1
#      ;;
      (q|exit)
      kill %2
      exit 0
      ;;
#      (s)
#      print "Skipping... " $(wget -q --output-document=- "http://${baseurl}${basepath}/control.php?session=${lastfm_session}&command=skip&debug=0")
#      lastfm_get_np "${lastfm_session}" 0
#      ;;
      (p)
      lastfm_playfive "${lastfm_session}"
      ;;
      (t (#b)(lastfm://[^ ]##))
      lastfm_tune ${lastfm_session} $match[1]
      lastfm_playfive "${lastfm_session}"
#      lastfm_get_np "${lastfm_session}" 0
      ;;
      (tu (#b)([^ ]##))
      lastfm_tune ${lastfm_session} lastfm://user/$match[1]/personal
      lastfm_playfive "${lastfm_session}"
#      lastfm_get_np "${lastfm_session}" 0
      ;;
      (tl (#b)([^ ]##))
      lastfm_tune ${lastfm_session} lastfm://user/$match[1]/loved
      lastfm_playfive "${lastfm_session}"
#      lastfm_get_np "${lastfm_session}" 0
      ;;
      (tn (#b)([^ ]##))
      lastfm_tune ${lastfm_session} lastfm://user/$match[1]/neighbours
      lastfm_playfive "${lastfm_session}"
#      lastfm_get_np "${lastfm_session}" 0
      ;;
#      (love)
#      print "Loving... " $(wget -q --output-document=- "http://${baseurl}${basepath}/control.php?session=${lastfm_session}&command=love&debug=0")
#      ;;
#      (ban)
#      print "Banning... " $(wget -q --output-document=- "http://${baseurl}${basepath}/control.php?session=${lastfm_session}&command=ban&debug=0")
#      ;;
#      (discovery (#b)(on|off))
#      print "Turning discovery ${match[1]}..." $(wget -q --output-document=- "http://${baseurl}${basepath}/adjust.php?session=${lastfm_session}&url=lastfm://settings/discovery/${match[1]}&debug=0")
#      ;;
#      ((#b)(rtp|nortp))
#      print "Setting ${match[1]}..." $(wget -q --output-document=- "http://${baseurl}${basepath}/control.php?session=${lastfm_session}&command=${match[1]}")
#      ;;
      (*)
      print "WTF?! Try 'h' for help"
      ;;

    esac
  done

}