bathyscaphe 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -32,7 +32,23 @@ or just
32
32
  ### TODO
33
33
 
34
34
  1. Test regexp for matching more tv-show names
35
- 2. Maybe add other sources
35
+ 2. Write some tests
36
+ 3. Maybe add other sources
37
+
38
+ ### Changelog
39
+
40
+ #### 0.1.3
41
+ - RDoc + Refactoring
42
+
43
+ #### 0.1.2
44
+ - Some new exceptions handling
45
+ - LICENCE defined
46
+
47
+ #### 0.1.1
48
+ Readme chenged =)
49
+
50
+ #### 0.1.0
51
+ Initial release
36
52
 
37
53
  ### Authors
38
54
 
data/bin/bathyscaphe CHANGED
@@ -15,16 +15,17 @@ file_dir = File.expand_path(File.dirname(file_arg))
15
15
 
16
16
  begin
17
17
  tv_show = Bathyscaphe::TVDB.new(file_name)
18
-
19
- puts "TV Show: #{tv_show.name}"
20
- puts "Season: #{tv_show.season}"
21
- puts "Episode: #{tv_show.episode}"
22
-
23
18
  rescue Exception => e
24
- puts e
25
- exit 0
19
+ puts "\e[31m"+e+". Shame on us. \e[0m"
20
+ exit
26
21
  end
27
22
 
23
+ puts "\e[32m"+"--------"+"\e[0m"
24
+ puts "\e[32m"+"TV Show: #{tv_show.name}"+"\e[0m"
25
+ puts "\e[32m"+"Season: #{tv_show.season}"+"\e[0m"
26
+ puts "\e[32m"+"Episode: #{tv_show.episode}"+"\e[0m"
27
+ puts "\e[32m"+"--------"+"\e[0m"
28
+
28
29
  exit if Bathyscaphe::Config::OPTIONS[:dry_run]
29
30
 
30
31
  sub = Bathyscaphe::Addic7ed.new(tv_show.name, tv_show.season, tv_show.episode)
@@ -6,6 +6,13 @@ module Bathyscaphe
6
6
  require "tempfile"
7
7
  require "fileutils"
8
8
 
9
+ # Class to interact with http://addic7ed.com
10
+ #
11
+ # === Exaple:
12
+ #
13
+ # sub = Bathyscaphe::Addic7ed.new(tv_show, season, episode)
14
+ # sub.save("path/to/save/subtitles")
15
+ #
9
16
  class Addic7ed
10
17
 
11
18
  HTTP_HOST = "http://addic7ed.com"
@@ -18,13 +25,17 @@ module Bathyscaphe
18
25
  @temp_file = download
19
26
  end
20
27
 
28
+ # Fakes your identity as if you came directly from episode page (otherwise addic7ed will redirect you to this episode page) and downloads subtitles.
29
+ #
30
+ # Returns object of TempFile with subtitles.
31
+ #
21
32
  def download
22
33
 
23
- uri = URI(HTTP_HOST + sub_link)
34
+ uri = URI( download_link )
24
35
  path = uri.path.empty? ? "/" : uri.path
25
36
 
26
37
  headers = { "Host" => HOST,
27
- "Referer" => show_page(:referer)
38
+ "Referer" => episode_link(:referer)
28
39
  }
29
40
  @temp_file = Tempfile.open("bathyscaphe_"+@tv_show+@season+@episode)
30
41
  begin
@@ -38,37 +49,39 @@ module Bathyscaphe
38
49
  ensure
39
50
  @temp_file.close
40
51
  end
52
+
41
53
  @temp_file
54
+
42
55
  end
43
56
 
57
+
58
+ # Moves downloaded subtitles to local_path
59
+ #
44
60
  def save local_path
45
61
  @temp_file ||= download
46
62
  FileUtils.mv(@temp_file.path, local_path)
63
+ puts "\e[32m"+"We've downloaded them: #{local_path}"+"\e[0m"
47
64
  end
48
65
 
49
- def sub_link
50
- begin
51
- io_html = open(show_page(:lang))
52
- html = Nokogiri::HTML(io_html)
53
- rescue URI::InvalidURIError => e
54
- # STDERR.puts e
55
- puts "We think we didn't parse your TV-Show's name right (#{@tv_show}). Correct us:"
56
- name = STDIN.gets
57
- @tv_show = name.strip
58
- retry
59
- end
60
- if io_html.status[0] == "200" && html.text.empty?
61
- puts "We beliewe addic7ed don't have subtitles for your episode. Check yourself:"
62
- puts "http://www.addic7ed.com/search.php?search=#{URI::escape(@tv_show)}"
66
+
67
+ # Returns direct link to most updated subtitles with highest downloads counter
68
+ #
69
+ def download_link
70
+
71
+ html = Nokogiri::HTML( get_html )
72
+
73
+ if html.text.empty?
74
+ puts "\e[31m"+"We beliewe addic7ed don't have subtitles for your episode."+"\e[0m"
75
+ puts " Go check yourself:"
76
+ puts " #{search_link}"
63
77
  exit
