renameFromDisk.pl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. # Includes
  5. use URI::Escape;
  6. use XML::Simple;
  7. use LWP::Simple;
  8. use File::Basename;
  9. use HTTP::Request::Common;
  10. # Debug
  11. my $DEBUG = 0;
  12. if ($ENV{'DEBUG'}) {
  13. $DEBUG = 1;
  14. }
  15. # Parameters
  16. my ($host, $user, $pass, $section, $series) = @ARGV;
  17. if (!$host || !$user || !$pass || (!$section && !$series)) {
  18. die('Usage: ' . basename($0) . ' host[:port] user passwd section_number [series_metadata_number]');
  19. }
  20. # Globals
  21. my $baseURL = 'http://' . $host;
  22. my $auth = 'X-Plex-User=' . $user . '&X-Plex-Pass=' . $pass;
  23. my $xml = new XML::Simple;
  24. my @shows = ();
  25. if ($series) {
  26. # Fetch just the provided show
  27. print STDERR 'Fetching show ' . $series . "\n";
  28. @shows = ('/library/metadata/' . $series . '/children');
  29. } else {
  30. # Fetch the list of all shows in the section
  31. print STDERR "Fetching shows...\n";
  32. {
  33. my $content = get($baseURL . '/library/sections/' . $section . '/all/?' . $auth);
  34. if ($DEBUG) {
  35. print STDERR $baseURL . '/library/sections/' . $section . '/all/?' . $auth . "\n";
  36. }
  37. if (!$content) {
  38. die(basename($0) . ': Unable to fetch data for section: ' . $section . "\n");
  39. }
  40. # Find all TV shows in the section
  41. {
  42. my $tree = $xml->XMLin($content);
  43. @shows = keys(%{ $tree->{'Directory'} });
  44. }
  45. }
  46. print STDERR 'Found ' . scalar(@shows) . " shows\n";
  47. }
  48. # Fetch the list of all seasons in each show
  49. my @seasons = ();
  50. print STDERR "Fetching seasons...\n";
  51. foreach my $show (@shows) {
  52. my $content = get($baseURL . $show . '?' . $auth);
  53. if ($DEBUG) {
  54. print STDERR $baseURL . $show . '?' . $auth . "\n";
  55. }
  56. if (!$content) {
  57. warn(basename($0) . ': Could not fetch seasons for show: ' . $show . "\n");
  58. next;
  59. }
  60. # Find all seasons in the show
  61. {
  62. my $tree = $xml->XMLin($content);
  63. # XML::Simple retuns different structure depending on the number of same-named child elements
  64. if ($tree->{'Directory'}->{'key'}) {
  65. push(@seasons, $tree->{'Directory'}->{'key'});
  66. } else {
  67. push(@seasons, keys(%{ $tree->{'Directory'} }));
  68. }
  69. }
  70. }
  71. print STDERR 'Found ' . scalar(@seasons) . " seasons\n";
  72. # Forget the list of shows
  73. undef(@shows);
  74. # Fetch the list of all episodes in each season
  75. my @episodes = ();
  76. print STDERR "Fetching episodes...\n";
  77. foreach my $season (@seasons) {
  78. my $content = get($baseURL . $season . '?' . $auth);
  79. if ($DEBUG) {
  80. print STDERR $baseURL . $season . '?' . $auth . "\n";
  81. }
  82. if (!$content) {
  83. warn(basename($0) . ': Could not fetch episodes for season: ' . $season . "\n");
  84. next;
  85. }
  86. # Find all episodes in the season
  87. {
  88. my $tree = $xml->XMLin($content);
  89. # XML::Simple retuns different structure depending on the number of same-named child elements
  90. if ($tree->{'Video'}->{'key'}) {
  91. push(@episodes, $tree->{'Video'}->{'key'});
  92. } else {
  93. push(@episodes, keys(%{ $tree->{'Video'} }));
  94. }
  95. }
  96. }
  97. print STDERR 'Found ' . scalar(@episodes) . " episodes\n";
  98. # Forget the list of seasons
  99. undef(@seasons);
  100. # Fetch the metadata from each episode
  101. print STDERR "Checking episodes...\n";
  102. foreach my $episode (@episodes) {
  103. my $content = get($baseURL . $episode . '?' . $auth);
  104. if ($DEBUG) {
  105. print STDERR $baseURL . $episode . '?' . $auth . "\n";
  106. }
  107. if (!$content) {
  108. warn(basename($0) . ': Could not fetch metadata for episode: ' . $episode . "\n");
  109. next;
  110. }
  111. # Parse the metadata
  112. {
  113. my $tree = $xml->XMLin($content);
  114. my $title = $tree->{'Video'}->{'title'};
  115. my $summary = $tree->{'Video'}->{'summary'};
  116. my $file = $tree->{'Video'}->{'Media'}->{'Part'}->{'file'};
  117. if (!$title) {
  118. warn('Invalid title for episode: ' . $episode . "\n");
  119. next;
  120. }
  121. if (!$file) {
  122. if (!$tree->{'Video'}->{'Media'}->{'key'}) {
  123. warn('No support for multi-file episodes: ' . $episode . "\n");
  124. } else {
  125. warn('Invalid file for episode: ' . $episode . "\n");
  126. }
  127. next;
  128. }
  129. # Rename if the file has a valid name but the episode does not
  130. $file = uri_unescape($file);
  131. $file = basename($file);
  132. $file =~ s/^\d+\s+\-\s*//;
  133. $file =~ s/S\d+E\d+\s+\-\s*//i;
  134. $file =~ s/\.\w{2,4}//;
  135. if (!$file || length($file) < 2 || $file =~ /NoName/i || $file =~ /Episode\s+\d+/i) {
  136. next;
  137. }
  138. if ($file =~ /\s+S\d+D\d+\-\d+$/) {
  139. print STDERR 'Skipping encoder-named file: ' . $file . "\n";
  140. }
  141. if ($title =~ /^Episode\s+\d+$/) {
  142. my $ua = LWP::UserAgent->new();
  143. my $encoded = uri_escape($file);
  144. print STDERR 'Renaming ' . $episode . ' from "' . $title . '" to "' . $file . '"' . "\n";
  145. my $url = $baseURL . $episode . '?title=' . $encoded . '&titleSort=' . $encoded . '&title.locked=1&titleSort.locked=1&' . $auth;
  146. my $request = HTTP::Request->new('PUT' => $url);
  147. $ua->request($request);
  148. if (!$summary) {
  149. print STDERR "\tAlso setting summary\n";
  150. $url = $baseURL . $episode . '?title=' . $encoded . '&summary=' . $encoded . '&summary.locked=1&' . $auth;
  151. $request = HTTP::Request->new('PUT' => $url);
  152. $ua->request($request);
  153. }
  154. }
  155. }
  156. }