| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046 |
- #!/usr/bin/perl
- use strict;
- use warnings;
- use FindBin;
- use lib "$FindBin::Bin/";
- use File::Touch;
- use File::Basename;
- use Date::Parse;
- use Date::Format;
- use LWP::Simple;
- use URI::Escape;
- use JSON;
- use XML::LibXML;
- use IPC::System::Simple qw( system run capture EXIT_ANY $EXITVAL );
- use IPC::Cmd qw( can_run );
- use PrettyPrint;
- # Paramters
- my %USERS = ('profplump' => 'kj-Ob6eYHvzo-P0UWfnQzA', 'shanda' => 'hfwMHzkPXOOFDce5hyQkTA');
- my $EXTRAS_FILE = 'extra_videos.ini';
- my $EXCLUDES_FILE = 'exclude_videos.ini';
- my $YTDL_BIN = $ENV{'HOME'} . '/bin/video/yt/youtube-dl';
- my @YTDL_ARGS = ('--force-ipv4', '--socket-timeout', '10', '--no-playlist', '--max-downloads', '1', '--age-limit', '99');
- my @YTDL_QUIET = ('--quiet', '--no-warnings');
- my @YTDL_DEBUG = ('--verbose');
- my $BATCH_SIZE = 50;
- my $MAX_INDEX = 25000;
- my $FETCH_LIMIT = 50;
- my $DRIFT_TOLERANCE = 2;
- my $DRIFT_FACTOR = 100.0;
- my $DELAY = 5;
- my $API_URL = 'https://gdata.youtube.com/feeds/api/';
- my %API = (
- 'search' => {
- 'prefix' => $API_URL . 'users/',
- 'suffix' => '/uploads',
- 'params' => {
- 'start-index' => 1,
- 'max-results' => 1,
- 'strict' => 1,
- 'v' => 2,
- 'alt' => 'jsonc',
- },
- },
- 'subscriptions' => {
- 'prefix' => $API_URL . 'users/',
- 'suffix' => '/subscriptions',
- 'params' => {
- 'start-index' => 1,
- 'max-results' => 1,
- 'strict' => 1,
- 'v' => 2,
- 'alt' => 'json',
- },
- },
- 'video' => {
- 'prefix' => $API_URL . 'videos/',
- 'suffix' => '',
- 'params' => {
- 'strict' => 1,
- 'v' => 2,
- 'alt' => 'jsonc'
- },
- },
- 'channel' => {
- 'prefix' => $API_URL . 'users/',
- 'suffix' => '',
- 'params' => {
- 'strict' => 1,
- 'v' => 2,
- 'alt' => 'json'
- },
- },
- );
- # Prototypes
- sub findVideos($);
- sub findFiles();
- sub findVideo($);
- sub buildNFO($);
- sub buildSeriesNFO($);
- sub getSubscriptions($$);
- sub saveSubscriptions($$);
- sub saveChannel($);
- sub getChannel($);
- sub fetchParse($$);
- sub saveString($$);
- sub readExcludes();
- sub readExtras();
- sub parseVideoData($);
- sub dropExcludes($);
- sub addExtras($);
- sub updateNFOData($$$);
- sub videoSE($$);
- sub videoPath($$$$);
- sub renameVideo($$$$$$);
- sub parseFilename($);
- # Sanity check
- if (scalar(@ARGV) < 1) {
- die('Usage: ' . basename($0) . " output_directory\n");
- }
- # Command-line parameters
- my ($dir) = @ARGV;
- $dir =~ s/\/+$//;
- if (!-d $dir) {
- die('Invalid output directory: ' . $dir . "\n");
- }
- my $user = basename($dir);
- if (length($user) < 1 || !($user =~ /^\w+$/)) {
- die('Invalid user: ' . $user . "\n");
- }
- # Move to the target directory so we can use relative paths later
- chdir($dir);
- # Environmental parameters (debug)
- my $DEBUG = 0;
- if ($ENV{'DEBUG'}) {
- if ($ENV{'DEBUG'} =~ /(\d+)/) {
- $DEBUG = $1;
- } else {
- $DEBUG = 1;
- }
- }
- my $NO_FETCH = 0;
- if ($ENV{'NO_FETCH'}) {
- $NO_FETCH = 1;
- }
- my $NO_NFO = 0;
- if ($ENV{'NO_NFO'}) {
- $NO_NFO = 1;
- }
- my $NO_SEARCH = 0;
- if ($ENV{'NO_SEARCH'}) {
- $NO_SEARCH = 1;
- }
- my $NO_FILES = 0;
- if ($ENV{'NO_FILES'}) {
- $NO_FILES = 1;
- }
- my $NO_CHANNEL = 0;
- if ($ENV{'NO_CHANNEL'}) {
- $NO_CHANNEL = 1;
- }
- my $NO_EXTRAS = 0;
- if ($ENV{'NO_EXTRAS'}) {
- $NO_EXTRAS = 1;
- }
- my $NO_EXCLUDES = 0;
- if ($ENV{'NO_EXCLUDES'}) {
- $NO_EXCLUDES = 1;
- }
- my $NO_RENAME = 0;
- if ($ENV{'NO_RENAME'}) {
- $NO_RENAME = 1;
- }
- # Environmental parameters (functional)
- my $FORCE_RENAME = 0;
- if ($ENV{'FORCE_RENAME'}) {
- $FORCE_RENAME = 1;
- }
- my $SUDO_CHATTR = 1;
- if ($ENV{'NO_CHATTR'} || !can_run('sudo') || !can_run('chattr')) {
- $SUDO_CHATTR = 0;
- }
- if ($ENV{'MAX_INDEX'} && $ENV{'MAX_INDEX'} =~ /(\d+)/) {
- $MAX_INDEX = $1;
- }
- if ($ENV{'BATCH_SIZE'} && $ENV{'BATCH_SIZE'} =~ /(\d+)/) {
- $BATCH_SIZE = $1;
- }
- if (exists($ENV{'FETCH_LIMIT'}) && $ENV{'FETCH_LIMIT'} =~ /(\d+)/) {
- $FETCH_LIMIT = $1;
- }
- # Construct globals
- foreach my $key (keys(%API)) {
- if (exists($API{$key}{'params'}{'max-results'})) {
- $API{$key}{'params'}{'max-results'} = $BATCH_SIZE;
- }
- }
- if ($DEBUG > 1) {
- push(@YTDL_ARGS, @YTDL_DEBUG);
- } elsif (!$DEBUG) {
- push(@YTDL_ARGS, @YTDL_QUIET);
- }
- # Allow use as a subscription manager
- if ($0 =~ /subscription/i) {
- my %subs = ();
- foreach my $user (keys(%USERS)) {
- my $tmp = getSubscriptions($user, $USERS{$user});
- foreach my $sub (keys(%{$tmp})) {
- if (exists($subs{$sub})) {
- $subs{$sub} .= ', ' . $tmp->{$sub};
- } else {
- $subs{$sub} = $tmp->{$sub};
- }
- }
- }
- saveSubscriptions($dir, \%subs);
- exit(0);
- }
- # Grab the channel data
- my $channel = {};
- if (!$NO_CHANNEL) {
- $channel = getChannel($user);
- saveChannel($channel);
- }
- # Find all the user's videos on YT
- my $videos = {};
- if (!$NO_SEARCH) {
- $videos = findVideos($user);
- }
- # Find any requested "extra" videos
- my $extras = {};
- if (!$NO_EXTRAS) {
- $extras = addExtras($videos);
- }
- # Drop any "excludes" videos
- my $excludes = {};
- if (!$NO_EXCLUDES) {
- $excludes = dropExcludes($videos);
- }
- # Calculate the episode number using the publish dates
- {
- my @byDate = sort { $videos->{$a}->{'date'} <=> $videos->{$b}->{'date'} || $a cmp $b } keys %{$videos};
- my $num = undef();
- my $lastSeason = undef();
- foreach my $id (@byDate) {
- if (!$lastSeason || $lastSeason != $videos->{$id}->{'season'}) {
- $num = 1;
- $lastSeason = $videos->{$id}->{'season'};
- }
- $videos->{$id}->{'number'} = $num++;
- }
- }
- # Find all existing YT files on disk
- my $files = {};
- if (!$NO_FILES) {
- $files = findFiles();
- }
- # Whine about unknown videos
- foreach my $id (keys(%{$files})) {
- if (!exists($videos->{$id}) && $files->{$id}->{'season'} > 0) {
- print STDERR 'Local video not known to YT channel (' . $user . '): ' . $id . "\n";
- renameVideo($files->{$id}->{'path'}, $files->{$id}->{'suffix'}, $files->{$id}->{'nfo'}, $id, 0, $files->{$id}->{'season'} . $files->{$id}->{'number'});
- }
- }
- # Fill in missing videos and NFOs
- my $fetched = 0;
- foreach my $id (keys(%{$videos})) {
- my $nfo = videoPath($videos->{$id}->{'season'}, $videos->{$id}->{'number'}, $id, 'nfo');
- # Skip remote-only videos
- if (!exists($files->{$id})) {
- next;
- }
- # Determine if we may or must rename
- my $rename = 0;
- my ($season, $number) = parseFilename($files->{$id}->{'nfo'});
- if ($files->{$id}->{'season'} != $season || $files->{$id}->{'number'} != $number) {
- $rename = -1;
- } elsif ($files->{$id}->{'number'} != $videos->{$id}->{'number'}
- || $files->{$id}->{'season'} != $videos->{$id}->{'season'})
- {
- $rename = 1;
- }
- # Warn (and optionally rename) if the video numbers drift
- if ($rename != 0) {
- if ($rename < 0 || $FORCE_RENAME) {
- renameVideo($files->{$id}->{'path'}, $files->{$id}->{'suffix'}, $files->{$id}->{'nfo'}, $id, $videos->{$id}->{'season'}, $videos->{$id}->{'number'});
- } else {
- # Find the old NFO to avoid re-fetching
- $nfo = $files->{$id}->{'nfo'};
- # Calculate the drift magnitude
- my $delta = abs($files->{$id}->{'number'} - $videos->{$id}->{'number'});
- my $tolerance = $files->{$id}->{'number'} / $DRIFT_FACTOR;
- if ($tolerance < $DRIFT_TOLERANCE) {
- $tolerance = $DRIFT_TOLERANCE;
- }
- # Ignore small changes
- my $rename = 0;
- if ($FORCE_RENAME || $delta > $tolerance) {
- $rename = 1;
- }
- # Execute
- if ($DEBUG || $rename) {
- print STDERR 'Video ' . $id . ' had video number ' . $files->{$id}->{'number'} . ' but now has video number ' . $videos->{$id}->{'number'} . "\n";
- if ($rename) {
- renameVideo($files->{$id}->{'path'}, $files->{$id}->{'suffix'}, $files->{$id}->{'nfo'}, $id, $videos->{$id}->{'season'}, $videos->{$id}->{'number'});
- }
- }
- }
- }
- # If we haven't heard of the file, or don't have an NFO for it
- # Checking for the NFO allows use to resume failed downloads
- if (!exists($files->{$id}) || !-e $nfo) {
- if ($DEBUG) {
- print STDERR 'Fetching video: ' . $id . "\n";
- }
- # Let youtube-dl handle the URLs and downloading
- {
- my @args = ('--output', videoSE($videos->{$id}->{'season'}, $videos->{$id}->{'number'}) . '%(id)s.%(ext)s', '--', $id);
- my @name = ($YTDL_BIN);
- push(@name, @YTDL_ARGS);
- my @fetch = @name;
- push(@name, '--get-filename');
- push(@fetch, @args);
- push(@name, @args);
- if ($NO_FETCH) {
- print STDERR 'Not running: ' . join(' ', @fetch) . "\n";
- } else {
- # Count fetch attempts (even if they fail later)
- $fetched++;
- if ($FETCH_LIMIT && $fetched > $FETCH_LIMIT) {
- print STDERR 'Reached fetch limit (' . $FETCH_LIMIT . ') for: ' . $user . "\n";
- if ($DEBUG) {
- print STDERR "\tLocal/Remote videos at start:" . scalar(keys(%{$files})) . '/' . scalar(keys(%{$videos})) . "\n";
- }
- exit 1;
- }
- # Find the output file name
- if ($DEBUG > 1) {
- print STDERR join(' ', @name) . "\n";
- }
- sleep($DELAY);
- my $file = capture(EXIT_ANY, @name);
- if ($EXITVAL != 0) {
- warn('Error executing youtube-dl (name): ' . $EXITVAL . "\n");
- next;
- }
- $file =~ s/^\s+//;
- $file =~ s/\s+$//;
- # Sanity check
- if (!$file) {
- warn('No file name available for video: ' . $id . "\n");
- next;
- }
- # Download
- if ($DEBUG > 1) {
- print STDERR join(' ', @fetch) . "\n";
- }
- sleep($DELAY);
- my $exit = run(EXIT_ANY, @fetch);
- if ($exit != 0) {
- warn('Error executing youtube-dl for video: ' . $file . "\n");
- next;
- }
- # Ensure we found something useful
- if (-e $file . '.part') {
- warn('Partial download detected: ' . $file . "\n");
- next;
- }
- if (!-s $file) {
- warn('No output video file: ' . $file . "\n");
- next;
- }
- # Touch the file to reflect the download time rather than the upload time
- touch($file);
- }
- }
- # Build and save the XML document
- my $xml = buildNFO($videos->{$id});
- if ($DEBUG > 1) {
- print STDERR 'Saving NFO: ' . $xml . "\n";
- }
- if ($NO_NFO) {
- print STDERR "Not saving NFO\n";
- } else {
- saveString($nfo, $xml);
- }
- }
- }
- sub saveString($$) {
- my ($path, $str) = @_;
- my $fh = undef();
- if (!open($fh, '>', $path)) {
- warn('Cannot open file for writing: ' . $path . ': ' . $! . "\n");
- return undef();
- }
- print $fh $str;
- close($fh);
- return 1;
- }
- sub buildSeriesNFO($) {
- my ($channel) = @_;
- # Create an XML tree
- my $doc = XML::LibXML::Document->new('1.0', 'UTF-8');
- $doc->setStandalone(1);
- my $show = $doc->createElement('tvshow');
- $doc->setDocumentElement($show);
- my $elm;
- # Add data
- $elm = $doc->createElement('title');
- $elm->appendText($channel->{'title'});
- $show->appendChild($elm);
- $elm = $doc->createElement('premiered');
- $elm->appendText(time2str('%Y-%m-%d', $channel->{'date'}));
- $show->appendChild($elm);
- $elm = $doc->createElement('plot');
- $elm->appendText($channel->{'description'});
- $show->appendChild($elm);
- # Return the string
- return $doc->toString();
- }
- sub buildNFO($) {
- my ($video) = @_;
- # Create an XML tree
- my $doc = XML::LibXML::Document->new('1.0', 'UTF-8');
- $doc->setStandalone(1);
- my $show = $doc->createElement('episodedetails');
- $doc->setDocumentElement($show);
- my $file = $doc->createElement('fileinfo');
- $show->appendChild($file);
- my $stream = $doc->createElement('streamdetails');
- $file->appendChild($stream);
- my $stream_video = $doc->createElement('video');
- $stream->appendChild($stream_video);
- my $elm;
- # Add data
- $elm = $doc->createElement('season');
- $elm->appendText($video->{'season'});
- $show->appendChild($elm);
- $elm = $doc->createElement('episode');
- $elm->appendText($video->{'number'});
- $show->appendChild($elm);
- if (!defined($video->{'title'})) {
- $video->{'title'} = 'Episode ' . $video->{'number'};
- }
- $elm = $doc->createElement('title');
- $elm->appendText($video->{'title'});
- $show->appendChild($elm);
- if (!defined($video->{'date'})) {
- $video->{'date'} = time();
- }
- $elm = $doc->createElement('aired');
- $elm->appendText(time2str('%Y-%m-%d', $video->{'date'}));
- $show->appendChild($elm);
- if (defined($video->{'description'})) {
- $elm = $doc->createElement('plot');
- $elm->appendText($video->{'description'});
- $show->appendChild($elm);
- }
- if (defined($video->{'duration'})) {
- $elm = $doc->createElement('runtime');
- $elm->appendText($video->{'duration'});
- $show->appendChild($elm);
- $elm = $doc->createElement('durationinseconds');
- $elm->appendText($video->{'duration'});
- $stream_video->appendChild($elm);
- }
- if (defined($video->{'rating'})) {
- $elm = $doc->createElement('rating');
- $elm->appendText($video->{'rating'});
- $show->appendChild($elm);
- }
- if (defined($video->{'creator'})) {
- $elm = $doc->createElement('director');
- $elm->appendText($video->{'creator'});
- $show->appendChild($elm);
- }
- # Return the string
- return $doc->toString();
- }
- sub findFiles() {
- my %files = ();
- # Allow complete bypass
- if ($NO_FILES) {
- return \%files;
- }
- # Read the output directory
- my $fh = undef();
- opendir($fh, '.')
- or die('Unable to open files directory: ' . $! . "\n");
- while (my $file = readdir($fh)) {
- my ($season, $num, $id, $suffix) = parseFilename($file);
- if (defined($id) && length($id) > 0) {
- my $path = $dir . '/' . $file;
- # Create the record as needed
- if (!exists($files{$id})) {
- my %tmp = (
- 'season' => $season,
- 'number' => $num,
- );
- $files{$id} = \%tmp;
- }
- # Video or NFO?
- my $type = 'path';
- if ($suffix eq 'nfo') {
- $type = 'nfo';
- }
- # Deal with duplicates
- if (exists($files{$id}->{$type})) {
- if ($type eq 'nfo') {
- warn('Duplicate NFO: ' . $id . "\n\t" . $files{$id}->{'nfo'} . "\n\t" . $file . "\n");
- if (!$NO_RENAME) {
- if ($SUDO_CHATTR) {
- system('sudo', 'chattr', '-i', $path);
- }
- unlink($path);
- next;
- }
- } else {
- warn('Duplicate video: ' . $id . "\n\t" . $files{$id}->{'path'} . "\n\t" . $path . "\n");
- if (!$NO_RENAME) {
- my $del = $path;
- if ($suffix ne 'mp4' && $files{$id}->{'suffix'} eq 'mp4') {
- $del = $files{$id}->{'path'};
- }
- if ($SUDO_CHATTR) {
- system('sudo', 'chattr', '-i', $del);
- }
- unlink($del);
- next;
- }
- }
- }
- # Assign the component paths
- $files{$id}->{$type} = $file;
- if ($type ne 'nfo') {
- $files{$id}->{'suffix'} = $suffix;
- }
- }
- }
- close($fh);
- if ($DEBUG) {
- print STDERR 'Found ' . scalar(keys(%files)) . " local videos\n";
- if ($DEBUG > 1) {
- print STDERR prettyPrint(\%files, "\t") . "\n";
- }
- }
- return \%files;
- }
- sub fetchParse($$) {
- my ($name, $id) = @_;
- my $url = $API{$name}{'prefix'} . $id . $API{$name}{'suffix'} . '?';
- foreach my $key (keys(%{ $API{$name}{'params'} })) {
- $url .= '&' . uri_escape($key) . '=' . uri_escape($API{$name}{'params'}{$key});
- }
- # Fetch
- if ($DEBUG) {
- print STDERR 'Fetching ' . $name . ' API URL: ' . $url . "\n";
- }
- sleep($DELAY);
- my $content = get($url);
- if (!defined($content) || length($content) < 10) {
- die('Invalid content from URL: ' . $url . "\n");
- }
- # Parse
- my $data = decode_json($content);
- if (!defined($data) || ref($data) ne 'HASH') {
- die('Invalid JSON: ' . $content . "\n");
- }
- if ($DEBUG > 2) {
- print STDERR "Raw JSON data:\n" . prettyPrint($data, ' ') . "\n";
- }
- return $data;
- }
- sub getSubscriptions($$) {
- my ($user, $id) = @_;
- my $index = 1;
- my $itemCount = undef();
- my %subs = ();
- SUBS_LOOP:
- {
- # Build, fetch, parse
- $API{'subscriptions'}{'params'}{'start-index'} = $index;
- my $data = fetchParse('subscriptions', $id);
- # It's all in the feed
- if (!exists($data->{'feed'}) || ref($data->{'feed'}) ne 'HASH') {
- die("Invalid subscription data\n");
- }
- $data = $data->{'feed'};
- # Grab the total count, so we know when to stop
- if (!defined($itemCount)) {
- if ( !exists($data->{'openSearch$totalResults'})
- || ref($data->{'openSearch$totalResults'}) ne 'HASH'
- || !exists($data->{'openSearch$totalResults'}->{'$t'}))
- {
- die("Invalid subscription feed metadata\n");
- }
- $itemCount = $data->{'openSearch$totalResults'}->{'$t'};
- }
- # Process each item
- if (!exists($data->{'entry'}) || ref($data->{'entry'}) ne 'ARRAY') {
- die("Invalid subscription entries\n");
- }
- my $items = $data->{'entry'};
- foreach my $item (@{$items}) {
- if ( ref($item) ne 'HASH'
- || !exists($item->{'yt$username'})
- || ref($item->{'yt$username'}) ne 'HASH'
- || !exists($item->{'yt$username'}->{'$t'}))
- {
- next;
- }
- if ($DEBUG) {
- print STDERR prettyPrint($item->{'yt$username'}, "\t") . "\n";
- }
- $subs{ $item->{'yt$username'}->{'$t'} } = $user;
- }
- # Loop if there are results left to fetch
- $index += $BATCH_SIZE;
- if (defined($itemCount) && $itemCount >= $index) {
- # But don't go past the max supported index
- if ($index <= $MAX_INDEX) {
- redo SUBS_LOOP;
- }
- }
- }
- # Return the list of subscribed usernames
- return \%subs;
- }
- sub saveSubscriptions($$) {
- my ($folder, $subs) = @_;
- # Check for local subscriptions missing from YT
- my %locals = ();
- my $fh = undef();
- opendir($fh, '.')
- or die('Unable to open subscriptions directory: ' . $folder . ': ' . $! . "\n");
- while (my $file = readdir($fh)) {
- # Skip dotfiles
- if ($file =~ /^\./) {
- next;
- }
- # Skip non-directories
- if (!-d $file) {
- next;
- }
- # YT has some trouble with case
- my $lcFile = lc($file);
- # Anything else should be in the list
- if (!$subs->{$file} && !$subs->{$lcFile}) {
- print STDERR 'Missing YT subscription for: ' . $file . "\n";
- }
- # Note local subscriptions
- $locals{$lcFile} = 1;
- }
- closedir($fh);
- # Check for YT subscriptions missing locally
- foreach my $sub (keys(%{$subs})) {
- if (!exists($locals{ lc($sub) })) {
- print STDERR 'Adding local subscription for: ' . $sub . ' (' . $subs->{$sub} . ")\n";
- mkdir($folder . '/' . $sub);
- }
- }
- }
- sub saveChannel($) {
- my ($channel) = @_;
- my $nfo = 'tvshow.nfo';
- if (!-e $nfo) {
- if ($DEBUG) {
- print STDERR 'Saving series data for: ' . $channel->{'title'} . "\n";
- }
- # Save the poster
- if (exists($channel->{'thumbnail'}) && length($channel->{'thumbnail'}) > 5) {
- my $jpg = get($channel->{'thumbnail'});
- saveString('poster.jpg', $jpg);
- }
- # Save the series NFO
- my $xml = buildSeriesNFO($channel);
- if ($DEBUG > 1) {
- print STDERR 'Saving NFO: ' . $xml . "\n";
- }
- saveString($nfo, $xml);
- }
- }
- sub getChannel($) {
- my ($user) = @_;
- # Build, fetch, parse
- my $data = fetchParse('channel', $user);
- if (!exists($data->{'entry'}) || ref($data->{'entry'}) ne 'HASH') {
- die("Invalid channel data\n");
- }
- $data = $data->{'entry'};
- # Extract the data we want
- my %channel = (
- 'id' => $data->{'yt$channelId'}->{'$t'},
- 'title' => $data->{'title'}->{'$t'},
- 'date' => str2time($data->{'published'}->{'$t'}),
- 'description' => $data->{'summary'}->{'$t'},
- 'thumbnail' => $data->{'media$thumbnail'}->{'url'},
- );
- return \%channel;
- }
- sub readExcludes() {
- my %excludes = ();
- # Read and parse the excludes videos file, if it exists
- if (-e $EXCLUDES_FILE) {
- my $fh;
- open($fh, $EXCLUDES_FILE)
- or die('Unable to open excludes videos file: ' . $! . "\n");
- while (<$fh>) {
- # Skip blank lines and comments
- if ($_ =~ /^\s*#/ || $_ =~ /^\s*$/) {
- next;
- }
- # Match our specific format or whine
- if ($_ =~ /^\s*([\w\-]+)\s*$/) {
- if ($DEBUG > 1) {
- print STDERR 'Adding exclude video: ' . $1 . "\n";
- }
- $excludes{$1} = 1;
- } else {
- print STDERR 'Skipped exclude video line: ' . $_;
- }
- }
- close($fh);
- }
- return \%excludes;
- }
- sub readExtras() {
- my %extras = ();
- # Read and parse the extra videos file, if it exists
- if (-e $EXTRAS_FILE) {
- my $fh;
- open($fh, $EXTRAS_FILE)
- or die('Unable to open extra videos file: ' . $! . "\n");
- while (<$fh>) {
- # Skip blank lines and comments
- if ($_ =~ /^\s*#/ || $_ =~ /^\s*$/) {
- next;
- }
- # Match our specific format or whine
- if ($_ =~ /^\s*(\d+)\s*[=:>]+\s*([\w\-]+)\s*$/) {
- if ($DEBUG > 1) {
- print STDERR 'Adding extra video: ' . $1 . ' => ' . $2 . "\n";
- }
- $extras{$2} = $1;
- } else {
- print STDERR 'Skipped extra video line: ' . $_;
- }
- }
- close($fh);
- }
- return \%extras;
- }
- sub parseVideoData($) {
- my ($data) = @_;
- my %video = (
- 'title' => $data->{'title'},
- 'date' => str2time($data->{'uploaded'}),
- 'description' => $data->{'description'},
- 'duration' => $data->{'duration'},
- 'rating' => $data->{'rating'},
- 'creator' => $data->{'uploader'},
- );
- $video{'season'} = time2str('%Y', $video{'date'});
- return \%video;
- }
- sub findVideo($) {
- my ($id) = @_;
- # Build, fetch, parse
- my $data = fetchParse('video', $id);
- # Validate
- if (!exists($data->{'data'})) {
- die("Invalid video list\n");
- }
- $data = $data->{'data'};
- # Parse the video data
- return parseVideoData($data);
- }
- sub dropExcludes($) {
- my ($videos) = @_;
- my $excludes = readExcludes();
- foreach my $id (keys(%{$excludes})) {
- if (!exists($videos->{$id})) {
- if ($DEBUG > 1) {
- print STDERR 'Skipping unknown excludes video: ' . $id . "\n";
- }
- next;
- }
- delete($videos->{$id});
- }
- return $excludes;
- }
- sub addExtras($) {
- my ($videos) = @_;
- my $extras = readExtras();
- foreach my $id (keys(%{$extras})) {
- if (exists($videos->{$id})) {
- if ($DEBUG > 1) {
- print STDERR 'Skipping known extra video: ' . $id . "\n";
- }
- next;
- }
- my $video = findVideo($id);
- $videos->{$id} = $video;
- }
- return $extras;
- }
- sub findVideos($) {
- my ($user) = @_;
- my %videos = ();
- # Allow complete bypass
- if ($NO_SEARCH) {
- return \%videos;
- }
- # Loop through until we have all the entries
- my $index = 1;
- my $itemCount = undef();
- LOOP:
- {
- # Build, fetch, parse
- $API{'search'}{'params'}{'start-index'} = $index;
- my $data = fetchParse('search', $user);
- # Grab the total count, so we know when to stop
- if (!exists($data->{'data'})) {
- die("Invalid video list\n");
- }
- $data = $data->{'data'};
- if (!defined($itemCount) && exists($data->{'totalItems'})) {
- $itemCount = $data->{'totalItems'};
- }
- # Process each item
- if (!exists($data->{'items'}) || ref($data->{'items'}) ne 'ARRAY') {
- die("Invalid video list\n");
- }
- my $items = $data->{'items'};
- my $offset = 0;
- foreach my $item (@{$items}) {
- my $video = parseVideoData($item);
- $videos{ $item->{'id'} } = $video;
- $offset++;
- }
- # Loop if there are results left to fetch
- $index += $BATCH_SIZE;
- if (defined($itemCount) && $itemCount >= $index) {
- # But don't go past the max supported index
- if ($index <= $MAX_INDEX) {
- redo LOOP;
- }
- }
- }
- if ($DEBUG) {
- print STDERR 'Found ' . scalar(keys(%videos)) . " remote videos\n";
- if ($DEBUG > 1) {
- print STDERR prettyPrint(\%videos, "\t") . "\n";
- }
- }
- return \%videos;
- }
- sub updateNFOData($$$) {
- my ($file, $season, $episode) = @_;
- my $nfoData = undef();
- {
- open(my $fh, '<', $file)
- or warn('Unable to open NFO for renumber: ' . $file . ': ' . $! . "\n");
- $/ = undef;
- $nfoData = <$fh>;
- close($fh);
- }
- if ($nfoData) {
- $nfoData =~ s/<season>\d+<\/season>/<season>${season}<\/season>/;
- $nfoData =~ s/<episode>\d+<\/episode>/<episode>${episode}<\/episode>/;
- }
- return $nfoData;
- }
- sub videoSE($$) {
- my ($season, $episode) = @_;
- return sprintf('S%02dE%02d - ', $season, $episode);
- }
- sub videoPath($$$$) {
- my ($season, $episode, $id, $ext) = @_;
- return videoSE($season, $episode) . $id . '.' . $ext;
- }
- sub renameVideo($$$$$$) {
- my ($video, $suffix, $nfo, $id, $season, $episode) = @_;
- # Bail if disabled
- if ($NO_RENAME) {
- print STDERR 'Not renaming: ' . $video . "\n";
- return;
- }
- # General sanity checks
- if (!defined($video) || !defined($suffix) || !defined($nfo) || !defined($id) || !defined($season) || !defined($episode)) {
- die("Invalid call to renameVideo()\n\t" . $video . "\n\t" . $suffix . "\n\t" . $nfo . "\n\t" . $id . "\n\t" . $season . "\n\t" . $episode . "\n");
- }
- # Build a new path
- my $videoNew = videoPath($season, $episode, $id, $suffix);
- my $nfoNew = videoPath($season, $episode, $id, 'nfo');
- # Parse old NFO data
- my $nfoData = updateNFOData($nfo, $season, $episode)
- or die('No NFO data found, refusing to rename: ' . $id . "\n");
- # Specific sanity checks, as we do dangerous work here
- if (!-r $video || !-r $nfo) {
- die('Invalid video or NFO sources in rename: ' . $video . '/' . $nfo);
- }
- # Rename video
- if ($video ne $videoNew) {
- print STDERR 'Renaming ' . $video . ' => ' . $videoNew . "\n";
- if (-e $videoNew) {
- die('Rename: Target exists: ' . $videoNew . "\n");
- }
- if ($SUDO_CHATTR) {
- system('sudo', 'chattr', '-i', $video);
- }
- rename($video, $videoNew)
- or die('Unable to rename: ' . $video . ': ' . $! . "\n");
- }
- # Write a new NFO and unlink the old one
- if ($SUDO_CHATTR) {
- system('sudo', 'chattr', '-i', $nfo);
- }
- saveString($nfoNew, $nfoData);
- if ($nfo ne $nfoNew) {
- print STDERR 'Renaming ' . $nfo . ' => ' . $nfoNew . "\n";
- unlink($nfo)
- or warn('Unable to delete NFO during rename: ' . $! . "\n");
- }
- }
- sub parseFilename($) {
- my ($file) = @_;
- return $file =~ /^S(\d+)+E(\d+) - ([\w\-]+)\.(\w\w\w)$/i;
- }
|