Skip to content

Instantly share code, notes, and snippets.

@jesboat
Created July 10, 2013 17:31
Show Gist options
  • Save jesboat/5968325 to your computer and use it in GitHub Desktop.
Save jesboat/5968325 to your computer and use it in GitHub Desktop.
Script to upload cs173 lecture videos (as seen on http://cs.brown.edu/courses/cs173/2012/Videos/) to YouTube.
$USER = 'foo@gmail.com';
$PASS = 'abc123';
#!/usr/bin/perl
use warnings;
use strict;
use FindBin qw($RealBin);
use Getopt::Long qw(GetOptionsFromArray);
our $AUTHFILE = "$RealBin/private/youtube-account.pl";
our $CATEGORY = "Education";
our $TITLE_TEMPLATE = "Programming Languages - Lecture %s";
our ($USER, $PASS);
require $AUTHFILE;
my $no_act;
sub file_to_date {
my ($file) = @_;
# Format: YYYY-MM-DD.series-of-extensions
if ($file =~ m{^(20\d\d-\d\d-\d\d)\.[\w.]+(?<![.])$}) {
return $1;
} else {
die "Couldn't figure out date from filename '$file'\n";
}
}
sub make_command {
my ($file, $date) = @_;
my $title = (sprintf $TITLE_TEMPLATE, $date);
my @command = (
"youtube-upload",
"--email=$USER", "--password=-",
"--category=$CATEGORY",
"--title=$title",
$file);
\@command;
}
sub run {
my ($cmd) = @_;
print "Running command {@$cmd}\n";
if ($no_act) {
print "(Skipping; --no-act was specified.)\n";
return;
}
my $kid_pid = (open my($to_kid), "|-", @$cmd)
or die "fork+exec: $!\n";
print {$to_kid} "$PASS\n"
or die "child: write: $!\n";
close $to_kid
or die ($! ? "child: close: $!\n"
: "child: failed with wait status $?\n");
}
sub upload_file {
my ($file) = @_;
my $date = file_to_date($file);
my $cmd = make_command($file, $date);
run($cmd);
}
sub main {
GetOptionsFromArray(\@_,
'n|no-act' => \$no_act)
and @_ == 1
or die "Usage: (example) $0 2012-09-14.high.m4v\n";
my ($file) = @_;
(-f $file)
or die "$file: $!\n";
upload_file($file);
}
main(@ARGV);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment