Now playing - Twitter or Last.fm?

Hi

Is there a script which can send the current song to Twitter? Or Last.fm? Or both?

Thanks
EC

I’m after a Last.fm script myself Eddie but Torben did tweet me this

“the recent scrobbling protocol is so complex that it cannot be done with a simple script.”

The same is true for Twitter now. You need to perform OAuth authentication, and obtain an API key for your application, and so forth.

To get an idea of how complicated it his, here’s an implementation in PHP I found: http://www.1stwebdesigner.com/tutorials/twitter-app-oauth-php/

(If you’re running a web server, it might be possible to use the above PHP app together with mAirList - install the app on your web server and use HTTP GET calls from mAirList to post status updates to it.)

If it helps anyone, the official ‘how to Scrobble’ documentation for last.fm is here:
http://www.lastfm.de/api/scrobbling

BUT please don’t ask me for further info! I don’t use last.fm (whatever that is?) and I don’t do HTTP ‘programming’ either, so I genuinely can’t help you any more than supplying the link above.

BFN
Cad

Every time a song is played we’ve set mAirList v3 to send the artist and title to a PHP script on our web server using HTTP POST.

The PHP script adds the information to a MySQL database, writes to a simple ‘no playing’ text file which is included on the website and then scrobbles to a last.fm account.

The last.fm part was achieved with the link Cad posted above, and mostly based on this script:

https://github.com/desfrenes/PHP-Scrobbler/blob/master/Scrobbler.php

The script is still updating the profile today.

[quote=“Torben, post:3, topic:8352”]The same is true for Twitter now. You need to perform OAuth authentication, and obtain an API key for your application, and so forth.

To get an idea of how complicated it his, here’s an implementation in PHP I found: http://www.1stwebdesigner.com/tutorials/twitter-app-oauth-php/

(If you’re running a web server, it might be possible to use the above PHP app together with mAirList - install the app on your web server and use HTTP GET calls from mAirList to post status updates to it.)[/quote]

It is not that hard. See this simple tutorial. In German unfortunately but PHP is inernational and for the rest we have Google Translator: http://frank-it-beratung.com/blog/2011/04/15/tutorial-twitter-api-mit-php-bordmitteln/.

