subscribe.pl 33 KB

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