subscribe.pl 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use FindBin;
  5. use lib "$FindBin::Bin/";
  6. use File::Touch;
  7. use File::Basename;
  8. use Date::Parse;
  9. use Date::Format;
  10. use LWP::UserAgent;
  11. use URI::Escape;
  12. use JSON;
  13. use XML::LibXML;
  14. use IPC::System::Simple qw( system run capture EXIT_ANY $EXITVAL );
  15. use IPC::Cmd qw( can_run );
  16. use Cwd qw( abs_path );
  17. use PrettyPrint;
  18. # Paramters
  19. my %USERS = ('profplump' => 'UCkj-Ob6eYHvzo-P0UWfnQzA', 'shanda' => 'UChfwMHzkPXOOFDce5hyQkTA');
  20. my $EXTRAS_FILE = 'extra_videos.ini';
  21. my $EXCLUDES_FILE = 'exclude_videos.ini';
  22. my $YTDL_BIN = $ENV{'HOME'} . '/bin/video/yt/youtube-dl';
  23. my @YTDL_ARGS = ('--force-ipv4', '--socket-timeout', '10', '--no-playlist', '--max-downloads', '1', '--age-limit', '99');
  24. my @YTDL_QUIET = ('--quiet', '--no-warnings');
  25. my @YTDL_DEBUG = ('--verbose');
  26. my $CHATTR = '/usr/bin/chattr';
  27. my $BATCH_SIZE = 50;
  28. my $MAX_INDEX = 25000;
  29. my $FETCH_LIMIT = 50;
  30. my $DELAY = 0.5;
  31. my $HTTP_TIMEOUT = 10;
  32. my $HTTP_UA = 'ZachBot/1.0 (Plex)';
  33. my $HTTP_VERIFY = 0;
  34. my $API_URL = 'https://www.googleapis.com/youtube/v3/';
  35. my $API_KEY = $ENV{'YT_API_KEY'};
  36. my $API_ST_REL = 0.50;
  37. my $API_ST_ABS = 5;
  38. my %VIDEO_FIELDS = (
  39. 'id' => '',
  40. 'title' => '',
  41. 'description' => '',
  42. 'creator' => '',
  43. 'publishedAt' => '',
  44. 'channelId' => '',
  45. 'rawDuration' => '',
  46. 'date', => 0,
  47. 'season' => 0,
  48. 'duration' => 0,
  49. );
  50. my %API = (
  51. #https://www.googleapis.com/youtube/v3/channels?part=id&forUsername={channelName}&key={YOUR_API_KEY}
  52. 'channelID' => {
  53. 'url' => $API_URL . 'channels',
  54. 'params' => {
  55. 'part' => 'id',
  56. 'key' => $API_KEY,
  57. },
  58. },
  59. #https://www.googleapis.com/youtube/v3/channels?part=snippet&id={channelID}&key={YOUR_API_KEY}
  60. 'channel' => {
  61. 'url' => $API_URL . 'channels',
  62. 'params' => {
  63. 'part' => 'snippet',
  64. 'key' => $API_KEY,
  65. },
  66. },
  67. #https://www.googleapis.com/youtube/v3/search?part=snippet&channelId={channelID}&maxResults={maxResults}&pageToken={foo}&safeSearch=none&type=video&key={YOUR_API_KEY}
  68. 'search' => {
  69. 'url' => $API_URL . 'search',
  70. 'params' => {
  71. 'part' => 'id',
  72. 'key' => $API_KEY,
  73. 'maxResults' => 5,
  74. 'safeSearch' => 'none',
  75. 'type' => 'video',
  76. 'order' => 'date',
  77. },
  78. },
  79. #https://www.googleapis.com/youtube/v3/videos?part=snippet&id={videoID}&key={YOUR_API_KEY}
  80. 'video' => {
  81. 'url' => $API_URL . 'videos',
  82. 'params' => {
  83. 'part' => 'snippet,contentDetails',
  84. 'key' => $API_KEY,
  85. },
  86. },
  87. #https://www.googleapis.com/youtube/v3/subscriptions?part=snippet&channelId={channelID}&key={YOUR_API_KEY}
  88. 'subscriptions' => {
  89. 'url' => $API_URL . 'subscriptions',
  90. 'params' => {
  91. 'part' => 'snippet',
  92. 'key' => $API_KEY,
  93. 'maxResults' => 5,
  94. },
  95. },
  96. );
  97. # Prototypes
  98. sub getVideoData($);
  99. sub localMetadata($$);
  100. sub findVideos($);
  101. sub findFiles($);
  102. sub buildNFO($);
  103. sub buildSeriesNFO($);
  104. sub getSubscriptions($);
  105. sub saveSubscriptions($$);
  106. sub saveChannel($);
  107. sub getChannel($);
  108. sub fetchParse($$);
  109. sub saveString($$);
  110. sub readExcludes();
  111. sub readExtras();
  112. sub parseVideoData($);
  113. sub calcVideoData($);
  114. sub dropExcludes($);
  115. sub addExtras($);
  116. sub updateNFOData($$$);
  117. sub videoNumberStr($$);
  118. sub videoSE($$);
  119. sub videoPath($$$$);
  120. sub renameVideo($$$$$$);
  121. sub parseFilename($);
  122. sub fetch($$);
  123. # Debug
  124. my $DEBUG = 0;
  125. if ($ENV{'DEBUG'}) {
  126. if ($ENV{'DEBUG'} =~ /(\d+)/) {
  127. $DEBUG = $1;
  128. } else {
  129. $DEBUG = 1;
  130. }
  131. $DELAY /= 2;
  132. }
  133. # Sanity check
  134. if (scalar(@ARGV) < 1) {
  135. die('Usage: ' . basename($0) . " output_directory\n");
  136. }
  137. if (!defined($API_KEY) || length($API_KEY) < 1) {
  138. die("No API key provided\n");
  139. }
  140. my $SUBSCRIPTIONS = 0;
  141. if ($0 =~ /subscription/i) {
  142. $SUBSCRIPTIONS = 1;
  143. }
  144. # Command-line parameters
  145. our ($DIR) = @ARGV;
  146. $DIR = Cwd::abs_path($DIR);
  147. $DIR =~ s/\/+$//;
  148. if (!-d $DIR) {
  149. die('Invalid output directory: ' . $DIR . "\n");
  150. }
  151. our $NAME = basename($DIR);
  152. our $ID = $NAME;
  153. if ($NAME =~ /^(.*)\s\(([\w\-]+)\)$/) {
  154. $NAME = $1;
  155. $ID = $2;
  156. if (length($ID) < 1 || !($ID =~ /^[\w\-]+$/)) {
  157. die('Invalid channel ID: ' . $ID . "\n");
  158. }
  159. } elsif (!$SUBSCRIPTIONS) {
  160. die('Invalid folder: ' . $DIR . "\n");
  161. }
  162. # Allow folders to disable themselves
  163. if (-e "${DIR}/season_done") {
  164. if ($DEBUG) {
  165. print STDERR "Folder disabled. Exiting...\n";
  166. }
  167. exit(0);
  168. }
  169. # Move to the target directory so we can use relative paths later
  170. chdir($DIR) or die('Unable to chdir to: ' . $DIR . "\n");
  171. # Environmental parameters (debug)
  172. my $NO_FETCH = 0;
  173. if ($ENV{'NO_FETCH'}) {
  174. $NO_FETCH = 1;
  175. }
  176. my $NO_NFO = 0;
  177. if ($ENV{'NO_NFO'}) {
  178. $NO_NFO = 1;
  179. }
  180. my $NO_SEARCH = 0;
  181. if ($ENV{'NO_SEARCH'}) {
  182. $NO_SEARCH = 1;
  183. }
  184. my $NO_FILES = 0;
  185. if ($ENV{'NO_FILES'}) {
  186. $NO_FILES = 1;
  187. }
  188. my $NO_CHANNEL = 0;
  189. if ($ENV{'NO_CHANNEL'}) {
  190. $NO_CHANNEL = 1;
  191. }
  192. my $NO_EXTRAS = 0;
  193. if ($ENV{'NO_EXTRAS'}) {
  194. $NO_EXTRAS = 1;
  195. }
  196. my $NO_EXCLUDES = 0;
  197. if ($ENV{'NO_EXCLUDES'}) {
  198. $NO_EXCLUDES = 1;
  199. }
  200. my $NO_RENAME = 0;
  201. if ($ENV{'NO_RENAME'}) {
  202. $NO_RENAME = 1;
  203. }
  204. # Environmental parameters (functional)
  205. my $SUDO_CHATTR = 1;
  206. if ($ENV{'NO_CHATTR'} || !can_run('sudo') || !can_run($CHATTR)) {
  207. $SUDO_CHATTR = 0;
  208. }
  209. if ($ENV{'MAX_INDEX'} && $ENV{'MAX_INDEX'} =~ /(\d+)/) {
  210. $MAX_INDEX = $1;
  211. }
  212. if ($ENV{'BATCH_SIZE'} && $ENV{'BATCH_SIZE'} =~ /(\d+)/) {
  213. $BATCH_SIZE = $1;
  214. }
  215. if (exists($ENV{'FETCH_LIMIT'}) && $ENV{'FETCH_LIMIT'} =~ /(\d+)/) {
  216. $FETCH_LIMIT = $1;
  217. }
  218. # Construct globals
  219. my $LWP = undef();
  220. foreach my $key (keys(%API)) {
  221. if (exists($API{$key}{'params'}{'maxResults'})) {
  222. $API{$key}{'params'}{'maxResults'} = $BATCH_SIZE;
  223. }
  224. }
  225. if ($DEBUG > 1) {
  226. push(@YTDL_ARGS, @YTDL_DEBUG);
  227. } elsif (!$DEBUG) {
  228. push(@YTDL_ARGS, @YTDL_QUIET);
  229. }
  230. # Allow use as a subscription manager
  231. if ($SUBSCRIPTIONS) {
  232. my %subs = ();
  233. foreach my $user (keys(%USERS)) {
  234. my $tmp = getSubscriptions($USERS{$user});
  235. foreach my $sub (keys(%{$tmp})) {
  236. $subs{$sub} = $tmp->{$sub};
  237. }
  238. }
  239. saveSubscriptions($DIR, \%subs);
  240. exit(0);
  241. }
  242. # Disable channel fetching if the ID channel is "None"
  243. if ($ID =~ /^\s*None\s*$/i) {
  244. if ($DEBUG) {
  245. print STDERR 'Channel features disabled for: ' . $NAME . "\n";
  246. }
  247. $NO_CHANNEL = 1;
  248. $NO_SEARCH = 1;
  249. }
  250. # Run the local "update" script if any
  251. {
  252. my @update = ('./update', $DIR);
  253. if (can_run($update[0])) {
  254. if ($DEBUG) {
  255. print STDERR "Executing local update script...\n";
  256. }
  257. system(@update);
  258. }
  259. }
  260. # Grab the channel data
  261. my $channel = {};
  262. if (!$NO_CHANNEL) {
  263. $channel = getChannel($ID);
  264. saveChannel($channel);
  265. }
  266. # Find all the user's videos on YT
  267. my $videos = {};
  268. if (!$NO_SEARCH) {
  269. $videos = findVideos($ID);
  270. }
  271. # Find any requested "extra" videos
  272. my $extras = {};
  273. if (!$NO_EXTRAS) {
  274. $extras = addExtras($videos);
  275. }
  276. # Drop any "excludes" videos
  277. my $excludes = {};
  278. if (!$NO_EXCLUDES) {
  279. $excludes = dropExcludes($videos);
  280. }
  281. # Find all existing YT files on disk
  282. my $files = {};
  283. if (!$NO_FILES) {
  284. $files = findFiles($videos);
  285. }
  286. # Deal with unknown videos
  287. foreach my $id (keys(%{$files})) {
  288. if ((!exists($videos->{$id}) || !$videos->{$id}->{'date'}) && $files->{$id}->{'season'} > 0) {
  289. # Check the ID specifically to find things the APIv3 fuzzy search misses
  290. if ($DEBUG) {
  291. print STDERR 'Validating local video by ID: ' . $id . "\n";
  292. }
  293. my $video = getVideoData([$id]);
  294. if ($video->[0] && $video->[0]->{'season'} > 0 && $video->[0]->{'channelId'} eq $ID) {
  295. $videos->{$id} = $video->[0];
  296. next;
  297. }
  298. # Whine when videos do go away
  299. warn('Local video not known to YT channel (' . $NAME . '): ' . $id . "\n");
  300. renameVideo($files->{$id}->{'path'}, $files->{$id}->{'suffix'}, $files->{$id}->{'nfo'}, $id, 0, $files->{$id}->{'season'} . $files->{$id}->{'number'});
  301. }
  302. }
  303. # Calculate the episode number using the publish dates
  304. {
  305. my @byDate = sort { $videos->{$a}->{'date'} <=> $videos->{$b}->{'date'} || $a cmp $b } keys %{$videos};
  306. my $num = undef();
  307. my $lastSeason = undef();
  308. foreach my $id (@byDate) {
  309. if (!$lastSeason || $lastSeason != $videos->{$id}->{'season'}) {
  310. $num = 1;
  311. $lastSeason = $videos->{$id}->{'season'};
  312. }
  313. $videos->{$id}->{'number'} = $num++;
  314. }
  315. }
  316. # Fill in missing videos and NFOs
  317. my $fetched = 0;
  318. FETCH_LOOP: foreach my $id (keys(%{$videos})) {
  319. if ($DEBUG > 1) {
  320. print STDERR 'Checking remote video: ' . $id . "\n";
  321. if (!exists($files->{$id})) {
  322. print STDERR "\tLocal file: <missing>\n";
  323. } else {
  324. print STDERR "\tLocal media: " . $files->{$id}->{'path'} . "\n";
  325. print STDERR "\tLocal NFO: " . $files->{$id}->{'nfo'} . "\n";
  326. }
  327. }
  328. my $nfo = videoPath($videos->{$id}->{'season'}, $videos->{$id}->{'number'}, $id, 'nfo');
  329. # Determine if we may or must rename
  330. my $rename = 0;
  331. if (exists($files->{$id})) {
  332. # Rename if we drift
  333. if (!$rename && $files->{$id}->{'path'}) {
  334. if ( $files->{$id}->{'number'} != $videos->{$id}->{'number'}
  335. || $files->{$id}->{'season'} != $videos->{$id}->{'season'})
  336. {
  337. if ($DEBUG) {
  338. print STDERR "Rename due to drift\n";
  339. }
  340. $rename = 1;
  341. }
  342. }
  343. # Always rename if the NFO does not match the media
  344. if (!$rename && $files->{$id}->{'path'} && $files->{$id}->{'nfo'}) {
  345. my ($season, $number) = parseFilename($files->{$id}->{'nfo'});
  346. if ($files->{$id}->{'season'} != $season || $files->{$id}->{'number'} != $number) {
  347. if ($DEBUG) {
  348. print STDERR "Rename due to media-metadata mismatch\n";
  349. }
  350. $rename = 1;
  351. }
  352. }
  353. }
  354. # Warn (and optionally rename) as selected
  355. if ($rename) {
  356. print STDERR 'Video ' . $id . ' had number ' . $files->{$id}->{'number'} . ' but now has number ' . $videos->{$id}->{'number'} . "\n";
  357. renameVideo($files->{$id}->{'path'}, $files->{$id}->{'suffix'}, $files->{$id}->{'nfo'}, $id, $videos->{$id}->{'season'}, $videos->{$id}->{'number'});
  358. }
  359. # If we haven't heard of the file, or don't have an NFO for it
  360. # Checking for the NFO allows use to resume failed downloads
  361. if (!exists($files->{$id}) || !-e $nfo) {
  362. if ($DEBUG) {
  363. print STDERR 'Fetching video: ' . $id . "\n";
  364. }
  365. # Let youtube-dl handle the URLs and downloading
  366. {
  367. my @args = ('--output', videoSE($videos->{$id}->{'season'}, $videos->{$id}->{'number'}) . '%(id)s.%(ext)s', '--', $id);
  368. my @name = ($YTDL_BIN);
  369. push(@name, @YTDL_ARGS);
  370. my @fetch = @name;
  371. push(@name, '--get-filename');
  372. push(@fetch, @args);
  373. push(@name, @args);
  374. if ($NO_FETCH) {
  375. print STDERR 'Not running: ' . join(' ', @fetch) . "\n";
  376. } else {
  377. # Count fetch attempts (even if they fail later)
  378. $fetched++;
  379. if ($FETCH_LIMIT && $fetched > $FETCH_LIMIT) {
  380. print STDERR 'Reached fetch limit (' . $FETCH_LIMIT . ') for: ' . $NAME . "\n";
  381. if ($DEBUG) {
  382. print STDERR "\tLocal/Remote videos at start:" . scalar(keys(%{$files})) . '/' . scalar(keys(%{$videos})) . "\n";
  383. }
  384. exit 1;
  385. }
  386. # Find the output file name
  387. if ($DEBUG > 1) {
  388. print STDERR join(' ', @name) . "\n";
  389. }
  390. sleep($DELAY);
  391. my $file = capture(EXIT_ANY, @name);
  392. if ($EXITVAL != 0) {
  393. warn('Error executing youtube-dl for name: ' . $NAME . '/' . $id . ' (' . $EXITVAL . ")\n");
  394. next FETCH_LOOP;
  395. }
  396. $file =~ s/^\s+//;
  397. $file =~ s/\s+$//;
  398. # Sanity check
  399. if (!$file) {
  400. warn('No file name available for video: ' . $NAME . '/' . $id . "\n");
  401. next FETCH_LOOP;
  402. }
  403. # Download
  404. if ($DEBUG > 1) {
  405. print STDERR join(' ', @fetch) . "\n";
  406. }
  407. sleep($DELAY);
  408. my $exit = run(EXIT_ANY, @fetch);
  409. if ($exit != 0) {
  410. warn('Error executing youtube-dl for video: ' . $NAME . '/' . $id . "\n");
  411. next FETCH_LOOP;
  412. }
  413. # Ensure we found something useful
  414. if (-e $file . '.part') {
  415. warn('Partial download detected: ' . $NAME . '/' . $file . "\n");
  416. next FETCH_LOOP;
  417. }
  418. if (!-s $file) {
  419. warn('No output video file: ' . $NAME . '/' . $file . "\n");
  420. next FETCH_LOOP;
  421. }
  422. # Touch the file to reflect the download time rather than the upload time
  423. touch($file);
  424. }
  425. }
  426. # Build and save the XML document
  427. my $xml = buildNFO($videos->{$id});
  428. if ($DEBUG > 1) {
  429. print STDERR 'Saving NFO: ' . $xml . "\n";
  430. }
  431. if ($NO_NFO) {
  432. print STDERR "Not saving NFO\n";
  433. } else {
  434. saveString($nfo, $xml);
  435. }
  436. }
  437. }
  438. sub saveString($$) {
  439. my ($path, $str) = @_;
  440. my $fh = undef();
  441. if (!open($fh, '>', $path)) {
  442. warn('Cannot open file for writing: ' . $path . ': ' . $! . "\n");
  443. return undef();
  444. }
  445. print $fh $str;
  446. close($fh);
  447. return 1;
  448. }
  449. sub buildSeriesNFO($) {
  450. my ($channel) = @_;
  451. # Create an XML tree
  452. my $doc = XML::LibXML::Document->new('1.0', 'UTF-8');
  453. $doc->setStandalone(1);
  454. my $show = $doc->createElement('tvshow');
  455. $doc->setDocumentElement($show);
  456. my $elm;
  457. # Add data
  458. $elm = $doc->createElement('title');
  459. $elm->appendText($channel->{'title'});
  460. $show->appendChild($elm);
  461. $elm = $doc->createElement('premiered');
  462. $elm->appendText(time2str('%Y-%m-%d', $channel->{'date'}));
  463. $show->appendChild($elm);
  464. $elm = $doc->createElement('plot');
  465. $elm->appendText($channel->{'description'});
  466. $show->appendChild($elm);
  467. # Return the string
  468. return $doc->toString();
  469. }
  470. sub buildNFO($) {
  471. my ($video) = @_;
  472. # Create an XML tree
  473. my $doc = XML::LibXML::Document->new('1.0', 'UTF-8');
  474. $doc->setStandalone(1);
  475. my $show = $doc->createElement('episodedetails');
  476. $doc->setDocumentElement($show);
  477. my $file = $doc->createElement('fileinfo');
  478. $show->appendChild($file);
  479. my $stream = $doc->createElement('streamdetails');
  480. $file->appendChild($stream);
  481. my $stream_video = $doc->createElement('video');
  482. $stream->appendChild($stream_video);
  483. my $elm;
  484. # Add data
  485. $elm = $doc->createElement('season');
  486. $elm->appendText($video->{'season'});
  487. $show->appendChild($elm);
  488. $elm = $doc->createElement('episode');
  489. $elm->appendText(videoNumberStr($video->{'season'}, $video->{'number'}));
  490. $show->appendChild($elm);
  491. if (!defined($video->{'title'})) {
  492. $video->{'title'} = 'Episode ' . $video->{'number'};
  493. }
  494. $elm = $doc->createElement('title');
  495. $elm->appendText($video->{'title'});
  496. $show->appendChild($elm);
  497. if (!defined($video->{'date'})) {
  498. $video->{'date'} = time();
  499. }
  500. $elm = $doc->createElement('aired');
  501. $elm->appendText(time2str('%Y-%m-%d', $video->{'date'}));
  502. $show->appendChild($elm);
  503. if (defined($video->{'description'})) {
  504. $elm = $doc->createElement('plot');
  505. $elm->appendText($video->{'description'});
  506. $show->appendChild($elm);
  507. }
  508. if (defined($video->{'duration'})) {
  509. $elm = $doc->createElement('runtime');
  510. $elm->appendText($video->{'duration'});
  511. $show->appendChild($elm);
  512. $elm = $doc->createElement('durationinseconds');
  513. $elm->appendText($video->{'duration'});
  514. $stream_video->appendChild($elm);
  515. }
  516. if (defined($video->{'creator'})) {
  517. $elm = $doc->createElement('director');
  518. $elm->appendText($video->{'creator'});
  519. $show->appendChild($elm);
  520. }
  521. # Return the string
  522. return $doc->toString();
  523. }
  524. sub findFiles($) {
  525. my ($videos) = @_;
  526. my %files = ();
  527. our $NAME;
  528. # Allow complete bypass
  529. if ($NO_FILES) {
  530. return \%files;
  531. }
  532. # Read the output directory
  533. my $fh = undef();
  534. opendir($fh, '.')
  535. or die($NAME . ': Unable to open files directory: ' . $! . "\n");
  536. while (my $file = readdir($fh)) {
  537. my ($season, $number, $id, $suffix) = parseFilename($file);
  538. if (defined($id) && length($id) > 0) {
  539. # Determine if there is another video with the same season/number
  540. my $exists = 0;
  541. if ( exists($videos->{$id})
  542. && exists($videos->{$id}->{'season'})
  543. && exists($videos->{$id}->{'number'})
  544. && $season == $videos->{$id}->{'season'}
  545. && $number == $videos->{$id}->{'number'})
  546. {
  547. $exists = 1;
  548. }
  549. # Create the record as needed
  550. if (!exists($files{$id})) {
  551. my %tmp = (
  552. 'season' => $season,
  553. 'number' => $number,
  554. );
  555. $files{$id} = \%tmp;
  556. }
  557. # Video or NFO?
  558. my $type = 'path';
  559. if ($suffix eq 'nfo') {
  560. $type = 'nfo';
  561. }
  562. # Deal with duplicates
  563. if (exists($files{$id}->{$type})) {
  564. if ($type eq 'nfo') {
  565. warn('Duplicate NFO: ' . $id . "\n\t" . $files{$id}->{'nfo'} . "\n\t" . $file . "\n");
  566. if (!$NO_RENAME) {
  567. my $del = $file;
  568. if ($exists) {
  569. $del = $files{$id}->{'nfo'};
  570. }
  571. warn("\tDeleting: " . $file . "\n");
  572. if ($SUDO_CHATTR) {
  573. system('sudo', $CHATTR, '-i', Cwd::abs_path($file));
  574. }
  575. unlink($file);
  576. next;
  577. }
  578. } else {
  579. warn('Duplicate video: ' . $id . "\n\t" . $files{$id}->{'path'} . "\n\t" . $file . "\n");
  580. if (!$NO_RENAME) {
  581. my $del = $file;
  582. if ($exists) {
  583. $del = $files{$id}->{'path'};
  584. } elsif ($suffix ne 'mp4' && $files{$id}->{'suffix'} eq 'mp4') {
  585. $del = $files{$id}->{'path'};
  586. }
  587. warn("\tDeleting: " . $del . "\n");
  588. if ($SUDO_CHATTR) {
  589. system('sudo', $CHATTR, '-i', Cwd::abs_path($del));
  590. }
  591. unlink($del);
  592. next;
  593. }
  594. }
  595. }
  596. # Assign the component paths
  597. $files{$id}->{$type} = $file;
  598. if ($type ne 'nfo') {
  599. $files{$id}->{'suffix'} = $suffix;
  600. }
  601. }
  602. }
  603. close($fh);
  604. if ($DEBUG) {
  605. print STDERR 'Found ' . scalar(keys(%files)) . " local videos\n";
  606. if ($DEBUG > 1) {
  607. print STDERR prettyPrint(\%files, "\t") . "\n";
  608. }
  609. }
  610. return \%files;
  611. }
  612. sub fetchParse($$) {
  613. my ($name, $params) = @_;
  614. our $NAME;
  615. my $url = $API{$name}{'url'} . '?';
  616. foreach my $key (keys(%{ $API{$name}{'params'} }), keys(%{$params})) {
  617. $url .= '&' . uri_escape($key) . '=';
  618. if (exists($params->{$key})) {
  619. $url .= uri_escape($params->{$key});
  620. } else {
  621. $url .= uri_escape($API{$name}{'params'}{$key});
  622. }
  623. }
  624. # Fetch
  625. if ($DEBUG > 1) {
  626. print STDERR 'Fetching ' . $name . ' API URL: ' . $url . "\n";
  627. }
  628. sleep($DELAY);
  629. my $content;
  630. my $code = fetch($url, \$content);
  631. if ($code != 200 || !defined($content) || length($content) < 10) {
  632. my $msg = $NAME . ': Invalid content from URL (' . $code . '): ' . $url . "\n" . $content . "\n";
  633. if ($code >= 500 && $code < 600) {
  634. if ($DEBUG) {
  635. print STDERR $msg;
  636. }
  637. exit(0);
  638. }
  639. if ($code >= 400 && $code < 500) {
  640. if ($DEBUG) {
  641. print STDERR "Wainting 30 seconds after 400 error\n";
  642. }
  643. sleep(30);
  644. }
  645. die($msg);
  646. }
  647. # Parse
  648. if ($DEBUG > 2) {
  649. print STDERR "Raw JSON data:\n" . $content . "\n";
  650. }
  651. my $data = decode_json($content);
  652. if (!defined($data) || ref($data) ne 'HASH') {
  653. die($NAME . ': Invalid JSON: ' . $content . "\n");
  654. }
  655. return $data;
  656. }
  657. sub getSubscriptions($) {
  658. my ($id) = @_;
  659. our $NAME;
  660. # Loop through until we have all the entries
  661. my $pageToken = '';
  662. my %subs = ();
  663. SUBS_LOOP:
  664. {
  665. # Build, fetch, parse, check
  666. my $data = fetchParse('subscriptions', { 'channelId' => $id, 'pageToken' => $pageToken });
  667. $pageToken = '';
  668. if ( exists($data->{'pageInfo'})
  669. && ref($data->{'pageInfo'}) eq 'HASH'
  670. && exists($data->{'pageInfo'}->{'totalResults'})
  671. && $data->{'pageInfo'}->{'totalResults'} > 0
  672. && exists($data->{'pageInfo'}->{'resultsPerPage'})
  673. && exists($data->{'items'})
  674. && ref($data->{'items'}) eq 'ARRAY')
  675. {
  676. if (exists($data->{'nextPageToken'})) {
  677. $pageToken = $data->{'nextPageToken'};
  678. }
  679. $data = $data->{'items'};
  680. } else {
  681. die($NAME . ": Invalid subscription data\n");
  682. }
  683. foreach my $item (@{$data}) {
  684. if ( !exists($item->{'snippet'})
  685. || ref($item->{'snippet'}) ne 'HASH'
  686. || !exists($item->{'snippet'}->{'title'})
  687. || !exists($item->{'snippet'}->{'resourceId'})
  688. || ref($item->{'snippet'}->{'resourceId'}) ne 'HASH'
  689. || !exists($item->{'snippet'}->{'resourceId'}->{'channelId'}))
  690. {
  691. warn($NAME . ": Skipping invalid subscription\n");
  692. if ($DEBUG > 2) {
  693. print STDERR prettyPrint($item, "\t") . "\n";
  694. }
  695. next;
  696. }
  697. if ($DEBUG) {
  698. print STDERR $item->{'snippet'}->{'title'} . ' => ' . $item->{'snippet'}->{'resourceId'}->{'channelId'} . "\n";
  699. }
  700. $subs{ $item->{'snippet'}->{'resourceId'}->{'channelId'} } = $item->{'snippet'}->{'title'};
  701. }
  702. # Loop if there are results left to fetch
  703. if ($pageToken) {
  704. redo SUBS_LOOP;
  705. }
  706. }
  707. # Return the list of subscribed channel IDs
  708. return \%subs;
  709. }
  710. sub saveSubscriptions($$) {
  711. my ($folder, $subs) = @_;
  712. our $NAME;
  713. # Check for local subscriptions missing from YT
  714. my %locals = ();
  715. my $fh = undef();
  716. opendir($fh, '.')
  717. or die($NAME . ': Unable to open subscriptions directory: ' . $folder . ': ' . $! . "\n");
  718. while (my $file = readdir($fh)) {
  719. # Skip dotfiles
  720. if ($file =~ /^\./) {
  721. next;
  722. }
  723. # Skip non-directories
  724. if (!-d $file) {
  725. next;
  726. }
  727. # Extract the YTID
  728. my ($id) = $file =~ /\(([\w\-]+)\)$/;
  729. if (!$id) {
  730. warn('Skipping invalid local subscription: ' . $file . "\n");
  731. next;
  732. }
  733. # Skip "None" directories (i.e. non-channel subscriptions)
  734. if ($id eq 'None') {
  735. if ($DEBUG) {
  736. print STDERR 'Ignoring non-channel subscription: ' . $file . "\n";
  737. }
  738. next;
  739. }
  740. # Anything else should be in the list
  741. if (!$subs->{$id}) {
  742. my (undef(), undef(), $hour) = localtime(time);
  743. if ($hour == 0 || $DEBUG) {
  744. print STDERR 'Missing YT subscription for: ' . $file . "\n";
  745. }
  746. }
  747. # Note local subscriptions
  748. $locals{$id} = 1;
  749. }
  750. closedir($fh);
  751. # Check for YT subscriptions missing locally
  752. foreach my $id (keys(%{$subs})) {
  753. if (!exists($locals{$id})) {
  754. my $dir = $subs->{$id} . ' (' . $id . ')';
  755. print STDERR 'Adding local subscription for: ' . $dir . "\n";
  756. mkdir($folder . '/' . $dir);
  757. }
  758. }
  759. }
  760. sub saveChannel($) {
  761. my ($channel) = @_;
  762. my $nfo = 'tvshow.nfo';
  763. if (!-e $nfo) {
  764. if ($DEBUG) {
  765. print STDERR 'Saving series data for: ' . $channel->{'title'} . "\n";
  766. }
  767. # Save the poster
  768. if (exists($channel->{'thumbnail'}) && length($channel->{'thumbnail'}) > 5) {
  769. my $jpg;
  770. if (fetch($channel->{'thumbnail'}, \$jpg) == 200) {
  771. saveString('poster.jpg', $jpg);
  772. }
  773. }
  774. # Save the series NFO
  775. my $xml = buildSeriesNFO($channel);
  776. if ($DEBUG > 1) {
  777. print STDERR 'Saving NFO: ' . $xml . "\n";
  778. }
  779. saveString($nfo, $xml);
  780. }
  781. }
  782. sub getChannel($) {
  783. my ($id) = @_;
  784. our $NAME;
  785. # Build, fetch, parse, check
  786. my $data = fetchParse('channel', { 'id' => $id });
  787. if ( exists($data->{'pageInfo'})
  788. && ref($data->{'pageInfo'}) eq 'HASH'
  789. && exists($data->{'pageInfo'}->{'totalResults'})
  790. && $data->{'pageInfo'}->{'totalResults'} == 1
  791. && exists($data->{'items'})
  792. && ref($data->{'items'}) eq 'ARRAY'
  793. && scalar(@{ $data->{'items'} }) > 0
  794. && ref($data->{'items'}[0]) eq 'HASH'
  795. && exists($data->{'items'}[0]->{'snippet'})
  796. && ref($data->{'items'}[0]->{'snippet'}) eq 'HASH')
  797. {
  798. $data = $data->{'items'}[0]->{'snippet'};
  799. } else {
  800. die($NAME . ": Invalid channel data\n");
  801. }
  802. # Extract the data we want
  803. my %channel = (
  804. 'id' => $id,
  805. 'title' => $data->{'title'},
  806. 'date' => str2time($data->{'publishedAt'}),
  807. 'description' => $data->{'description'},
  808. 'thumbnail' => $data->{'thumbnails'}->{'high'}->{'url'},
  809. );
  810. return \%channel;
  811. }
  812. sub readExcludes() {
  813. my %excludes = ();
  814. our $NAME;
  815. # Read and parse the excludes videos file, if it exists
  816. if (-e $EXCLUDES_FILE) {
  817. my $fh;
  818. open($fh, $EXCLUDES_FILE)
  819. or die($NAME . ': Unable to open excludes videos file: ' . $! . "\n");
  820. while (<$fh>) {
  821. # Skip blank lines and comments
  822. if ($_ =~ /^\s*#/ || $_ =~ /^\s*$/) {
  823. next;
  824. }
  825. # Match our specific format or whine
  826. if ($_ =~ /^\s*([\w\-]+)\s*$/) {
  827. if ($DEBUG > 1) {
  828. print STDERR 'Adding exclude video: ' . $1 . "\n";
  829. }
  830. $excludes{$1} = 1;
  831. } else {
  832. print STDERR 'Skipped exclude video line: ' . $_;
  833. }
  834. }
  835. close($fh);
  836. }
  837. return \%excludes;
  838. }
  839. sub readExtras() {
  840. my %extras = ();
  841. our $NAME;
  842. # Read and parse the extra videos file, if it exists
  843. if (-e $EXTRAS_FILE) {
  844. my $fh;
  845. open($fh, $EXTRAS_FILE)
  846. or die($NAME . ': Unable to open extra videos file: ' . $! . "\n");
  847. while (<$fh>) {
  848. # Skip blank lines and comments
  849. if ($_ =~ /^\s*#/ || $_ =~ /^\s*$/) {
  850. next;
  851. }
  852. # Match our specific format or whine
  853. if ($_ =~ /^\s*([\w\-]+)\s*$/) {
  854. if ($DEBUG > 1) {
  855. print STDERR 'Adding extra video: ' . $1 . "\n";
  856. }
  857. $extras{$1} = 1;
  858. } else {
  859. print STDERR 'Skipped extra video line: ' . $_;
  860. }
  861. }
  862. close($fh);
  863. }
  864. return \%extras;
  865. }
  866. sub parseVideoData($) {
  867. my ($data) = @_;
  868. my %video = %VIDEO_FIELDS;
  869. # Sanity check
  870. if ( !exists($data->{'id'})
  871. || !exists($data->{'snippet'})
  872. || ref($data->{'snippet'}) ne 'HASH'
  873. || !exists($data->{'contentDetails'})
  874. || ref($data->{'contentDetails'}) ne 'HASH'
  875. || !exists($data->{'contentDetails'}->{'duration'}))
  876. {
  877. if ($DEBUG > 2) {
  878. print STDERR "Unable to parse video data:\n" . prettyPrint($data, "\t") . "\n";
  879. }
  880. return undef();
  881. }
  882. # Direct extraction
  883. $video{'id'} = $data->{'id'};
  884. $video{'title'} = $data->{'snippet'}->{'title'};
  885. $video{'description'} = $data->{'snippet'}->{'description'};
  886. $video{'creator'} = $data->{'snippet'}->{'channelTitle'};
  887. $video{'publishedAt'} = $data->{'snippet'}->{'publishedAt'};
  888. $video{'channelId'} = $data->{'snippet'}->{'channelId'};
  889. $video{'rawDuration'} = $data->{'contentDetails'}->{'duration'};
  890. # Convert dates and whatnot
  891. calcVideoData(\%video);
  892. return \%video;
  893. }
  894. sub calcVideoData($) {
  895. my ($video) = @_;
  896. if ($video->{'publishedAt'} && !$video->{'date'}) {
  897. $video->{'date'} = str2time($video->{'publishedAt'});
  898. }
  899. if ($video->{'date'}) {
  900. $video->{'season'} = time2str('%Y', $video->{'date'});
  901. } else {
  902. $video->{'season'} = 0;
  903. }
  904. if ($video->{'rawDuration'} =~ /PT(\d+)H(\d+)M(\d+)S/) {
  905. $video->{'duration'} = ($1 * 3600) + ($2 * 60) + $2;
  906. } elsif ($video->{'rawDuration'} =~ /PT(\d+)M(\d+)S/) {
  907. $video->{'duration'} = ($1 * 60) + $2;
  908. } elsif ($video->{'rawDuration'} =~ /PT(\d+)S/) {
  909. $video->{'duration'} = $1;
  910. }
  911. }
  912. sub dropExcludes($) {
  913. my ($videos) = @_;
  914. my $excludes = readExcludes();
  915. foreach my $id (keys(%{$excludes})) {
  916. if (!exists($videos->{$id})) {
  917. if ($DEBUG > 1) {
  918. print STDERR 'Skipping unknown excludes video: ' . $id . "\n";
  919. }
  920. next;
  921. }
  922. delete($videos->{$id});
  923. }
  924. return $excludes;
  925. }
  926. sub addExtras($) {
  927. my ($videos) = @_;
  928. my $extras = readExtras();
  929. foreach my $id (keys(%{$extras})) {
  930. if (exists($videos->{$id})) {
  931. if ($DEBUG > 1) {
  932. print STDERR 'Skipping known extra video: ' . $id . "\n";
  933. }
  934. next;
  935. }
  936. my $video = getVideoData([$id]);
  937. $videos->{$id} = $video->[0];
  938. }
  939. return $extras;
  940. }
  941. sub getVideoData($) {
  942. my ($ids) = @_;
  943. my @videos = ();
  944. our $NAME;
  945. my %found = ();
  946. # Do a batch request for the entire batch of video data
  947. my $data = fetchParse('video', { 'id' => join(',', @{$ids}) });
  948. if ( exists($data->{'pageInfo'})
  949. && ref($data->{'pageInfo'}) eq 'HASH'
  950. && exists($data->{'pageInfo'}->{'totalResults'})
  951. && exists($data->{'items'})
  952. && ref($data->{'items'}) eq 'ARRAY')
  953. {
  954. if (scalar(@{$ids}) > 1 && $data->{'pageInfo'}->{'totalResults'} != scalar(@{$ids})) {
  955. die($NAME . ': Video/metadata search count mismatch: ' . $data->{'pageInfo'}->{'totalResults'} . '/' . scalar(@{$ids}) . "\n");
  956. }
  957. $data = $data->{'items'};
  958. } else {
  959. die($NAME . ": Invalid search video data\n" . prettyPrint($data, "\t") . "\n");
  960. }
  961. # Build each metadata record
  962. foreach my $item (@{$data}) {
  963. if (!exists($item->{'snippet'}) || ref($item->{'snippet'}) ne 'HASH') {
  964. warn($NAME . ": Skipping invalid metadata item\n");
  965. if ($DEBUG > 2) {
  966. print STDERR prettyPrint($item, "\t") . "\n";
  967. }
  968. next;
  969. }
  970. my $video = parseVideoData($item);
  971. if (!$video) {
  972. die($NAME . ": Unable to parse video data\n");
  973. }
  974. # Allow local metadata overrides
  975. localMetadata($video->{'id'}, $video);
  976. # Save
  977. $found{ $video->{'id'} } = 1;
  978. push(@videos, $video);
  979. }
  980. # Load local metadata even for videos YT doesn't know anything about
  981. foreach my $id (@{$ids}) {
  982. if ($found{$id}) {
  983. next;
  984. }
  985. my %video = %VIDEO_FIELDS;
  986. $video{'id'} = $id;
  987. localMetadata($id, \%video);
  988. # Save
  989. $found{$id} = 1;
  990. push(@videos, \%video);
  991. }
  992. # Parse the video data
  993. return \@videos;
  994. }
  995. sub localMetadata($$) {
  996. my ($id, $video) = @_;
  997. my $meta = $id . '.meta';
  998. if (!-r $meta) {
  999. return;
  1000. }
  1001. my $fh = undef();
  1002. open($fh, '<', $meta)
  1003. or warn('Unable to open local metadata file: ' . $meta . ': ' . $! . ". Skipping...\n");
  1004. while (<$fh>) {
  1005. if (/^\s*#/ || /^\s*$/) {
  1006. next;
  1007. }
  1008. if (/^\s*([^\s\=][^\=]*[^\s\=])\s*=\>?\s*(\S.*\S)\s*$/) {
  1009. if (exists($video->{$1})) {
  1010. if ($DEBUG) {
  1011. print STDERR 'Using local metadata line (' . $id . '): ' . $1 . ' => ' . $2 . "\n";
  1012. }
  1013. $video->{$1} = $2;
  1014. } else {
  1015. die('Invalid local metadata attribute (' . $1 . '): ' . $meta . "\n");
  1016. }
  1017. } else {
  1018. die('Invalid local metdata line (' . $meta . '): ' . chomp($_) . "\n");
  1019. }
  1020. }
  1021. close($fh);
  1022. calcVideoData($video);
  1023. }
  1024. sub findVideos($) {
  1025. my ($id) = @_;
  1026. my %videos = ();
  1027. our $NAME;
  1028. # Allow complete bypass
  1029. if ($NO_SEARCH) {
  1030. return \%videos;
  1031. }
  1032. # Loop through until we have all the entries
  1033. my $totalCount = 0;
  1034. my %params = ('channelId' => $id);
  1035. LOOP:
  1036. {
  1037. # Loop status
  1038. my $count = scalar(keys(%videos));
  1039. if ($DEBUG && $totalCount) {
  1040. print STDERR 'Fetching ' . $count . ' of ' . $totalCount . ' (' . int(100 * $count / $totalCount) . "%)\n";
  1041. }
  1042. # Build, fetch, parse, check
  1043. my $data = fetchParse('search', \%params);
  1044. delete($params{'pageToken'});
  1045. if ( exists($data->{'pageInfo'})
  1046. && ref($data->{'pageInfo'}) eq 'HASH'
  1047. && exists($data->{'pageInfo'}->{'totalResults'})
  1048. && exists($data->{'pageInfo'}->{'resultsPerPage'})
  1049. && exists($data->{'items'})
  1050. && ref($data->{'items'}) eq 'ARRAY')
  1051. {
  1052. if (!$totalCount) {
  1053. $totalCount = $data->{'pageInfo'}->{'totalResults'};
  1054. }
  1055. $data = $data->{'items'};
  1056. } else {
  1057. die($NAME . ": Invalid search data\n");
  1058. }
  1059. # Sanity check
  1060. if (scalar(@{$data}) < 1) {
  1061. last LOOP;
  1062. }
  1063. # Grab each video ID
  1064. my @ids = ();
  1065. foreach my $item (@{$data}) {
  1066. if (!exists($item->{'id'}) || ref($item->{'id'}) ne 'HASH' || !exists($item->{'id'}->{'videoId'})) {
  1067. warn($NAME . ": Skipping invalid video item\n");
  1068. if ($DEBUG > 2) {
  1069. print STDERR prettyPrint($data, "\t") . "\n";
  1070. }
  1071. next;
  1072. }
  1073. push(@ids, $item->{'id'}->{'videoId'});
  1074. }
  1075. # Do a batch request for the entire batch of video data
  1076. my $more = 0;
  1077. my $records = getVideoData(\@ids);
  1078. foreach my $rec (@{$records}) {
  1079. if (exists($rec->{'duration'}) && $rec->{'duration'} == 0) {
  1080. if ($DEBUG > 1) {
  1081. print STDERR 'Skipping 0-length video: ' . $rec->{'id'} . "\n";
  1082. }
  1083. next;
  1084. }
  1085. $videos{ $rec->{'id'} } = $rec;
  1086. if (!defined($params{'publishedBefore'}) || (defined($rec->{'publishedAt'}) && ($rec->{'publishedAt'} cmp $params{'publishedBefore'}) < 0)) {
  1087. $params{'publishedBefore'} = $rec->{'publishedAt'};
  1088. $more = 1;
  1089. }
  1090. }
  1091. # Loop if there are results left to fetch
  1092. if ($more && $params{'publishedBefore'} && $count <= $MAX_INDEX) {
  1093. redo LOOP;
  1094. }
  1095. }
  1096. # Sanity check/debug
  1097. {
  1098. my $count = scalar(keys(%videos));
  1099. if ($DEBUG) {
  1100. print STDERR 'Found ' . $count . " remote videos\n";
  1101. if ($DEBUG > 1) {
  1102. print STDERR prettyPrint(\%videos, "\t") . "\n";
  1103. }
  1104. }
  1105. if (abs(1 - ($count / $totalCount)) > $API_ST_REL && abs($totalCount - $count) > $API_ST_ABS) {
  1106. die($NAME . ': Found only ' . $count . ' of ' . $totalCount . ' (' . int(100 * $count / $totalCount) . "%) remote videos. Aborting...\n");
  1107. }
  1108. }
  1109. return \%videos;
  1110. }
  1111. sub updateNFOData($$$) {
  1112. my ($file, $season, $episode) = @_;
  1113. my $nfoData = undef();
  1114. {
  1115. open(my $fh, '<', $file)
  1116. or warn('Unable to open NFO for renumber: ' . $file . ': ' . $! . "\n");
  1117. $/ = undef;
  1118. $nfoData = <$fh>;
  1119. close($fh);
  1120. }
  1121. if ($nfoData) {
  1122. $nfoData =~ s/<season>\d+<\/season>/<season>${season}<\/season>/;
  1123. $nfoData =~ s/<episode>\d+<\/episode>/<episode>${episode}<\/episode>/;
  1124. }
  1125. return $nfoData;
  1126. }
  1127. sub videoNumberStr($$) {
  1128. my ($season, $episode) = @_;
  1129. my $str = '';
  1130. if ($season == 0 && $episode =~ /^(20[012]\d)(\d{2,4})$/) {
  1131. $str = sprintf('%04d%04d', $1, $2);
  1132. } else {
  1133. $str = sprintf('%02d', $episode);
  1134. }
  1135. return $str;
  1136. }
  1137. sub videoSE($$) {
  1138. my ($season, $episode) = @_;
  1139. return sprintf('S%0dE%s - ', $season, videoNumberStr($season, $episode));
  1140. }
  1141. sub videoPath($$$$) {
  1142. my ($season, $episode, $id, $ext) = @_;
  1143. return videoSE($season, $episode) . $id . '.' . $ext;
  1144. }
  1145. sub renameVideo($$$$$$) {
  1146. my ($video, $suffix, $nfo, $id, $season, $episode) = @_;
  1147. our $NAME;
  1148. # Bail if disabled
  1149. if ($NO_RENAME) {
  1150. print STDERR 'Not renaming: ' . $video . "\n";
  1151. return;
  1152. }
  1153. # General sanity checks
  1154. if (!defined($id) || !defined($season) || !defined($episode)) {
  1155. die($NAME . ": Invalid call to renameVideo()\n\t" . $video . "\n\t" . $suffix . "\n\t" . $nfo . "\n\t" . $id . "\n\t" . $season . "\n\t" . $episode . "\n");
  1156. }
  1157. # Warning about useless calls
  1158. if (!defined($nfo) && (!defined($video) || !defined($suffix))) {
  1159. warn("No NFO or video path provided in renameVideo()\n");
  1160. return;
  1161. }
  1162. # Video
  1163. if (defined($video) && defined($suffix)) {
  1164. my $videoNew = videoPath($season, $episode, $id, $suffix);
  1165. # Sanity checks, as we do dangerous work here
  1166. if (!-r $video) {
  1167. die($NAME . ': Invalid video source in rename: ' . $video . "\n");
  1168. }
  1169. if ($video ne $videoNew) {
  1170. print STDERR 'Renaming ' . $NAME . '/' . $video . ' => ' . $videoNew . "\n";
  1171. if (-e $videoNew) {
  1172. die($NAME . ': Rename: Target exists: ' . $videoNew . "\n");
  1173. }
  1174. if ($SUDO_CHATTR) {
  1175. system('sudo', $CHATTR, '-i', Cwd::abs_path($video));
  1176. }
  1177. rename($video, $videoNew)
  1178. or die($NAME . ': Unable to rename: ' . $video . ': ' . $! . "\n");
  1179. }
  1180. }
  1181. # NFO
  1182. if (defined($nfo)) {
  1183. my $nfoNew = videoPath($season, $episode, $id, 'nfo');
  1184. # Sanity checks, as we do dangerous work here
  1185. if (!-r $nfo) {
  1186. die($NAME . ': Invalid NFO source in rename: ' . $nfo . "\n");
  1187. }
  1188. # Parse old NFO data
  1189. my $nfoData = updateNFOData($nfo, $season, $episode)
  1190. or die($NAME . ': No NFO data found, refusing to rename: ' . $id . "\n");
  1191. # Write a new NFO and unlink the old one
  1192. if ($SUDO_CHATTR) {
  1193. system('sudo', $CHATTR, '-i', Cwd::abs_path($nfo));
  1194. }
  1195. saveString($nfoNew, $nfoData);
  1196. if ($nfo ne $nfoNew) {
  1197. print STDERR 'Renaming ' . $NAME . '/' . $nfo . ' => ' . $nfoNew . "\n";
  1198. unlink($nfo)
  1199. or warn('Unable to delete NFO during rename: ' . $! . "\n");
  1200. }
  1201. }
  1202. }
  1203. sub parseFilename($) {
  1204. my ($file) = @_;
  1205. return $file =~ /^S(\d+)+E(\d+) - ([\w\-]+)\.(\w\w\w)$/i;
  1206. }
  1207. # GET the requested URL, accepting encoded data and extracting the HTTP status code
  1208. sub fetch($$) {
  1209. my ($url, $data) = @_;
  1210. # Init the global LWP as needed
  1211. if (!defined($LWP)) {
  1212. $LWP = new LWP::UserAgent;
  1213. $LWP->agent($HTTP_UA);
  1214. $LWP->timeout($HTTP_TIMEOUT);
  1215. $LWP->ssl_opts({ 'verify_hostname' => $HTTP_VERIFY });
  1216. }
  1217. # Make a request
  1218. my $request = HTTP::Request->new('GET' => $url);
  1219. $request->header('Accept-Encoding' => HTTP::Message::decodable);
  1220. my $response = $LWP->request($request);
  1221. # Do something useful with the response
  1222. my $code = 400;
  1223. if ($response->status_line =~ /^(\d+)\s/) {
  1224. $code = $1;
  1225. }
  1226. ${$data} = $response->decoded_content();
  1227. return $code;
  1228. }