Skip to content

Instantly share code, notes, and snippets.

@rezzafr33
Last active February 19, 2018 08:18
Show Gist options
  • Save rezzafr33/71f8b2d8aa31748f3629 to your computer and use it in GitHub Desktop.
Save rezzafr33/71f8b2d8aa31748f3629 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use WWW::Mechanize;
use Getopt::Std;
require File::Temp;
use File::Temp ();
#
## Argiacyber.com ,:, Download Laracasts video using youtube-dl
## Script Name : laracast.pl
## Version : 0.45
## Valid from : March 2015
## Forked from : https://calomel.org/youtube_wget.html
## OS Support : Linux, Mac OSX, OpenBSD, FreeBSD or any system with perl
# `:`
## Arguments:
## -c cookies file (optional)
## -e email (use with -e)
## -p password (use with -p)
## Positional arguments:
## $1 Laracasts URL to video page
## $2 prefix to the file name of the video (optional)
#
############ options ##########################################
my %options=();
getopts("e:p:c:", \%options);
my $DEBUG=1;
my $fileType = "mp4";
my $prefix = "";
my $user_prefix = "";
my $retry = 5;
my $mech = WWW::Mechanize->new('Timeout' => '30');
my $cookies = File::Temp->new();
my $cookie_file = (defined $options{c}) ? $options{c} : $cookies->filename;
my $user_url = "";
# collect the URL from the command line argument
chomp($user_url = $ARGV[0]);
my $url = "$1" if ($user_url =~ m/^([a-zA-Z0-9\_\-\&\?\=\:\.\/]+)$/ or die "\nError: Illegal characters in URL\n\n" );
# declare the user defined file name prefix if specified
if (defined($ARGV[1])) {
chomp($user_prefix = $ARGV[1]);
$prefix = "$1" if ($user_prefix =~ m/^([a-zA-Z0-9\_\-\.\ ]+)$/ or die "\nError: Illegal characters in filename prefix\n\n" );
}
if (! -s $cookie_file){
$mech->get("https://laracasts.com/login");
# $mech->follow_link(text => 'Log In', n => 1);
$mech->form_number(1);
$mech->field('email', $options{e});
$mech->field('password', $options{p});
$mech->click();
$mech->cookie_jar->save($cookie_file);
$mech->get($url);
}
else {
$mech->cookie_jar->load($cookie_file);
$mech->get($url);
}
# Print to file
my $output = $mech->content();
my ($download) = $output =~ m{(?:src)="(.*?hd\.mp4.*?)"}ig or die "\n Error: no Video URL Found\n\n";
$download =~ s/\:\ \"//;
$download =~ s/\;\ \"//;
$download =~ s/https\://;
$download = "https:" . $download;
# Print the long, sanitized laracasts url for testing and debugging
print "\n The following url will be passed to youtube-dl: $download\n" if ($DEBUG == 1);
# format the title of the page to use as the file name
my ($title) = $output =~ m/<title>(.+)<\/title>/si;
$title =~ s/[^\w\d]+/_/g;
$title = lc ($title);
$title =~ s/^_//ig;
my $filename = "$prefix$title.$fileType";
# print the file name of the video being downloaded for the user
print "\n Download: $filename\n\n";
# Download the video
system("youtube-dl", "--retries", "$retry", "--continue", "$download", "--output", "$filename");
if ($? > 0) {
# Print the error code of youtube-dl
print "\n youtube-dl error code: $?\n" if ($DEBUG == 1);
}
if( $? == 0 && -e "$filename" && ! -z "$filename" )
{
print "\n Finished: $filename\n\n" if ($DEBUG == 1);
}
else
{
print STDERR "\n FAILED: $filename\n\n" if ($DEBUG == 1);
}
#### EOF #####
@thomasgroch
Copy link

still works?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment