michaelbell

Last.fm to Twitter

Description: 

PHP script to post what your listening to to Twitter. It will only post the album when it changes.

Due to the last.fm api it's a bit ropey because the results are inconsistent. The twitter code was taken from the twitter api examples.

You'll need a twitter api key a Last.fm account. You can follow me on twitter @digital_mike

  1. <?php
  2.  
  3. //Last.fm to twitter - posts current album to twitter
  4. //Version 0.75
  5. //Bug:Unknown Album is firing WAY too often, not writing to text file
  6. //Bug:Cutting of of data (album, dred = dredg)
  7.  
  8. //constants
  9. $twitter_username = 'TWITTER ACCOUNT NAME';
  10. $twitter_password = 'TWITTER PASSWORD';
  11.  
  12. $errno = 0;
  13. $errstr = '';
  14. $response = '';
  15.  
  16. function httpRequest($host, $path = '/', $method = 'GET') {
  17.  
  18. global $errno, $errstr, $response;
  19. global $twitter_username, $twitter_password;
  20.  
  21. $header = "$method $path HTTP/1.1\r\n";
  22. $header .= "Host: $host\r\n";
  23. $header .= "Accept-Encoding: none\r\n";
  24. $header .= "Authorization: Basic " . base64_encode("{$twitter_username}:{$twitter_password}") . "\r\n";
  25. $header .= "Connection: Close\r\n\r\n";
  26.  
  27. $sock = fsockopen($host, 80, $errno, $errstr, 30);
  28. if (!$sock) {
  29. die("<p><strong>fsockopen() error:</strong><br />$errstr ($errno)</p>");
  30. } else {
  31. fwrite($sock, $header);
  32. while (!feof($sock)) {
  33. $response .= fgets($sock, 128);
  34. }
  35. fclose($sock);
  36.  
  37. $response = trim(str_replace(array('<', '>'), array('&lt;', '&gt;'), $response));
  38. return true;
  39. }
  40.  
  41. }
  42.  
  43. $count = '0';
  44.  
  45. //function to get lastfm data
  46. function getLastfmdata() {
  47. $lastfm_username = "LASTFM USERNAME";
  48. $lastfm_apikey = 'LASTFM API KEY';
  49. $myFile = "data.txt";
  50.  
  51. $lfm = simplexml_load_file("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=".$lastfm_username."&api_key=".$lastfm_apikey);
  52.  
  53. //get the data and write to variables and text doc
  54. foreach ($lfm->recenttracks as $recent) {
  55. foreach ($recent->track as $track) {
  56. global $artist;
  57. $artist = $track->artist;
  58. global $album;
  59. $album = $track->album; //gets data
  60. echo $album . ' by ' . $artist . ' - LAST.FM DATA<br />';
  61.  
  62. //get old data
  63. //set flag if different
  64. //write new data if different
  65.  
  66. $fh = fopen($myFile, 'r');
  67. $datalength = fread($fh, filesize($myFile));
  68. fclose($fh);
  69. list($finalAlbum, $finalArtist) = split('\#-#', $datalength);
  70.  
  71. global $Falbum;
  72. $Falbum = $finalAlbum;
  73. global $Fartist;
  74. $Fartist = $finalArtist;
  75. global $flag;
  76. if ($finalAlbum == $album) {
  77. $flag = '1';
  78. }
  79.  
  80. $count = $count + 1; // count so it stops after one
  81. $fh = fopen($myFile, 'w') or die("can't open file"); //writes data to text file
  82. $stringData = $album;
  83. if ($album == "") {
  84. $stringData = "Unkown Album";
  85. }
  86. fwrite($fh, $stringData);
  87. $stringData = "#-#";
  88. fwrite($fh, $stringData);
  89. $stringData = $artist;
  90. fwrite($fh, $stringData);
  91. fclose($fh);
  92. $LFalbum = $artist;
  93. if ($count == "1") { // stop after the first record
  94. break;
  95. }
  96. }
  97. }
  98. };
  99.  
  100. getLastfmdata();
  101. $myFile = "data.txt";
  102. echo $flag;
  103. $fh = fopen($myFile, 'r');
  104. $datalength = fread($fh, filesize($myFile));
  105. fclose($fh);
  106. list($finalAlbum, $finalArtist) = split('\#-#', $datalength);
  107.  
  108. if ($flag != '1') {
  109. $finaltwitter = "Listening to: ".$finalAlbum." by ".$finalArtist;
  110. $finaltwitter3 = urlencode($finaltwitter);
  111. echo $finaltwitter . '- SENT TO TWITTER<br />';
  112. httpRequest('twitter.com', "/statuses/update.xml?status=$finaltwitter3", 'POST');
  113. echo "<p>Response:<br /><hr /><pre>$response</pre><hr /></p>\n";
  114. }
  115. else
  116. {
  117. echo 'SAME ALBUM';
  118. }
  119. ?>