Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- use strict;
- use v5.10;
- use YAML;
- use Net::Twitter::Lite;
- use Audio::MPD;
- my $twitter = Net::Twitter::Lite->new(
- consumer_key => '',
- consumer_secret => ''
- );
- my $mpd = Audio::MPD->new;
- my $song = $mpd->current
- or die "MPD is paused/stopped.\n";
- my $artist = $song->artist;
- my $album = $song->album;
- my $title = $song->title;
- sub get_config_file
- {
- my @configpaths = (
- 'config.yml',
- '~/.config/mpdtweet/config.yml',
- );
- for (@configpaths) {
- return $_ if -e $_;
- }
- }
- sub first_run
- {
- print "I haven't found any configuration file, maybe this is the first time you run this software.\n";
- print "Would you like to start the first run wizard? [y/n] "; &sure;
- sub sure
- {
- chomp (my $yn = <STDIN>);
- given ($yn) {
- when (/n/) {
- print "KBAI\n";
- exit 0;
- }
- when (/y/) {
- &make_sample_config(&obtain_auth);
- print "Configuration and authorization finisched!\n";
- }
- default {
- print "$yn is not a valid answer. Try again "; &sure;
- }
- }
- }
- }
- sub obtain_auth
- {
- $twitter->access_token("");
- $twitter->access_token_secret("");
- unless ($twitter->authorized) {
- print "In order to use this I have to obtain the permissions from twitter. Asking them...\n";
- print "Please go there", $twitter->get_authorization_url, "\n";
- print "And insert here the PIN: ";
- my $pin = <STDIN>; chomp $pin;
- my ($access_token, $access_token_secret) = $twitter->request_access_token(verifier => $pin);
- print "ACCESS_TOKEN => $access_token\n";
- print "ACCESS_TOKEN_SECRET => $access_token_secret\n";
- return ($access_token, $access_token_secret);
- }
- }
- sub make_sample_config
- {
- my $first_config = {
- patterns => [
- '#nowplaying %a - %t (%d)',
- '#nowplaying %a - %t',
- ],
- 'access_token' => $_[0],
- 'access_token_secret' => $_[1],
- };
- open my $first_configfile, '>', 'config.yml'
- or die "Can't make config file: $!";
- print $first_configfile Dump($first_config);
- close $first_configfile;
- }
- sub load_conf
- {
- my $config = YAML::LoadFile($_[0]);
- my %configuration = %$config;
- return %configuration;
- }
- sub substitute
- {
- my $arg = $_[0];
- for ($arg) {
- s/%a/$artist/g;
- s/%d/$album/g;
- s/%t/$title/g;
- }
- return $arg;
- }
- open my $configfile, '<', &get_config_file
- or &first_run and exit 0;
- my %config = &load_conf($configfile);
- close $configfile;
- sub mkstatus
- {
- for my $i (0 .. $#{$config{patterns}}) {
- return substitute($config{patterns}[$i]) if length substitute($config{patterns}[$i]) <= 140;
- }
- my $pattern = "#nowplaying $artist - $title";
- while (length $pattern > 140) {
- $pattern = substr($pattern, 0, -1);
- $pattern += "$pattern…";
- }
- return $pattern;
- }
- $twitter->access_token($config{access_token});
- $twitter->access_token_secret($config{access_token_secret});
- my $status = &mkstatus;
- print "Tweeting «$status».\n";
- eval { $twitter->update($status) };
- warn "$@\n" if $@;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement