Skip to content

Instantly share code, notes, and snippets.

@rezzafr33
Created September 19, 2015 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rezzafr33/97cf45f068c53cf49540 to your computer and use it in GitHub Desktop.
Save rezzafr33/97cf45f068c53cf49540 to your computer and use it in GitHub Desktop.
Tutplus Video Downloader
#!/usr/bin/perl
use strict;
use warnings;
use WWW::Mechanize;
use Getopt::Std;
use JSON;
require File::Temp;
use File::Temp ();
#
## Argiacyber.com ,:, Download Tutplus video using youtube-dl
## Script Name : tutplus.pl
## Version : 0.1
## Valid from : September 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 Tutplus URL to video page
## $2 prefix to the file name of the video (optional)
#
############ options ##########################################
# Arguments
my %options=();
getopts("e:p:c:", \%options);
# Predefined variable
my $DEBUG=1;
my $fileType = "mp4";
my $prefix = "";
my $user_prefix = "";
my $retry = 5;
my $user_url = "";
my $download = "";
# Cookie File
my $cookies = File::Temp->new();
my $cookie_file = (defined $options{c}) ? $options{c} : $cookies->filename;
# 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" );
}
my $mech = WWW::Mechanize->new( autocheck => 1 );
if (! -s $cookie_file){
$mech->get( 'https://tutsplus.com/sign_in' );
$mech->form_number(3);
$mech->field('session[login]', $options{e});
$mech->field('session[password]', $options{p});
$mech->click();
$mech->cookie_jar->save($cookie_file);
$mech->get($url);
}
else {
$mech->cookie_jar->load($cookie_file);
$mech->get($url);
}
my $output = $mech->content();
my ($video_id) = $output =~ m{data-wistia-id=["']?((?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?}ig or die "\n Error: no Video ID Found\n\n";
# Filename
my ($title) = $output =~ m/<title>(.+)<\/title>/si;
$title =~ s/[^\w\d]+/_/g;
$title = lc ($title);
$title =~ s/^_//ig;
my $filename = "$prefix$title.$fileType";
# Get Json response from wistia
$mech->add_header( Referer => $url );
$mech->get('https://fast.wistia.com/embed/medias/' . $video_id . '.json');
$output = $mech->content();
my $json = new JSON;
my $json_text = $json->allow_nonref->utf8->relaxed->decode($output);
$download = $json_text->{'media'}->{'assets'}->{'mdmp4'}->{'url'};
if($download eq "") {
$download = $json_text->{'media'}->{'assets'}->{'original'}->{'url'};
}
print "\n The following url will be passed to youtube-dl: $download\n" if ($DEBUG == 1);
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 #####
@bhill77
Copy link

bhill77 commented Sep 19, 2015

how to use it?

@rezzafr33
Copy link
Author

similar to laracast.pl.. 👍

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