Advertisement
turlando

mpdtweet2.pl

Feb 4th, 2012
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.31 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use v5.10;
  4. use YAML;
  5. use Audio::MPD;
  6. use Net::Twitter::Lite;
  7.  
  8. sub get_config_file
  9. {
  10.     my @configpaths = (
  11.         "config.yml",
  12. #       "$ENV{'HOME'}/.config/mpdtweet/config.yml",
  13.     );
  14.     for (@configpaths) {
  15.         return $_ if -e $_;
  16.     }
  17. }
  18.  
  19. sub load_conf
  20. {
  21.     my $config = YAML::LoadFile($_[0]);
  22.     my %configuration = %$config;
  23.     return %configuration;
  24. }
  25.  
  26. sub first_run
  27. {
  28.     print "I haven't found any configuration file, maybe this is the first time you run this software.\n";
  29.     &sure;
  30.     sub sure
  31.     {
  32.         print "Would you like to start the first run wizard? [y/n] ";
  33.         chomp (my $choice = <STDIN>);
  34.         given ($choice) {
  35.             when (/n/) {
  36.                 print "KBAI\n";
  37.                 exit 0;
  38.             }
  39.             when (/y/) {
  40.                 print "I need your twitter username and password to tweet.\n";
  41.                 my ($twitter_username, $twitter_password) = &request_twitter_username_and_password;
  42.                 print "I need some informations about how to connect to MPD.\n";
  43.                 my ($mpd_host, $mpd_port, $mpd_password) = &request_mpd_informations;
  44.                 &make_sample_config($twitter_username, $twitter_password, $mpd_host, $mpd_port, $mpd_password);
  45.                 print "Configuration finished!\n";
  46.             }
  47.             default {
  48.                 print "$choice is not a valid answer.\n"; &sure;
  49.             }
  50.         }
  51.     }  
  52. }
  53.  
  54. sub make_sample_config
  55. {
  56.     my $first_config = {
  57.         'twitter_username' => $_[0],
  58.         'twitter_password' => $_[1],
  59.         'mpd_host'     => $_[2],
  60.         'mpd_port'     => $_[3],
  61.         'mpd_password' => $_[4],
  62.       patterns => [
  63.        '#nowplaying %a - %t (%d)',
  64.        '#nowplaying %a - %t',
  65.         ],
  66.         patterns_comment => [
  67.             '#nowplaying %a - %t (%d) « %c',
  68.             '#nowplaying %a - %t « %c',
  69.         ],
  70.     };
  71.     open my $first_configfile, '>', 'config.yml'
  72.         or die "Can't make config file: $!";
  73.     print $first_configfile Dump($first_config);
  74.     close $first_configfile;
  75. }
  76.  
  77. sub request_twitter_username_and_password
  78. {
  79.     sub request_twitter_username
  80.     {
  81.         print "Username: ";
  82.         chomp (my $username = <STDIN>);
  83.         &request_username if !$username;
  84.         return $username;
  85.     }
  86.     sub request_twitter_password
  87.     {
  88.         print "Password: ";
  89.         chomp(my $password = <STDIN>);
  90.         &request_password if !$password;
  91.         return $password;
  92.     }
  93.     return (&request_twitter_username, &request_twitter_password);
  94. }
  95.  
  96. sub request_mpd_informations
  97. {
  98.     sub request_mpd_host
  99.     {
  100.         print "Host [localhost]: ";
  101.         chomp (my $host = <STDIN>);
  102.         return "localhost" if !$host;
  103.     }
  104.     sub request_mpd_port
  105.     {
  106.         print "Port [6600]: ";
  107.         chomp (my $port = <STDIN>);
  108.         return 6600 if !$port;
  109.     }
  110.     sub request_mpd_password
  111.     {
  112.         print "Password:";
  113.         chomp(my $password = <STDIN>);
  114.         return "" if !$password;
  115.     }
  116.     return (&request_mpd_host, &request_mpd_port, &request_mpd_password);
  117. }
  118.  
  119. open my $configfile, '<', &get_config_file
  120.     or &first_run and exit 0;
  121. my %config = &load_conf($configfile);
  122. close $configfile;
  123.  
  124. my $mpd = Audio::MPD->new(
  125.     host     => $config{mpd_host},
  126.     port     => $config{mpd_port},
  127.     password => $config{mpd_password},
  128. );
  129.  
  130. my $twitter = Net::Twitter::Lite->new(
  131.     username => $config{twitter_username},
  132.     password => $config{twitter_password},
  133. );
  134.  
  135. my $song = $mpd->current
  136.     or die "MPD is paused/stopped.\n";
  137. my $artist = $song->artist;
  138. my $album = $song->album;
  139. my $title = $song->title;
  140. my $comment = $ARGV[0] if $ARGV[0];
  141.  
  142. sub make_status
  143. {
  144.     sub substitute
  145.     {
  146.         my $arg = $_[0];
  147.         for ($arg) {
  148.             s/%a/$artist/g;
  149.             s/%d/$album/g;
  150.             s/%t/$title/g;
  151.             if ($comment) {
  152.                 s/%c/$comment/g;
  153.             }
  154.         }
  155.         return $arg;
  156.     }
  157.     if ($comment) {
  158.         for my $i (0 .. $#{$config{patterns_comment}}) {
  159.             return &substitute($config{patterns_comment}[$i]) if length &substitute($config{patterns_comment}[$i]) <= 140;
  160.         }
  161.         my $pattern = "$artist - $title « $comment";
  162.         while (length $pattern > 140) {
  163.             $title = substr($title, 0, -1);
  164.             $title += "…";
  165.             $pattern = "#nowplaying $artist - $title « $comment";
  166.         }
  167.         return $pattern;
  168.     } else {
  169.         for my $i (0 .. $#{$config{patterns}}) {
  170.             return &substitute($config{patterns}[$i]) if length &substitute($config{patterns}[$i]) <= 140;
  171.         }
  172.         my $pattern = "$artist - $title";
  173.         while (length $pattern > 140) {
  174.             $title = substr($title, 0, -1);
  175.             $title += "…";
  176.             $pattern = "#nowplaying $artist - $title";
  177.         }
  178.         return $pattern;
  179.     }
  180. }
  181.  
  182. my $status = &make_status;
  183. print "Tweeting «$status».\n";
  184. eval { $twitter->update($status) };
  185. warn "$@\n" if $@;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement