renameFromDisk.pl 4.2 KB

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