<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
<?xml-stylesheet type="text/css" href="http://linuxevolution.org/styles/feed.css"?>


<title type="html">Filed under: Last_FM | Linux Evolution dot org and the NSLU2</title>
<subtitle type="html"></subtitle>
<link rel="alternate" type="text/html" href="http://linuxevolution.org"/>
<link rel="self" type="application/atom+xml" href="http://linuxevolution.org/archives/last_fm/index-atom.xml"/>
<updated>2010-12-26T18:50:23-05:00</updated>
<author>
<name>orvtech</name>
<uri>http://linuxevolution.org</uri>
</author>
<id>http://linuxevolution.org/</id>
<generator uri="http://nanoblogger.sourceforge.net" version="3.4">
NanoBlogger
</generator>

<entry>
<title type="html">The NSLU2 as a last.fm to twitter bridge.</title>
<author>
<name>orvtech</name>
</author>
<link rel="alternate" type="text/html" href="http://linuxevolution.org/archives/2009/11/30/the_nslu2_as_a_last_fm_to_twitter_bridge/index.html"/>

<id>http://linuxevolution.org/archives/2009/11/30/the_nslu2_as_a_last_fm_to_twitter_bridge/index.html</id>
<published>2009-11-30T16:57:10-05:00</published>
<updated>2009-11-30T16:57:10-05:00</updated>
<category term="Scripts" />
<category term="Last_FM" />
<category term="NSLU2" />
<category term="Linux" />
<category term="Twitter" />
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<p>This simple script came from the need of updating <a href="http://twitter.com/m1n1m3">my twitter</a> 
status with the songs that I am playing, the trick is that it will only update my profile 
if the song has been tagged as 'loved' on last.fm. It will also detect if it is Monday 
and will add the <a href="http://twitter.com/#search?q=%23mm"><i>#mm</i> hash</a>.</p>

<textarea class="bashcode" readonly wrap="off">twitter_user="<your_twitter_user_here>"
twitter_passwd="your_twitter_password_here"
URL="http://twitter.com/statuses/update.xml"
tags="#lastfm"
lastfm_api="<your_api_from_lastfm_here>"
lastfm_user="<your_lastfm_user_here>"
bit_ly_user="<your_bit.ly_user_here>"
bit_ly_apikey="<your_bit.ly_api_key_here>"

fmtwitted=`cat /tmp/lastfm2twitter.tmp | \
sed -e 's/status=Just played: //g' -e 's/#.*//g' | \
grep '[a-z]'|sed -e 's/\ //g' -e 's/\-/./g'`

fmcurrent=`curl -s "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&limit=1&user=$lastfm_user&api_key=$lastfm_api" | \
grep -E '<name>|</name>|<artist\ |</artist>' |\
head -n2| sed -e 's/<[^>]*>//g' | tr '\n' '.' |\
sed -e 's/\ //g' -e 's/.$//g'`


lastfm_song_url=`curl -s "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&limit=1&user=$lastfm_user&api_key=$lastfm_api" |\
grep -E '<url>|</url>' | sed -e 's/<[^>]*>//g' |\
head -n1| tr '\n' '.' | sed -e 's/\ //g' -e 's/.$//g'`
bit_ly_url=`curl -s "http://api.bit.ly/shorten?version=2.0.1&longUrl=$lastfm_song_url&login=$bit_ly_user&apiKey=$bit_ly_apikey" | \
grep shortUrl | sed -e 's/\"/\ /g' | awk '{print $3}'`

today_mm=`date +%A`
if [[ $today_mm == "Monday"  ]] 
  then tags="$tags #mm"
fi


if grep $fmcurrent /tmp/lastfm_loved.tmp  > /dev/null
  then if [[ "$fmtwitted" == "$fmcurrent" ]]
      then exit 0
        else curl -s "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&limit=1&user=$lastfm_user&api_key=$lastfm_api" | \
	grep -E '<name>|</name>|<artist\ |</artist>' |\
	head -n2| sed -e 's/<[^>]*>//g' | tr '\n' '-' | \
	sed -e 's/^[ \t]*//' -e 's/-/\ -\ /g' -e 's/- $//g' -e 's/\ $//g' |\
	awk '{print "status=Just played: "$0}' > /tmp/lastfm2twitter.tmp
	lastfm2twitter=`cat /tmp/lastfm2twitter.tmp; 
	echo $bit_ly_url 
	echo $tags | tr '\n' ' '`
	echo $lastfm2twitter > /tmp/lastfm2twitter.tmp
	curl -s -u $twitter_user:$twitter_passwd -d @/tmp/lastfm2twitter.tmp $URL > /dev/null
	  fi
	  else exit 0
fi</textarea>
<br />
<p>As you can see I am using bit.ly to shorten the urls of the song, you will need a bit.ly API for this to work. 
I think what is left is to optimize the script storing the curl requests in a file sort of like a cache 
and add a routine to detect if it is longer than the 140 charaters allowed by twitte and shorten the text 
to be posted as the status.r</p> 
<br />
<p><i>
<b>Spanish version:</b> <a href="http://orvtech.com/howto/el-nslu2-como-un-puente-de-last-fm-a-twitter/" rel="friend" hreflang="es">El NSLU2 como un puente de last.fm a twitter.</a>
</i></p>
</div>
</content>

</entry>

</feed>

