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 Audio::MPD;
- use Net::Twitter::Lite;
- sub get_config_file
- {
- my @configpaths = (
- "config.yml",
- # "$ENV{'HOME'}/.config/mpdtweet/config.yml",
- );
- for (@configpaths) {
- return $_ if -e $_;
- }
- }
- sub load_conf
- {
- my $config = YAML::LoadFile($_[0]);
- my %configuration = %$config;
- return %configuration;
- }
- sub first_run
- {
- print "I haven't found any configuration file, maybe this is the first time you run this software.\n";
- &sure;
- sub sure
- {
- print "Would you like to start the first run wizard? [y/n] ";
- chomp (my $choice = <STDIN>);
- given ($choice) {
- when (/n/) {
- print "KBAI\n";
- exit 0;
- }
- when (/y/) {
- print "I need your twitter username and password to tweet.\n";
- my ($twitter_username, $twitter_password) = &request_twitter_username_and_password;
- print "I need some informations about how to connect to MPD.\n";
- my ($mpd_host, $mpd_port, $mpd_password) = &request_mpd_informations;
- &make_sample_config($twitter_username, $twitter_password, $mpd_host, $mpd_port, $mpd_password);
- print "Configuration finished!\n";
- }
- default {
- print "$choice is not a valid answer.\n"; &sure;
- }
- }
- }
- }
- sub make_sample_config
- {
- my $first_config = {
- 'twitter_username' => $_[0],
- 'twitter_password' => $_[1],
- 'mpd_host' => $_[2],
- 'mpd_port' => $_[3],
- 'mpd_password' => $_[4],
- patterns => [
- '#nowplaying %a - %t (%d)',
- '#nowplaying %a - %t',
- ],
- patterns_comment => [
- '#nowplaying %a - %t (%d) « %c',
- '#nowplaying %a - %t « %c',
- ],
- };
- open my $first_configfile, '>', 'config.yml'
- or die "Can't make config file: $!";
- print $first_configfile Dump($first_config);
- close $first_configfile;
- }
- sub request_twitter_username_and_password
- {
- sub request_twitter_username
- {
- print "Username: ";
- chomp (my $username = <STDIN>);
- &request_username if !$username;
- return $username;
- }
- sub request_twitter_password
- {
- print "Password: ";
- chomp(my $password = <STDIN>);
- &request_password if !$password;
- return $password;
- }
- return (&request_twitter_username, &request_twitter_password);
- }
- sub request_mpd_informations
- {
- sub request_mpd_host
- {
- print "Host [localhost]: ";
- chomp (my $host = <STDIN>);
- return "localhost" if !$host;
- }
- sub request_mpd_port
- {
- print "Port [6600]: ";
- chomp (my $port = <STDIN>);
- return 6600 if !$port;
- }
- sub request_mpd_password
- {
- print "Password:";
- chomp(my $password = <STDIN>);
- return "" if !$password;
- }
- return (&request_mpd_host, &request_mpd_port, &request_mpd_password);
- }
- open my $configfile, '<', &get_config_file
- or &first_run and exit 0;
- my %config = &load_conf($configfile);
- close $configfile;
- my $mpd = Audio::MPD->new(
- host => $config{mpd_host},
- port => $config{mpd_port},
- password => $config{mpd_password},
- );
- my $twitter = Net::Twitter::Lite->new(
- username => $config{twitter_username},
- password => $config{twitter_password},
- );
- my $song = $mpd->current
- or die "MPD is paused/stopped.\n";
- my $artist = $song->artist;
- my $album = $song->album;
- my $title = $song->title;
- my $comment = $ARGV[0] if $ARGV[0];
- sub make_status
- {
- sub substitute
- {
- my $arg = $_[0];
- for ($arg) {
- s/%a/$artist/g;
- s/%d/$album/g;
- s/%t/$title/g;
- if ($comment) {
- s/%c/$comment/g;
- }
- }
- return $arg;
- }
- if ($comment) {
- for my $i (0 .. $#{$config{patterns_comment}}) {
- return &substitute($config{patterns_comment}[$i]) if length &substitute($config{patterns_comment}[$i]) <= 140;
- }
- my $pattern = "$artist - $title « $comment";
- while (length $pattern > 140) {
- $title = substr($title, 0, -1);
- $title += "…";
- $pattern = "#nowplaying $artist - $title « $comment";
- }
- return $pattern;
- } else {
- for my $i (0 .. $#{$config{patterns}}) {
- return &substitute($config{patterns}[$i]) if length &substitute($config{patterns}[$i]) <= 140;
- }
- my $pattern = "$artist - $title";
- while (length $pattern > 140) {
- $title = substr($title, 0, -1);
- $title += "…";
- $pattern = "#nowplaying $artist - $title";
- }
- return $pattern;
- }
- }
- my $status = &make_status;
- print "Tweeting «$status».\n";
- eval { $twitter->update($status) };
- warn "$@\n" if $@;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement