Advertisement
turlando

mpdtweet.pl

Feb 3rd, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.93 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use v5.10;
  4. use YAML;
  5. use Net::Twitter::Lite;
  6. use Audio::MPD;
  7.  
  8. my $twitter = Net::Twitter::Lite->new(
  9.     consumer_key    => '',
  10.     consumer_secret => ''
  11. );
  12.  
  13. my $mpd = Audio::MPD->new;
  14. my $song = $mpd->current
  15.     or die "MPD is paused/stopped.\n";
  16. my $artist = $song->artist;
  17. my $album = $song->album;
  18. my $title = $song->title;
  19.  
  20.  
  21. sub get_config_file
  22. {
  23.     my @configpaths = (
  24.         'config.yml',
  25.         '~/.config/mpdtweet/config.yml',
  26.     );
  27.     for (@configpaths) {
  28.         return $_ if -e $_;
  29.     }
  30. }
  31.  
  32. sub first_run
  33. {
  34.     print "I haven't found any configuration file, maybe this is the first time you run this software.\n";
  35.     print "Would you like to start the first run wizard? [y/n] "; &sure;
  36.     sub sure
  37.     {
  38.         chomp (my $yn = <STDIN>);
  39.         given ($yn) {
  40.             when (/n/) {
  41.                 print "KBAI\n";
  42.                 exit 0;
  43.             }
  44.             when (/y/) {
  45.                 &make_sample_config(&obtain_auth);
  46.                 print "Configuration and authorization finisched!\n";
  47.             }
  48.             default {
  49.                 print "$yn is not a valid answer. Try again "; &sure;
  50.             }
  51.         }
  52.     }
  53. }
  54.  
  55. sub obtain_auth
  56. {
  57.     $twitter->access_token("");
  58.     $twitter->access_token_secret("");
  59.     unless ($twitter->authorized) {
  60.         print "In order to use this I have to obtain the permissions from twitter. Asking them...\n";
  61.         print "Please go there", $twitter->get_authorization_url, "\n";
  62.         print "And insert here the PIN: ";
  63.         my $pin = <STDIN>; chomp $pin;
  64.         my ($access_token, $access_token_secret) = $twitter->request_access_token(verifier => $pin);
  65.         print "ACCESS_TOKEN        => $access_token\n";
  66.         print "ACCESS_TOKEN_SECRET => $access_token_secret\n";
  67.         return ($access_token, $access_token_secret);
  68.     }
  69. }
  70.  
  71. sub make_sample_config
  72. {
  73.     my $first_config = {
  74.       patterns => [
  75.        '#nowplaying %a - %t (%d)',
  76.        '#nowplaying %a - %t',
  77.         ],
  78.         'access_token' => $_[0],
  79.         'access_token_secret' => $_[1],
  80.     };
  81.     open my $first_configfile, '>', 'config.yml'
  82.         or die "Can't make config file: $!";
  83.     print $first_configfile Dump($first_config);
  84.     close $first_configfile;
  85. }
  86.  
  87. sub load_conf
  88. {
  89.     my $config = YAML::LoadFile($_[0]);
  90.     my %configuration = %$config;
  91.     return %configuration;
  92. }
  93.  
  94. sub substitute
  95. {
  96.     my $arg = $_[0];
  97.     for ($arg) {
  98.         s/%a/$artist/g;
  99.         s/%d/$album/g;
  100.         s/%t/$title/g;
  101.     }
  102.     return $arg;
  103. }
  104.  
  105. open my $configfile, '<', &get_config_file
  106.     or &first_run and exit 0;
  107. my %config = &load_conf($configfile);
  108. close $configfile;
  109.  
  110. sub mkstatus
  111. {
  112.     for my $i (0 .. $#{$config{patterns}}) {
  113.         return substitute($config{patterns}[$i]) if length substitute($config{patterns}[$i]) <= 140;
  114.     }
  115.     my $pattern = "#nowplaying $artist - $title";
  116.     while (length $pattern > 140) {
  117.         $pattern = substr($pattern, 0, -1);
  118.         $pattern += "$pattern…";
  119.     }
  120.     return $pattern;
  121. }
  122.  
  123. $twitter->access_token($config{access_token});
  124. $twitter->access_token_secret($config{access_token_secret});
  125.  
  126. my $status = &mkstatus;
  127. print "Tweeting «$status».\n";
  128. eval { $twitter->update($status) };
  129. warn "$@\n" if $@;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement