plextvtool 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 373445d3db12d5ac5fac4b27b4dca542cfc8f8614ccd564fb3f2420b348ad3a1
4
+ data.tar.gz: c8e3b00c395047e852dc8a8c96dbe11fe09a8aea0261cbeb85372c7017d688ad
5
+ SHA512:
6
+ metadata.gz: ca1e07becd24b2c504fcef9a3d429bc15c5599b079c831c57af7fe7510a7a18eaa558b30a2897091ae67909feec99303d08cc84f7f322510a568def016e2cbf5
7
+ data.tar.gz: ceaa85681401f9de9eb9a31dc3122a46b2ba0e61c6ac7c3516db894bb4cc8d38f55e5d42b481827b591f3e1d9d573da89075043baee9437ee3804e6a554a2bf5
data/bin/plextvtool ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'thor'
5
+ require 'plextvtoolservice'
6
+
7
+ # CLI for plextvtool
8
+ class PlexTvTool < Thor
9
+ desc 'fix <DIR>', 'rename episodes in <DIR>'
10
+ options title: :required, season: :required
11
+ def fix(dir)
12
+ PlexTvToolService.new(dir, options).rename_files!
13
+ end
14
+
15
+ desc 'config', 'set configurable parameters for fetching data'
16
+ options key: :required
17
+ def config
18
+ loaded_config = PlexTvToolService.load_config
19
+ loaded_config['plextvt']['omdb']['api_key'] = options[:key]
20
+ PlexTvToolService.write_config! loaded_config
21
+ end
22
+ end
23
+ PlexTvTool.start(ARGV)
data/lib/config.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ plextvt:
3
+ omdb:
4
+ base_uri: https://www.omdbapi.com/
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'net/http'
5
+ require 'thor'
6
+ require 'yaml'
7
+
8
+ # Service for renaming tv show filenames in a way that is friendly with Plex
9
+ class PlexTvToolService
10
+ attr_reader :dir, :episodes, :title, :season
11
+
12
+ def initialize(dir, options)
13
+ @dir = dir
14
+ @title = options[:title]
15
+ @season = options[:season]
16
+ @episodes = fetch_episodes
17
+ end
18
+
19
+ def rename_files!
20
+ sort_and_rename!
21
+ rescue StandardError => e
22
+ puts e.message
23
+ puts e.backtrace.inspect
24
+ end
25
+
26
+ class << self
27
+ def load_config
28
+ config_path = File.join File.dirname(__FILE__), 'config.yml'
29
+ YAML.safe_load File.read(config_path)
30
+ end
31
+
32
+ def write_config!(config)
33
+ File.open(File.join(File.dirname(__FILE__), 'config.yml'), 'w') do |f|
34
+ YAML.dump config, f
35
+ end
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def fetch_episodes
42
+ api_key, base_uri = api_params
43
+ params = { t: title, Season: season, apikey: api_key }
44
+ uri = URI base_uri
45
+ uri.query = URI.encode_www_form params
46
+ response = Net::HTTP.get_response uri
47
+ raise 'Bad response from OMDB API' unless response.is_a? Net::HTTPSuccess
48
+
49
+ body = JSON.parse response.body
50
+ check_response body
51
+ body['Episodes']
52
+ end
53
+
54
+ def check_response(response)
55
+ return unless response['Response'] == 'False'
56
+
57
+ puts "Error response from OMDB API: #{response['Error']}"
58
+ exit 1
59
+ end
60
+
61
+ def api_params
62
+ loaded_config = self.class.load_config
63
+ [loaded_config.dig('plextvt', 'omdb', 'api_key'),
64
+ loaded_config.dig('plextvt', 'omdb', 'base_uri')]
65
+ end
66
+
67
+ def sort_and_rename!
68
+ files = Dir.glob("#{dir}/*").sort
69
+ files.zip(episodes).each do |file, episode|
70
+ new_name = format_new_name(file, episode)
71
+ print "Renaming #{File.basename(file)} to #{new_name} ... "
72
+ File.rename(file, "#{dir}/#{new_name}")
73
+ puts 'DONE'
74
+ end
75
+ end
76
+
77
+ def format_new_name(file, episode)
78
+ new_name = "#{title} s#{season.to_s.rjust(2, '0')}"\
79
+ "e#{episode['Episode'].rjust(2, '0')} #{episode['Title']}#{File.extname(file)}"
80
+ new_name.tr('<>:"/\\|?*', '') # Invalid chars for ntfs
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: plextvtool
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ian Wright
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-03-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ description: A tool for renaming TV media files for Plex Media Server
28
+ email: hello@ianwright.dev
29
+ executables:
30
+ - plextvtool
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - bin/plextvtool
35
+ - lib/config.yml
36
+ - lib/plextvtoolservice.rb
37
+ homepage: https://github.com/ns-ian/plextvtool
38
+ licenses:
39
+ - MIT
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 2.5.0
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.2.15
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: Plex TV Tool
60
+ test_files: []