#!/usr/bin/perl
#
# umph
# Copyright (C) 2010-2011  Toni Gundogdu <legatvs@cpan.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

use 5.010001;
use feature 'say';

use warnings;
use strict;

binmode STDOUT, ":utf8";
binmode STDERR, ":utf8";

use version 0.77 (); our $VERSION = version->declare("0.2.0");

use Getopt::ArgvFile(home => 1, startupFilename => [qw(.umphrc)]);
use Getopt::Long qw(:config bundling);
use Carp qw(croak);

exit main();

sub print_version
{
  eval "require Umph::Prompt";
  my $p = $@ ? "" : ", Umph::Prompt version $Umph::Prompt::VERSION";
  say "umph version $VERSION$p
Copyright (C) 2010-2011  Toni Gundogdu
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.";
  exit 0;
}

sub print_help
{
  require Pod::Usage;
  Pod::Usage::pod2usage(-exitstatus => 0, -verbose => 1);
}

use constant MAX_RESULTS_LIMIT => 50;    # Refer to http://is.gd/OcSjwU
my %config;

sub check_max_results_value
{
  if ($config{max_results} > MAX_RESULTS_LIMIT)
  {
    say STDERR
      "WARNING --max-results exceeds max. accepted value, using "
      . MAX_RESULTS_LIMIT
      . " instead";
    $config{max_results} = MAX_RESULTS_LIMIT;
  }
}

sub check_umph_prompt
{
  if ($config{'interactive'} and not eval 'require Umph::Prompt')
  {
    say STDERR
      qq/WARNING Umph::Prompt not found, ignoring --interactive option/;
    $config{interactive} = 0;
  }
}

sub init
{
  GetOptions(
             \%config,
             'type|t=s',
             'start_index|start-index|s=i',
             'max_results|max-results|m=i',
             'interactive|i',
             'all|a',
             'json',
             'csv',
             'proxy=s',
             'no_proxy|no-proxy',
             'quiet|q',
             'version' => \&print_version,
             'help'    => \&print_help,
            ) or exit 1;

  print_help if scalar @ARGV == 0;

  $config{type}        ||= 'p';    # Default to "playlist".
  $config{start_index} ||= 1;      # Default to 1.
  $config{max_results} ||= 25;     # Default 25.

  check_max_results_value;
  check_umph_prompt;
}

sub spew_qe {print STDERR @_ unless $config{quiet}}

my @items;

