[go: up one dir, main page]

File: audioscrobbler

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 (302 lines) | stat: -rw-r--r-- 7,684 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# audioscrobbler: ZOMG audioscrobbler functions
#   Copyright (C) 2005, 2006, 2007  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

audioscrobbler_handshake() {
  local username="$1"
  local password="$2"
  local -a http_headers
  local -a response
  local xferencoding tstamp authtoken

  tcp_open post.audioscrobbler.com 80 scrobbler || return 2
  tstamp=$(export TZ=UTC; print ${EPOCHSECONDS})
  local pwsum="$(print -n "${password}" | md5sum | awk '{print $1}')${tstamp}"
  authtoken="$(print -n "${pwsum}" | md5sum | awk '{print $1}')"
  tcp_send -s scrobbler -- "GET /?hs=true&p=1.2&c=zom&v=0.1&u=${username}&t=${tstamp}&a=${authtoken} HTTP/1.1"

  tcp_send -s scrobbler -- "Host: post.audioscrobbler.com"
  tcp_send -s scrobbler -- "Accept-Charset: utf-8"
  tcp_send -s scrobbler -- ""

  sleep 2
  tcp_read -d -s scrobbler
  tcp_close -s scrobbler

  if [[ $tcp_lines[1] != "HTTP/1."[01]" 200 OK
" ]];
  then
    return 1
  fi

  http_headers=(${tcp_lines[1,(r)
]})
  xferencoding="${${http_headers[(r)Transfer-Encoding:*]}#Transfer-Encoding: }"
  case "$xferencoding" in
    (chunked*)
    startofbody=${tcp_lines[(i)
]}
    (( startofbody++ ))
    response=(${tcp_lines[startofbody,(r)0
]})
    ;;
    (*)
    response=(${tcp_lines[(r)
,-1]})
    ;;
  esac

  local rstatus=${response[2]}
  case "$rstatus" in
    (OK)
    sessionid="${response[3]/
(#e)/}"
    npurl="$response[4]"
    submiturl="$response[5]"
    ;;
    (BANNED)
    print "Client is banned.  Try getting a newer version at http://zomg.alioth.debian.org/"
    exit 1
    ;;
    (BADAUTH)
    print "Bad authentication.  Check your credentials in ~/.zomg/conf"
    exit 1
    ;;
    (BADTIME)
    print "Time mismatch.  Check your system clock."
    exit 1
    ;;
    (FAILED *)
    print "FAILURE."

    print $rstatus
    return 1
    ;;
    (*)
    print $rstatus what the fuck
    print '[[['
    print -l ${http_headers}
    print ']]]'
    print '{{{'
    print -l ${response}
    print '}}}'
    return 1

    ;;
  esac

  as_cache_credentials "$username" "$sessionid" "$npurl" "$submiturl"
}

as_cache_credentials() {
  print "Caching credentials for $1..."
  cat >~/.zomg/ascreds.${1} <<EOF
sessionid="${2}"
npurl="${3}"
submiturl="${4}"
EOF

}

as_validate_handshake() {
  local credfile
  credfile=( ~/.zomg/ascreds.${1}(Nmm-30) )
  if [[ -f ~/.zomg/ascreds."$1" ]] && [[ -n $credfile ]];
  then
    print "Refreshing credentials for $1..."
  else
    audioscrobbler_handshake "$@" || return 1
  fi

  source ~/.zomg/ascreds."${1}"
  return 0
}

as_invalidate_handshake() {
  print "Invalidating credentials for $1."
  rm -f ~/.zomg/ascreds."$1"
}

audioscrobbler_urlencode() {
  if (( $+options[multibyte] )); then
    setopt localoptions extendedglob nomultibyte
  else
    setopt localoptions extendedglob
  fi

  input=( ${(s::)1} )
  print -- ${(j::)input/(#b)([^A-Za-z0-9_.!~*\'\(\)-])/%$(([##16]#match))}
}

audioscrobbler_constructauthtoken() {
  local password=$1
  local cr="$(print -n "${password}" | md5sum | awk '{print $1}')${challenge}"
  local challengeresponse="$(print -n "${cr}" | md5sum | awk '{print $1}')"

  print $challengeresponse
}

audioscrobbler_construct_np_query() {
  local username=$(audioscrobbler_urlencode "$1") #unused
  local sid="$2"
  local artist=$(audioscrobbler_urlencode "$3")
  local track=$(audioscrobbler_urlencode "$4")
  local album=$(audioscrobbler_urlencode "$5")
  local mbid=$(audioscrobbler_urlencode "$6")
  local length=$(audioscrobbler_urlencode "$7")
  local ttime=$(audioscrobbler_urlencode "$8") #unused
  # need tracknum

  reply=("&s=${sid}" "&a=${artist}&t=${track}&b=${album}&l=${length}&n=${tracknum}&m=${mbid}"$'\0')
}

audioscrobbler_np_submit() {
  local poststr="$1"
  local username="$2"
  local conlen=$#poststr
  local sstatus

  if (( submit == 1 ));
  then

    print "connecting to $np_submithost:$np_submitport"

    tcp_open $np_submithost $np_submitport np_mit || return 3
    tcp_send -s np_mit -- "POST /${np_submitcgi} HTTP/1.1"
    tcp_send -s np_mit -- "Host: $np_submithost"
    tcp_send -s np_mit -- "Accept-Charset: utf-8"
    tcp_send -s np_mit -- "Content-Type: application/x-www-form-urlencoded"
    tcp_send -s np_mit -- "Content-Length: $conlen"
    tcp_send -s np_mit -- ""
    tcp_send -s np_mit -- "$poststr"
    print -r "Posting to /${np_submitcgi}:"
    print -r "$poststr"

    sleep 2
    tcp_read -d -s np_mit
    tcp_close -s np_mit

    http_headers=(${tcp_lines[1,(r)
]})
    xferencoding="${${http_headers[(r)Transfer-Encoding:*]}#Transfer-Encoding: }"
    case "$xferencoding" in
      (chunked*)
      startofbody=${tcp_lines[(i)
]}
      (( startofbody+=2 ))
      response=(${tcp_lines[startofbody,(r)0
]})
      ;;
      (*)
      startofbody=${tcp_lines[(i)
]}
      (( startofbody++ ))
      response=(${tcp_lines[startofbody,-1]})
      ;;
    esac

    sstatus=${response[1]}

    print "Status: $sstatus"
    if [[ $sstatus != "OK" ]];
    then
      submit=0
      print "FAILED ($sstatus)"
      print -l "{{{" "$response" "}}}"
      return 8
    fi

  else
    return 9
  fi

}


audioscrobbler_constructquery() {
  local sid="$1"
  local artist=$(audioscrobbler_urlencode "$2")
  local track=$(audioscrobbler_urlencode "$3")
  local album=$(audioscrobbler_urlencode "$4")
  local mbid=$(audioscrobbler_urlencode "$5")
  local length=$(audioscrobbler_urlencode "$6")
  local ttime=$(audioscrobbler_urlencode "$7")
  local source="$8"
#need tracknum

  reply=("&s=${sid}" "&a[0]=${artist}&t[0]=${track}&i[0]=${ttime}&o[0]=${source}&r[0]=&l[0]=${length}&b[0]=${album}&n[0]=${tracknum}&m[0]=${mbid}"$'\0')
}

audioscrobbler_submit() {
  local poststr="$1"
  local username="$2"
  local conlen=$#poststr
  local sstatus

  if (( submit == 1 ));
  then

    print "connecting to $submithost:$submitport"

    tcp_open $submithost $submitport scrobmit || return 3
    tcp_send -s scrobmit -- "POST /${submitcgi} HTTP/1.1"
    tcp_send -s scrobmit -- "Host: $submithost"
    tcp_send -s scrobmit -- "Accept-Charset: utf-8"
    tcp_send -s scrobmit -- "Content-Type: application/x-www-form-urlencoded"
    tcp_send -s scrobmit -- "Content-Length: $conlen"
    tcp_send -s scrobmit -- ""
    tcp_send -s scrobmit -- "$poststr"
    print -r "Posting to /${submitcgi}:"
    print -r "$poststr"

    sleep 2
    tcp_read -d -s scrobmit
    tcp_close -s scrobmit

    http_headers=(${tcp_lines[1,(r)
]})
    xferencoding="${${http_headers[(r)Transfer-Encoding:*]}#Transfer-Encoding: }"
    case "$xferencoding" in
      (chunked*)
      startofbody=${tcp_lines[(i)
]}
      (( startofbody+=2 ))
      response=(${tcp_lines[startofbody,(r)0
]})
      ;;
      (*)
      startofbody=${tcp_lines[(i)
]}
      (( startofbody++ ))
      response=(${tcp_lines[startofbody,-1]})
      ;;
    esac

    sstatus=${response[1]}

    print "Status: $sstatus"
    if [[ $sstatus != "OK" ]];
    then
      submit=0
      print "FAILED ($sstatus)"
      as_invalidate_handshake "$username"
      return 8
    fi

  else
    return 9
  fi
 }