subdl 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/subdl +3 -4
  3. data/lib/subdl.rb +39 -62
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 88749b74dbf27dfdba46db301bbeb2d302c8ee78
4
- data.tar.gz: f2e8bbc9698a7b8470e4e4b01478d31a9ab1a102
3
+ metadata.gz: d2db16138366c1df81aeaa2223014160c3d3d34c
4
+ data.tar.gz: c4766a193aab8bd65530651d666cb0aeb0ae3f5c
5
5
  SHA512:
6
- metadata.gz: af183442e79790255ef4f9b5703b2bf331f3a13ac9e459a63594dc8d11c67b82dab9746c81d2936f2ac7bbd91f802c2ffc9daa3696edce0272def174c940074d
7
- data.tar.gz: d166c4af0f2e0a98366ad5e52e173a84a500d5390da7047878a7fc4b8f62762eb43fab7589af37b844219588c83c853c0d2d1b8ade1500e6afa14fe7fb9092a0
6
+ metadata.gz: ecfbaf73ca2b4cd282b40d5f0599393cd6beeb8d35414439ffa83d2592b1849eb69f5c2636df0eb5068f521873805962c67c5e12b95ef3b81f6be7edac5f075f
7
+ data.tar.gz: fed07781bea4e3620b5cb5433cbdbfa2b6dbd20e2046ec073a36cdc13f07064f23ae39341dd358d3870d085a4a0df099f1beaf418326c3e7e3e76988de8e4305
data/bin/subdl CHANGED
@@ -1,10 +1,9 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'subdl'
3
3
 
4
- subdl = Subdl.new mechanize_agent,
5
- ItasaLoginForm.new,
4
+ subdl = Subdl.new(ItasaAgent.new,
5
+ SubtitlesNet.new,
6
6
  $stdout,
7
- FileSystem.new,
8
- FileReader.new
7
+ FileSystem.new)
9
8
 
10
9
  subdl.main ARGV
@@ -4,25 +4,20 @@ require 'json'
4
4
  require 'zipruby'
5
5
  require 'nokogiri'
6
6
 
7
+ require 'subdl/subtitles_net'
8
+ require 'subdl/italiansubs'
9
+
7
10
  class Subdl
8
- def initialize agent, itasa_login, stdout, file_system,
9
- file_reader
10
- itasa = Itasa.new(agent, itasa_login)
11
- credentials = Credentials.new file_reader
12
- @crawler = Crawler.new itasa, credentials, file_system, stdout
13
- end
14
11
  def main argv
15
12
  until argv.empty? do
16
- @crawler.download_sub_for argv.shift
13
+ download_sub_for argv.shift
17
14
  end
18
15
  end
19
- end
20
16
 
21
- class Crawler
22
-
23
- def initialize itasa, credentials, file_system, stdout
24
- @itasa = itasa
25
- @credentials = credentials
17
+ def initialize itasa, subtitles_net, stdout, file_system
18
+ @itasa = Itasa.new itasa
19
+ @subtitles_net = subtitles_net
20
+ @credentials = Credentials.new file_system
26
21
  @file_system = file_system
27
22
  @stdout = stdout
28
23
  end
@@ -31,22 +26,35 @@ class Crawler
31
26
  movie_file = MovieFile.new path, @stdout
32
27
  ids = @itasa.search_subtitles(movie_file.search_term)
33
28
 
34
- if not ids.any?
35
- @stdout.puts "No subtitles found on Itasa for: #{path}"
36
- end
37
-
38
- ids.each do |id|
39
- @itasa.login *@credentials.read
40
- @itasa.download_zip id do |zip_contents|
41
- unpack_subtitle_to zip_contents, movie_file
29
+ if ids.any?
30
+ ids.each do |id|
31
+ @itasa.login *@credentials.read
32
+ @itasa.download_zip id do |zip_contents|
33
+ unpack_subtitle_to zip_contents, movie_file, 'itasa'
34
+ end
42
35
  end
36
+ else
37
+ @stdout.puts "No subtitles found on ITASA for: #{path}"
38
+ show = movie_file.show
39
+ season = movie_file.season
40
+ episode = movie_file.episode
41
+
42
+ search_results = SearchResults.new(
43
+ @subtitles_net.search show, season, episode)
44
+
45
+ details_page_link = search_results.subtitles.first.href
46
+ page = @subtitles_net.get details_page_link
47
+ details_page = DetailsPage.new page
48
+
49
+ zip_contents = @subtitles_net.get details_page.zip_location
50
+ unpack_subtitle_to zip_contents, movie_file, 'subtitles-net'
43
51
  end
44
52
  end
45
53
 
46
- def unpack_subtitle_to zip_contents, movie_file
54
+ def unpack_subtitle_to zip_contents, movie_file, source_id
47
55
  Zip::Archive.open_buffer(zip_contents) do |archive|
48
56
  archive.each do |entry|
49
- movie_file.save_subtitle entry.read, @file_system
57
+ movie_file.save_subtitle entry.read, @file_system, source_id
50
58
  end
51
59
  end
52
60
  end
@@ -81,9 +89,10 @@ class MovieFile
81
89
  "%s %dx%02d" % [show, season, episode]
82
90
  end
83
91
 
84
- def save_subtitle contents, fs
92
+ # TODO: accept filename instead of source_id
93
+ def save_subtitle contents, fs, source_id
85
94
  srt_filename = @filename.gsub /.mp4$/, ''
86
- srt_filename += ".itasa#{next_distinguisher}.srt"
95
+ srt_filename += ".#{source_id}#{next_distinguisher}.srt"
87
96
  @stdout.puts "Downloaded as #{srt_filename}"
88
97
  fs.save_file srt_filename, contents
89
98
  end
@@ -105,45 +114,18 @@ class FileSystem
105
114
  f.write contents
106
115
  end
107
116
  end
108
- end
109
-
110
- def mechanize_agent
111
- Mechanize.new do |a|
112
- a.user_agent_alias = 'Mac FireFox'
113
- end
114
- end
115
-
116
- class ItasaLoginForm
117
-
118
- def login username, password, page_where_to_login, agent
119
- return if logged_in?
120
- home_page = agent.get page_where_to_login
121
- login_form = home_page.form 'login'
122
- login_form.username = username
123
- login_form.passwd = password
124
- @page = agent.submit(login_form)
125
- end
126
-
127
- private
128
-
129
- def logged_in?
130
- return false unless @page
131
- link_that_exists_only_once_logged = @page.search(
132
- "//a[@href='forum/index.php?action=unreadreplies']")
133
- return link_that_exists_only_once_logged.first != nil
117
+ def read_expand expandable_path
118
+ File.read(File.expand_path(expandable_path))
134
119
  end
135
-
136
-
137
120
  end
138
121
 
139
122
  class Itasa
140
- def initialize agent, login_form
123
+ def initialize agent
141
124
  @agent = agent
142
- @login_form = login_form || ItasaLoginForm.new
143
125
  end
144
126
 
145
127
  def login username, password
146
- @login_form.login username, password, "http://#{host}", @agent
128
+ @agent.login username, password
147
129
  end
148
130
 
149
131
  def search_subtitles text
@@ -201,12 +183,7 @@ class Credentials
201
183
  [username, password]
202
184
  end
203
185
  def read
204
- parse @file_reader.read_expand('~/.itasa-credentials')
186
+ @parsed ||= parse @file_reader.read_expand('~/.itasa-credentials')
205
187
  end
206
188
  end
207
189
 
208
- class FileReader
209
- def read_expand expandable_path
210
- File.read(File.expand_path(expandable_path))
211
- end
212
- end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: subdl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrea Francia