Class: Bathyscaphe::Addic7ed

Inherits:
Object
  • Object
show all
Defined in:
lib/bathyscaphe/addic7ed.rb

Overview

Class to interact with addic7ed.com

Exaple:

sub = Bathyscaphe::Addic7ed.new(tv_show, season, episode)
sub.save("path/to/save/subtitles")

Constant Summary collapse

HTTP_HOST =
"http://addic7ed.com"
HOST =
"www.addic7ed.com"

Instance Method Summary collapse

Constructor Details

#initialize(tv_show, season, episode) ⇒ Addic7ed

Returns a new instance of Addic7ed.



22
23
24
25
26
27
# File 'lib/bathyscaphe/addic7ed.rb', line 22

def initialize(tv_show, season, episode)
  @tv_show = tv_show
  @season = season
  @episode = episode
  @temp_file = download
end

Instance Method Details

#downloadObject

Fakes your identity as if you came directly from episode page (otherwise addic7ed will redirect you to this episode page) and downloads subtitles.

Returns object of TempFile with subtitles.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bathyscaphe/addic7ed.rb', line 33

def download

  uri = URI( download_link )
  path = uri.path.empty? ? "/" : uri.path

  headers = { "Host" => HOST,
              "Referer" => episode_link(:referer)
            }
  @temp_file = Tempfile.open("bathyscaphe_"+@tv_show+@season+@episode)
  begin
    Net::HTTP.start(uri.host, uri.port) { |http|
      resp = http.get(path, headers)
      @temp_file.write resp.body
      raise "Limit exceeded" if resp.code.to_s == "302"
    }
  rescue Exception => e
    puts e
  ensure
    @temp_file.close
  end

  @temp_file

end

Returns direct link to most updated subtitles with highest downloads counter



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/bathyscaphe/addic7ed.rb', line 70

def download_link

  html = Nokogiri::HTML( get_html )

  if html.text.empty?
    puts "\e[31m"+"We beliewe addic7ed don't have subtitles for your episode."+"\e[0m"
    puts "   Go check yourself:"
    puts "   #{search_link}"
    exit
  end

  if (search_result = html.xpath("//form[@action='/search.php']").children.xpath("./b")).any?
    if results = search_result.first.text.match(/(\d*) result.{0,1} found/)
      puts "\e[31m"+"Suddenly our bathyscaphe crashed into 'Search results page'"+"\e[0m"
      puts "   They've found #{results[1]} results matching your tv-show name. Go check yourself:"
      puts "   #{search_link}"
      exit
    end
  end

  subtitles = {}
  html.css(".tabel95 .newsDate").each do |td|
    if downloads = td.text.match(/\s(\d*)\sDownloads/i)
      done = false
      td.parent.parent.xpath("./tr/td/a[@class='buttonDownload']/@href").each do |link|
        if md = link.value.match(/updated/i)
          subtitles[downloads[1].to_i] = link.value
          done = true
        elsif link.value.match(/original/i) && done == false
          subtitles[downloads[1].to_i] = link.value
          done = true
        end
      end
    end
  end
 
  if subtitles.empty?
    puts "\e[31m"+"We didn't find your subtitles for some reason."+"\e[0m"
    puts "   Try to find them manually:"
    puts "   #{search_link}"
    exit
  end

  subtitles = subtitles.sort
  puts "\e[32m"+"Found subtitles with #{subtitles.last[0]} downloads: #{HTTP_HOST + subtitles.last[1]}"+"\e[0m"
  
  return HTTP_HOST + subtitles.last[1]
end

Returns properly generated link to the episode page.

Params

Takes symbol as an argument: :referer returns link for download method to fake identity. :lang returns link with english subtitles for defined episode.



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/bathyscaphe/addic7ed.rb', line 145

def episode_link(type = :lang)
  link = URI::escape("http://www.addic7ed.com/serie/#{@tv_show.gsub(" ", "_")}/#{@season}/#{@episode}/")
  link += case type
  when :referer
    "addic7ed"
  when :lang
    "1"
  else
    "1"
  end
end

#get_htmlObject

Returns Tempfile with html regarding your episode



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/bathyscaphe/addic7ed.rb', line 121

def get_html
  io_html = eat(episode_link(:lang))
rescue URI::InvalidURIError => e
  STDERR.puts "\e[31m"+"We generated url the wrong way. Shame on us."+"\e[0m"
  STDERR.puts e
  STDERR.puts episode_link(:lang)
  exit
rescue HTTPClient::BadResponseError => the_error
  STDERR.puts "\e[31m"+the_error+". Haven't seen it yet."+"\e[0m"
  STDERR.puts episode_link(:lang)
  exit
rescue OpenURI::HTTPError => the_error
  STDERR.puts "\e[31m"+"Server responded with funny status code #{the_error.io.status[0]}. Haven't seen it yet."+"\e[0m"
  STDERR.puts episode_link(:lang)
  exit
end

#save(local_path) ⇒ Object

Moves downloaded subtitles to local_path



61
62
63
64
65
# File 'lib/bathyscaphe/addic7ed.rb', line 61

def save local_path
  @temp_file ||= download
  FileUtils.mv(@temp_file.path, local_path)
  puts "\e[32m"+"We've downloaded them: #{local_path}"+"\e[0m"
end

Returns link to search results for your tv-show name



159
160
161
# File 'lib/bathyscaphe/addic7ed.rb', line 159

def search_link
  "http://www.addic7ed.com/search.php?search=#{URI::escape(@tv_show)}"
end