And if you are a bit skilled in Linux you can do it with 3 lines of code. The command ‘curl’ and the simple mobile Twitter logon page (https://mobile.twitter.com/signup) are your friends.

You could do it in Perl. I had to write something similar for the new API. This is a little script to get the last 5 tweets. You could easily change it, to post a message.

[code]#!/usr/bin/perl -T

use strict;
use warnings;
use CGI::Carp qw(fatalsToBrowser);

Twitter auth crap

my $Twitter_consumer_key = “8g90fd8g9sd8g9dßgf9”;
my $Twitter_consumer_secret = “8g90a8g9a0sd8g98ads98gds89g8ads0ß8g9dgd9”;
my $Twitter_token = “f87jh23rjh2jrh2j3hrj23r8sd0g7890sdg8asdg8907ads89g07”;
my $Twitter_token_secret = “25j5loru89ru8239r23hr2h3r9h2q389rhq283hr89”;

Print HTTP Header, get the last 5 tweets and print them

print “Content-type: text/html; charset=utf-8\n\n”;
my $Last5TwitterMessages = &GetTwitterStuff( “MyTwitterUserID”, 5 );
print $Last5TwitterMessages;

GetTwitterStuff

########################
sub GetTwitterStuff {
my ($request) = @_;
my $htout = “”;

use URI::Find;
use Date::Calc qw(Add_Delta_DHMS);
use Net::Twitter::Lite::WithAPIv1_1;
use Scalar::Util ‘blessed’;

my $DE_today = “heute”;

for my $line ($request) {
    my ($user_id,$count) = split (/\#/, $line); {
    my $content = "";
    my $file = "";


	my $request = Net::Twitter::Lite::WithAPIv1_1->new(
		consumer_key        => $Twitter_consumer_key,
		consumer_secret     => $Twitter_consumer_secret,
		access_token        => $Twitter_token,
		access_token_secret => $Twitter_token_secret,
	);

		my $tweets = $request->user_timeline({ user_id => $user_id, count => $count, include_entities => 'true'  });

		$htout.= "<ul>\n";
		 my $cnt=0;

		for my $item ( @$tweets ) {

			next unless defined ( $item->{'text'} );

			# reformat  RFC822 compliant date like "Wed Jun 19 16:29:58 +0000 2013";
													
			my $timestamp = $item->{'created_at'};
			$timestamp =~ m/([a-zA-Z]+)\s([a-zA-Z]+)\s(\d{2})\s(\d{2})\:(\d{2})\:(\d{2})\s\+(\d{4})\s(\d{4})/;
			my $day = $3;
			my $monthname = lc($2);
			my $year = $8;
			my $hour = $4;
			my $min = $5;
			my $sec = $6;

			my %month = qw(
			jan 01  feb 02  mar 03  apr 04  may 05  jun 06
			jul 07  aug 08  sep 09  oct 10 nov 11 dec 12
			);

			my $month = $month{$monthname};

			# adjust twitter time offset (2 hours)
			my ($year,$month,$day, $hour,$min,$sec) = Add_Delta_DHMS($year,$month,$day, $hour,$min,$sec, 0, +2, 0, 0);

			if (length($month) == 1) {$month = "0".$month;}
			if (length($day) == 1) {$day = "0".$day;}
			if (length($hour) == 1) {$hour = "0".$hour;}
			if (length($min) == 1) {$min = "0".$min;}

			$timestamp = $year.$month.$day.$hour.$min;
			my $pubDate = &GetLocalDateAndDay($timestamp);


			# compare localday and postday to set $pubDate to a word like "today"
			my $TimeToday = strftime( "%Y%m%d", &adjtime( localtime( time ) ) );
			my $TimePost = $year.$month{$monthname}.$day;

			if ($TimeToday eq $TimePost) {
			$pubDate = "$DE_today<br />$hour:$min Uhr";
			}

			else {
			$pubDate = "$pubDate<br />$hour:$min Uhr";
			}


			# get author nickname and author avatar
			my $nick = "";
			my $avatar = "";

			if (defined $item->{user}->{screen_name}) {
			$nick = $item->{user}->{screen_name};
			$avatar = $item->{user}->{profile_image_url};

			}

			if (defined $item->{retweeted_status}->{user}->{screen_name}) {
			$nick = $item->{retweeted_status}->{user}->{screen_name};
			$avatar = $item->{retweeted_status}->{user}->{profile_image_url};
			}

			$avatar =~ s/\_normal\.jpeg/\_bigger\.jpeg/g;

			# parse all links out of $item->{'title'}
			my $finder = URI::Find->new(sub {
			my ($uri, $orig_uri) = @_;
			return qq|<a href="$uri" target\=\"\_blank\" class\=\"twitterLink\"\>$orig_uri</a>|;
			});

			$finder->find(\$item->{'text'});

			$item->{'text'} =~ s/RT\s\@[a-zA-Z0-9_]+\:(.*?)/$1/g;
			$item->{'text'} =~ s/\@([a-zA-Z0-9_]+)/\<a href\=\"$TwitterMainURL$1\" target\=\"\_blank\" class\=\"twitterUsername\" \>\@$1\<\/a\>/g;
			$item->{'text'} =~ s/\#([^\"\<\>\s\,\.\;]+)/\<a href\=\"$TwitterHashTagSearchURL$1\" target\=\"\_blank\" class\=\"twitterHashtag\"\>\#$1\<\/a\>/g;

			# put all stuff together
			$htout.= qq|<li><a href="$TwitterMainURL$nick" target="_blank"><img src="$avatar" width="73" height="73" alt="$nick" class="twitterAvatar"></a><span class="twitterName"><a href="$TwitterMainURL$nick" target="_blank">$nick:</a></span>
							<p class="twitterTweet">$item->{'text'}</p>
							<p class="twitterTime">$pubDate</p>
						</li>|;

			$cnt++;
			last if ($cnt >= $count);
        }

		$htout.= "</ul>";
}

}

return &EncodeUTF8($htout);
}
########################

end GetTwitterStuff

sub EncodeUTF8 {
my ($request) = @_;
use Unicode::String qw(utf8);
my $encode = utf8($request);
return $encode;
}[/code]