subscribe.pl 33 KB

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