64
78
  end
65
79
 
66
- search_result = html.xpath("//form[@action='/search.php']").children.xpath("./b")
67
- if search_result.any?
80
+ if (search_result = html.xpath("//form[@action='/search.php']").children.xpath("./b")).any?
68
81
  if results = search_result.first.text.match(/(\d*) result.{0,1} found/)
69
- puts "Suddenly our bathyscaphe crashed into 'Search results page'"
70
- puts "They've found #{results[1]} results. Go check yourself:"
71
- puts "http://www.addic7ed.com/search.php?search=#{URI::escape(@tv_show)}"
82
+ puts "\e[31m"+"Suddenly our bathyscaphe crashed into 'Search results page'"+"\e[0m"
83
+ puts " They've found #{results[1]} results matching your tv-show name. Go check yourself:"
84
+ puts " #{search_link}"
72
85
  exit
73
86
  end
74
87
  end
@@ -88,28 +101,57 @@ module Bathyscaphe
88
101
  end
89
102
  end
90
103
  end
91
-
92
- subtitles = subtitles.sort
104
+
93
105
  if subtitles.empty?
94
- puts "We didn't find your subtitles for some reason."
95
- puts "Try to find them manually:"
96
- puts "http://www.addic7ed.com/search.php?search=#{URI::escape(@tv_show)}"
97
- puts show_page(:lang)
106
+ puts "\e[31m"+"We didn't find your subtitles for some reason."+"\e[0m"
107
+ puts " Try to find them manually:"
108
+ puts " #{search_link}"
98
109
  exit
99
110
  end
100
- puts "Found subtitles with #{subtitles.last[0]} downloads: http://www.addic7ed.com#{subtitles.last[1]}"
101
- subtitles.last[1]
111
+
112
+ subtitles = subtitles.sort
113
+ puts "\e[32m"+"Found subtitles with #{subtitles.last[0]} downloads: #{HTTP_HOST + subtitles.last[1]}"+"\e[0m"
114
+
115
+ return HTTP_HOST + subtitles.last[1]
116
+ end
117
+
118
+ # Returns Tempfile with html regarding your episode
119
+ #
120
+ def get_html
121
+ io_html = open(episode_link(:lang))
122
+ rescue URI::InvalidURIError => e
123
+ STDERR.puts "\e[31m"+"We generated url the wrong way. Shame on us."+"\e[0m"
124
+ STDERR.puts e
125
+ exit
126
+ rescue OpenURI::HTTPError => the_error
127
+ STDERR.puts "\e[31m"+"Server responded with funny status code #{the_error.io.status[0]}. Haven't seen it yet."+"\e[0m"
128
+ exit
102
129
  end
103
130
 
104
- def show_page(type)
105
- link = URI::escape("http://www.addic7ed.com/serie/#{@tv_show}/#{@season}/#{@episode}/")
131
+ # Returns properly generated link to the episode page.
132
+ #
133
+ # === Params
134
+ # Takes symbol as an argument:
135
+ # *:referer* returns link for download method to fake identity.
136
+ # *:lang* returns link with english subtitles for defined episode.
137
+ #
138
+ def episode_link(type = :lang)
139
+ link = URI::escape("http://www.addic7ed.com/serie/#{@tv_show.gsub(" ", "_")}/#{@season}/#{@episode}/")
106
140
  link += case type
107
141
  when :referer
108
142
  "addic7ed"
109
143
  when :lang
110
144
  "1"
145
+ else
146
+ "1"
111
147
  end
112
148
  end
113
149
 
150
+ # Returns link to search results for your tv-show name
151
+ #
152
+ def search_link
153
+ "http://www.addic7ed.com/search.php?search=#{URI::escape(@tv_show)}"
154
+ end
155
+
114
156
  end
115
157
  end
@@ -1,3 +1,3 @@
1
1
  module Bathyscaphe
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 2
9
- version: 0.1.2
8
+ - 3
9
+ version: 0.1.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ilia Zemskov