Update of /cvsroot/lastbash/lastawk
In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv7134
Added Files:
lastawk
Log Message:
Added the awk version of the program.
--- NEW FILE: lastawk ---
#!/usr/bin/gawk -f
BEGIN {
# Enable or disable debugging
debug = 0
# Server details
lastfm["host"] = "ws.audioscrobbler.com"
lastfm["port"] = "80"
lastfm["version"] = "0.1"
lastfm["platform"] = "linux"
# The playlist
playlist="playlist.m3u"
# Login first
lastfm_connect("cstroie", "00000000000000000000000000000000")
# Parse the command line
commandline()
}
function commandline()
{
if (ARGV[1] == "connect")
{
print "Connected to server..."
print " session: " lastfm["session"]
print " stream : " lastfm["stream_url"]
# Create the playlist
printf("#EXTM3U\r\n") > playlist
printf("#EXTINF:-1,%s\r\n", "Last.fm") > playlist
printf("%s\r\n", lastfm["stream_url"]) > playlist
close(playlist)
}
else if (ARGV[1] == "skip")
{
print "Next track..."
lastfm_command("skip")
show_metadata()
}
else if (ARGV[1] == "love")
{
print "Track love ;) ..."
lastfm_command("love")
show_metadata()
}
else if (ARGV[1] == "ban")
{
print "Track banned, next one please..."
lastfm_command("ban")
show_metadata()
}
else if (ARGV[1] == "artist")
{
if (ARGV[2] != "")
{
print "Similar artists to " ARGV[2]
lastfm_station("lastfm://artist/" ARGV[2] "/similarartists")
show_metadata()
}
else
print "Specify the artist too."
}
else if (ARGV[1] == "tags")
{
if (ARGV[2] != "")
{
print "Tune to the next tags: " ARGV[2]
lastfm_station("lastfm://globaltags/" ARGV[2])
show_metadata()
}
else
print "Specify the tags too."
}
else if (ARGV[1] == "help")
{
print "Use the next commands:"
print " connect"
print " skip"
print " love"
print " ban"
print " info"
print " artist <artist>"
print " tags <tags>"
}
else
show_metadata()
}
function show_metadata()
{
delete metadata
lastfm_metadata(metadata)
if (metadata["streaming"] == "true")
{
print " Artist : " metadata["artist"]
print " Title : " metadata["track"]
print " Album : " metadata["album"]
print " Cover : " metadata["albumcover_medium"]
print " Length : " metadata["trackduration"]
print " Station: " metadata["station"]
}
else
print "Not streaming."
}
function lastfm_connect(username, password, url, headers, response)
{
url = "http://" lastfm["host"] ":" lastfm["port"] \
"/radio/handshake.php" \
"?version=" lastfm["version"] \
"&platform=" lastfm["platform"] \
"&username=" username \
"&passwordmd5=" password
http_req(url, headers, response)
if (debug) for(i in headers)
print "H> " i ": " headers[i]
for(i in response)
{
if (debug) print "R> " i ": " response[i]
lastfm[i] = response[i]
}
}
function lastfm_command(cmd, url, headers, response)
{
# Commands are: skip, love, ban, rtp, nortp
url = "http://" lastfm["base_url"] ":" lastfm["port"] \
lastfm["base_path"] \
"/control.php" \
"?session=" lastfm["session"] \
"&command=" cmd
http_req(url, headers, response)
if (debug) print "R> response: " response["response"]
}
function lastfm_station(station, url, headers, response)
{
url = "http://" lastfm["base_url"] ":" lastfm["port"] \
lastfm["base_path"] \
"/adjust.php" \
"?session=" lastfm["session"] \
"&url=" station
http_req(url, headers, response)
if (debug) print "R> response: " response["response"]
}
function lastfm_metadata(metadata, url, headers, response)
{
url = "http://" lastfm["base_url"] ":" lastfm["port"] \
lastfm["base_path"] \
"/np.php" \
"?session=" lastfm["session"]
http_req(url, headers, response)
for(i in response)
{
if (debug) print "M> " i ": " response[i]
metadata[i] = response[i]
}
}
function http_req(url, headers, response, mch, service, rsp, rsp_mch)
{
delete headers
delete response
match(url, /http:\/\/([^:/]+)(:?([^/]+))(.*)/, mch)
service = "/inet/tcp/0/" mch[1] "/" mch[3]
RS = "\n"
print "GET " mch[4] " HTTP/1.0\r\n" \
"Host: " mch[1] "\r\n" \
"\r\n" |& service
while (service |& getline rsp)
{
sub(/\r/, "", rsp)
if (rsp == "") break
if (match(rsp, /^([^:]+): *(.+)/, rsp_mch))
headers[rsp_mch[1]] = rsp_mch[2]
}
while (service |& getline rsp)
{
sub(/\r/, "", rsp)
if (rsp == "") break
if (match(rsp, /^([^=]+)=(.+)/, rsp_mch))
response[rsp_mch[1]] = rsp_mch[2]
}
close(service)
}
# vim: set ft=awk nu nowrap:
|