sub main
{
  init;
  spew_qe "Checking ... ";

  require LWP;
  my $a = new LWP::UserAgent;
  $a->env_proxy;    # http://search.cpan.org/perldoc?LWP::UserAgent
  $a->proxy('http', $config{proxy}) if $config{proxy};
  $a->no_proxy('') if $config{no_proxy};

  require XML::DOM;
  my $p = new XML::DOM::Parser(LWP_UserAgent => $a);
  my $s = $config{start_index};
  my $m = $config{all} ? MAX_RESULTS_LIMIT : $config{max_results};

  while (1)
  {
    my $doc  = $p->parsefile(from_arg($ARGV[0], $s, $m));
    my $root = $doc->getDocumentElement;
    my $n    = 0;
    for my $entry ($root->getElementsByTagName("entry"))
    {
      my $t = to_item($entry, "title")->getFirstChild->getNodeValue;
      my $l =
        to_item($entry, "link")->getAttributeNode("href")->getValue;
      my %data = (title => $t, url => $l, selected => 1);
      push @items, \%data;
      spew_qe((++$n % 5 == 0) ? " " : ".");
      ++$s;
    }
    $doc->dispose;

    last if $n == 0 or not $config{all};
  }

  spew_qe "done.\n";

  croak "error: nothing found\n" if scalar @items == 0;

  open_prompt() if $config{interactive};

  say qq/{\n  "video": [/ if $config{json};

  my $i = 0;

  foreach (@items)
  {
    if ($_->{selected} or not $config{interactive})
    {
      ++$i;

      my $t = $_->{title} || "";
      $t =~ s/"/\\"/g;

      if ($config{json})
      {
        say "," if $i > 1;
        say "    {";
        say qq/      "title": "$t",/;
        say qq/      "url": "$_->{url}"/;
        print "    }";
      }
      elsif ($config{csv}) {say qq/"$t","$_->{url}"/}
      else                 {say "$_->{url}"}
    }
  }

  say "\n  ]\n}" if $config{json};
  0;
}

use constant GURL => "http://gdata.youtube.com/feeds/api";

sub from_arg
{
  my ($arg0, $s, $m) = @_;

  my $u;
  if ($config{type} eq "u" or $config{type} eq "uploads")
  {
    $u = GURL . "/users/$arg0/uploads?v=2";
  }
  elsif ($config{type} eq "f" or $config{type} eq "favorites")
  {
    $u = GURL . "/users/$arg0/favorites?v=2";
  }
  else
  {
    $u = GURL . "/playlists/$arg0?v=2";
  }

  $u .= "&start-index=$s";
  $u .= "&max-results=$m";
}

sub to_item
{
  my ($entry, $name) = @_;
  $entry->getElementsByTagName($name)->item(0);
}

sub open_prompt
{
  my $p = new Umph::Prompt(

    # Commands.
    commands => {
      q => sub {
        my ($p, $args) = @_;
        $p->exit(\@items, $args);
      },
      d => sub {
        my ($p, $args) = @_;
        $p->display(\@items, $args);
      },
      m => sub {
        my ($p, $args) = @_;
        $p->max_shown_items(@{$args});
      },
      s => sub {
        my ($p, $args) = @_;
        $p->select(\@items, $args);
      },
      h => sub {
        my ($p, $args) = @_;
        my @a;
        push @a,
          {cmd => 'normal', desc => 'print results in default format'};
        push @a, {cmd => 'json', desc => 'print results in json'};
        push @a, {cmd => 'csv',  desc => 'print results in csv'};
        $p->help(\@a);
      },
      n => sub {
        $config{json} = 0;
        $config{csv}  = 0;
        say STDERR "=> print in default format";
      },
      j => sub {
        $config{json} = 1;
        $config{csv}  = 0;
        say STDERR "=> print in json";
      },
      c => sub {
        $config{json} = 0;
        $config{csv}  = 1;
        say STDERR "=> print in csv";
      },
    },

    # Callbacks. All of these are optional.
    ontoggle => sub {
      my ($p, $args) = @_;
      $p->toggle(\@items, $args);
    },
    onitems  => sub {return \@items},
    onloaded => sub {
      my ($p, $args) = @_;
      $p->display(\@items, $args);
    },

    # Other (required) settings
    total_items     => scalar @items,
    prompt_msg      => 'umph',
    max_shown_items => 20
  );

  say STDERR qq/Enter prompt. Type "help" to get a list of commands./;
  $p->exec;
}

__END__

=head1 SYNOPSIS

umph [-q] [-i] [-a] [--csv | --json] [-t E<lt>typeE<gt>]
     [--proxy E<lt>addrE<gt> | --no-proxy]
     [E<lt>playlist_idE<gt> | E<lt>usernameE<gt>]

=head2 OPTIONS

     --help                     Print help and exit
     --version                  Print version and exit
 -q, --quiet                    Be quiet
 -i, --interactive              Run in interactive mode
 -t, --type arg (=p)            Get feed type
 -s, --start-index arg (=1)     Index of first matching result
 -m, --max-results arg (=25)    Max number of results included
 -a, --all                      Get all items
     --json                     Print details in JSON
     --csv                      Print details in CSV
     --proxy arg (=http_proxy)  Use proxy for HTTP connections
     --no-proxy                 Disable use of HTTP proxy

=cut

# vim: set ts=2 sw=2 tw=72 expandtab:
