#!/usr/bin/perl

use strict;
use warnings;
use WWW::Mechanize ();
use WWW::Scripter;
use URI::Find;
 use Gtk2::Notify -init, 'Basic';
# use diagnostics -verbose;

my $odtwarzacz="/usr/bin/vlc -A alsa --alsa-audio-device vlc --quiet ";
 my $mpvplayer="ALSAOUT=\"vlc\" /usr/bin/alltray  2>&1>/dev/null  /usr/bin/mpv";
my $smplayer="ALSAOUT=\"vlc\" /usr/bin/smplayer";

### my $smplayer="/usr/bin/smplayer";
# my $odtwarzacz="/usr/bin/smplayer";
# my $odtwarzacz="/usr/bin/alltray 2>&1>/dev/null  /usr/bin/totem";

# my $drugiodtwarzacz="/usr/bin/alltray 2>&1>/dev/null  /usr/bin/totem";
my $drugiodtwarzacz="$odtwarzacz";
# my $minitube="$odtwarzacz";
### my $minitube="$smplayer";
my $minitube="$mpvplayer";
my $vimeoplayer="$mpvplayer";
my $sznurekerror="";
my $terazgram="";
my $useragent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:54.0) Gecko/20100101 Firefox/54.0";

sub clipboard {
use Gtk2 '-init';

my $clipboard = Gtk2::Clipboard->get( Gtk2::Gdk->SELECTION_CLIPBOARD );

my $clip = $clipboard->wait_for_text;

if ($clip =~ /(\s)?http?(s?)\:\/\/(\.www\.)?(.*)$/)
{
	print "CLipboard zwrócił:  $clip\n";
	robota($clip);
	}
else 
{   
    my $sznurekerror = Gtk2::Notify->new("RssPerl!\nNie wiem, co to za sznurek!", "$clip");
    $sznurekerror ->show;
    
   	print "Nie wiem, co to za sznurek :  $clip\n\n";

	};	
};



sub youtube {	
	my $goto = $_[0] ;
	print "Adres oryginalny  to $goto\n";
	my $sznurek="";
     if ($goto =~ /(?<=\Wv\/)([\w|-]+)/)
	    {   
            my  $sznurek =$1;
            my $adresik = "https://www.youtube-nocookie.com/embed/$sznurek";
			my @minitube = ("$minitube  $adresik 2>&1>/dev/null &" );
		#	my $terazgram = Gtk2::Notify->new("RssPerl!\nTeraz gramy\:\n $goto\n");
        #   $terazgram ->show;
			print "$minitube otworzy $adresik\n\n";
			exec @minitube   or print STDERR "couldn't exec $minitube: $!";
			
         }  

     elsif ($goto =~ /(?<=\Wv=)([\w|-]+)/)
        {
			my $videoid = "$1" if $1;
			my $adres = "https://www.youtube-nocookie.com/embed/$videoid" ;
		    print "Otwieram $adres\n";
			my @minitube = ("$minitube  $adres 2>&1>/dev/null &" );
     	    print "$minitube otworzy $adres\n";
     	 #  my $terazgram = Gtk2::Notify->new("RssPerl!\nTeraz gramy\:\n $goto\n");
         #  $terazgram ->show;
     	    exec @minitube   or print STDERR "couldn't exec $minitube : $!";
         }
         
     elsif ($goto =~ /youtu.be\/([\w|-]+)/)
        {
			my $videoid = "$1" if $1;
			my $adres = "https://www.youtube-nocookie.com/embed/$videoid" ;
		    print "Otwieram $adres\n";
			my @minitube = ("$minitube  $adres 2>&1>/dev/null &" );
     	    print "$minitube otworzy $adres\n";
     	  # my $terazgram = Gtk2::Notify->new("RssPerl!\nTeraz gramy\:\n $goto\n");
          # $terazgram ->show;
            exec @minitube   or print STDERR "couldn't exec $minitube : $!";

         }
     elsif ($goto =~ /youtube-nocookie.com\/([\w|-]+)/)
        {
			my $adres = "$goto" ;
		    print "Otwieram $adres\n";
			my @minitube = ("$minitube  $adres 2>&1>/dev/null &" );
     	    print "$minitube otworzy $adres\n";
     	  # my $terazgram = Gtk2::Notify->new("RssPerl!\nTeraz gramy\:\n $goto\n");
          # $terazgram ->show;
            exec @minitube   or print STDERR "couldn't exec $minitube : $!";

         }
      elsif ($goto =~ /youtube.com\/embed\/([\w|-]+)/)
        {
			my $adres = "$goto" ;
		    print "Otwieram $adres\n";
			my @minitube = ("$minitube  $adres 2>&1>/dev/null &" );
     	    print "$minitube otworzy $adres\n";
     	  # my $terazgram = Gtk2::Notify->new("RssPerl!\nTeraz gramy\:\n $goto\n");
          # $terazgram ->show;
            exec @minitube   or print STDERR "couldn't exec $minitube : $!";

         }
      else 
         {
            firefox($goto);	 
		 }   
}

sub gazetapolska {

    	my $goto = $_[0] ;

        my $sznurek = WWW::Mechanize->new();
	       $sznurek ->agent( "$useragent" );
           $sznurek->max_redirect(0);
           $sznurek->default_header('Accept' => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 
               'Accept-Language' => " pl-PL,pl;q=0.5",
               'Accept-Charset' => "ISO-8859-2,utf-8;q=0.7,*;q=0.7",
               'Accept-Encoding' => "gzip,deflate",
               'Connection' => "keep-alive", );
				$sznurek->get( $goto );
	   	my $filmek = $sznurek->content();
            
    if ($filmek =~/(http:\/\/.*\.mp4)/) {
			my $adres=$1;
			print "$adres\n";
            my @odtwarzacz = ("$mpvplayer $adres 2>&1>/dev/null &" );
			print "$mpvplayer otworzy $adres\n\n" if $adres;
	     	exec @odtwarzacz   or print STDERR "couldn't exec $mpvplayer : $!";
		    # firefox($adres)
		} 
    
		elsif ( $filmek  =~ m{youtube.com/embed/(.+)\?}i ) 

		{
		#	print $filmek;
			my $film = $1 if $1;
			my $adres = "http://youtube.com/watch?v=$film";
			print "Film to: $adres\n\n" if $1;
			youtube($adres);
     	}
       
        else 
        {
        print "GazPol nie udało się";
			 firefox($goto)	
	
		}
 }

	
sub dailymotion {
		my $goto = $_[0] ;
		my @vlcstart = ("$mpvplayer $goto &");
        exec @vlcstart   or print STDERR "couldn't exec  $mpvplayer: $!";

}

sub vimeo {
	my $goto = $_[0] ;
	my @playerstart = ("$vimeoplayer $goto &");
	exec @playerstart   or print STDERR "couldn't exec  $vimeoplayer: $!";
	}

sub vimeoold {
	
	my  @opcjejakosci = ("hd" , "sd" , "mobile");

	my $goto = $_[0] ;
	
	my $sygnatura="";
	my $playerurl="";
	my $timestamp="";
	my $videoid="";
	my $vimqualities="";
	my $jakosc="";
	my $sznur="";
	my $adres="";
	

        my $sznurek = WWW::Mechanize->new();
	       $sznurek ->agent( "$useragent" );
           $sznurek->max_redirect(1);
           $sznurek->default_header('Accept' => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 
               'Accept-Language' => " pl-PL,pl;q=0.5",
               'Accept-Charset' => "ISO-8859-2,utf-8;q=0.7,*;q=0.7",
               'Accept-Encoding' => "gzip,deflate",
               'Connection' => "keep-alive", );
				$sznurek->get( $goto );
	   	my $vimtest = $sznurek->content();
        
        if ($vimtest =~m/\"signature":\"(.+?)\"/) {
	 		 $sygnatura = $1; }

        if ($vimtest =~m/\"player_url":\"(.+?)\"/) {
		     $playerurl=$1;  }
 

		if ($vimtest =~m/"timestamp":(\d+)/) {
			 $timestamp=$1; }

		if ($vimtest =~m/"video":\{"id":(\d+?),/) {
			 $videoid=$1; }

		if ($vimtest =~m/"qualities":\[(.*?)\]/) {
		     $vimqualities=$1; }

	
	  foreach  my $vimq (@opcjejakosci) {
       if ($vimqualities =~/$vimq/ ){
	   $jakosc = $vimq;
	   last; 
	   }
     }
			
	  $sznur = "http://$playerurl/play_redirect?quality=$jakosc&clip_id=$videoid&time=$timestamp&sig=$sygnatura";

        my $film =  WWW::Scripter->new(
				autocheck => 1,
 				show_progress => 1,);
  
    $film ->use_plugin(JavaScript => engine => "JE",);
    $film ->agent( "$useragent" );
    $film->max_redirect(0);

     $film->get($sznur);
        my $status = $film->status();
        if (($status >= 300) && ($status < 400)) {
        $adres = $film->response()->header('Location');
	}
   	my @vlcstart = ("$odtwarzacz  \"$adres\" 2>&1>/dev/null &" );
	exec @vlcstart   or print STDERR "couldn't exec $odtwarzacz: $!";
  
}

sub firefox {
		my $goto = $_[0] ;
		print "Adres oryginalny  to $goto\n";
	    # my @ffstart = ("/usr/bin/firefox -P duch --new-tab  \"$goto\"  2>&1>/dev/null &");
	  #  my @ffstart = ("/usr/bin/alltray /usr/bin/google-chrome-unstable \"$goto\"  2>&1>/dev/null &");
	    my @ffstart = ("/usr/local/bin/firefox16  \"$goto\"  2>&1>/dev/null &");

		exec @ffstart   or print STDERR "couldn't exec firefox: $!";
}
sub mpv {
		my $goto = $_[0] ;
		print "Adres oryginalny  to $goto\n";
	    # my @ffstart = ("/usr/bin/firefox -P duch --new-tab  \"$goto\"  2>&1>/dev/null &");
	  #  my @ffstart = ("/usr/bin/alltray /usr/bin/google-chrome-unstable \"$goto\"  2>&1>/dev/null &");
	    my @mpv = ( "$mpvplayer  \"$goto\"  2>&1>/dev/null &");

		exec @mpv   or print STDERR "couldn't exec \"$mpvplayer\": $!";
}
sub periscope {
		my $goto = $_[0] ;
		print "Adres oryginalny  to $goto\n";
	    # my @ffstart = ("/usr/bin/firefox -P duch --new-tab  \"$goto\"  2>&1>/dev/null &");
	  #  my @ffstart = ("/usr/bin/alltray /usr/bin/google-chrome-unstable \"$goto\"  2>&1>/dev/null &");
	    my @periscope = ( "$mpvplayer  \"$goto\"  2>&1>/dev/null &");

		exec @periscope   or print STDERR "couldn't exec \"$mpvplayer\": $!";
}
sub vodtvpget
       {
	    my $videoidget = $_[0] ;
#	    print "VodtvpGet - videoid = $videoidget\n";
	    my $tvpgoto="http://www.tvp.pl/pub/stat/videofileinfo?video_id=$videoidget";		
    	my $vodtvp = WWW::Mechanize->new();
	        $vodtvp ->agent( "$useragent" );
            $vodtvp->max_redirect(0);
            $vodtvp->default_header('Accept' => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 
               'Accept-Language' => " pl-PL,pl;q=0.5",
               'Accept-Charset' => "ISO-8859-2,utf-8;q=0.7,*;q=0.7",
               'Accept-Encoding' => "gzip,deflate",
               'Connection' => "keep-alive", );
				$vodtvp->get( $tvpgoto );
		my $tvpfilm = $vodtvp->content();
#        print "$tvpfilm";
        if ($tvpfilm  =~m/\"video_url"\s:\s\"(http:\/\/.+?)\"/)
        { my $tvpadres="$1";
              my @vlcstart = ("$odtwarzacz $tvpadres 2>&1>/dev/null &" );
               print "Adres filmu to:  $tvpadres\n\n";
              exec @vlcstart   or print STDERR "couldn't exec $odtwarzacz: $!";
        
			
			}
} 


sub vodtvp {
		my $goto = $_[0] ;
		print "Adres oryginalny  to $goto\n";
	    # my @ffstart = ("/usr/bin/firefox -P duch --new-tab  \"$goto\"  2>&1>/dev/null &");
	  #  my @ffstart = ("/usr/bin/alltray /usr/bin/google-chrome-unstable \"$goto\"  2>&1>/dev/null &");
	    my @vodtvp = ( "$mpvplayer  \"$goto\"  2>&1>/dev/null &");

		exec @vodtvp   or print STDERR "couldn't exec \"$mpvplayer\": $!";



};

if (@ARGV) {
    print "Zabieram się do roboty ;)\n\n";
} 
else 
{
	 print "Nie mam sznurka do otwarcia! ;(\n"; exit 1; 
	 }

my $argument = $ARGV[0];
chomp $argument;


if ($argument =~/^clip$/)
    {
	#	print "odpalam clipboard\n";
		clipboard();	 
     }
     
elsif ($argument =~ /^http(s?)\:\/\/(\.www\.)?(.*)$/)
	{
	#	print "$argument\n\n";
		robota($argument)
	 }

else
    {
		
    	print "Dziwny ten sznurek\n";	
		robota($argument);
	print "Nie wiem, co to za sznurek:  \"$argument\"\n";
	my $sznurekerror = Gtk2::Notify->new("RssPerl!\nNie wiem, co to za sznurek!", "$argument");
    $sznurekerror ->show;
	exit 0;
}

sub robota {
	    #print "zaczynam robotę\n";
	    my $link = $_[0];
	 #   print " Otwieram $link\n";
	    
if ($link=~/youtube-nocookie.com/)
	{
		 youtube("$link")
	 }
	 
elsif ($link=~/youtu.be/)
	{
		 youtube("$link")
	 }	 
elsif ($link=~/youtube/)
	{
		 youtube("$link")
	 }		 
	 
	 
elsif ($link=~/tvp\.pl/)
	{
		 vodtvp("$link")
	}
		 
	 
elsif ($link=~/vimeo/)
	{
# print "Vimeo chwilowo nieczynne do odwołania!!!!\n ;P\n";
		 vimeo("$link")
	}
	
elsif ($link=~/gazetapolska/)
	{
		 gazetapolska("$link")
	}
	elsif ($link=~/periscope.tv/)
	{
		 periscope("$link")
	}
elsif ( $link =~ m/^http(s?):\/\/.*?dailymotion.com\/video\/\.*?/ ) 
	{
		 dailymotion("$link")	
	}
elsif ( $link =~ m/^http(s?):\/\/.*?cda.pl\/video\/\.*?/ ) 
	{
		 mpv("$link")	
	}
elsif ( $link =~ m/^http(s?):\/\/.*?trt.pl\/film\/\.*?/ ) 
	{
		 mpv("$link")	
	}
elsif ( $link =~ m/^http(s?)\:\/\/(\.www\.)?(.*)$/) 
	{
		 firefox("$link")	
	}	

else
	{
	 print "Nie wiem, co to jest $link\n";
    }
};
