Class: Addic7edDownloader::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/addic7ed_downloader/search.rb

Constant Summary collapse

BASE_URL =
'http://www.addic7ed.com'.freeze
DEFAULTS =
{
  lang: 'en',
  tags: []
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(showname, season, episode, options = {}) ⇒ Search

Returns a new instance of Search.



26
27
28
29
30
31
32
33
34
# File 'lib/addic7ed_downloader/search.rb', line 26

def initialize(showname, season, episode, options = {})
  # Replace dots with spaces only if not followed by a space (Mr. Robot)
  @showname = showname.gsub(/\.(\S)/, ' \1')
  @season = season.to_i
  @episode = episode.to_i

  opts = DEFAULTS.merge(options)
  @lang, @tags = opts.values_at(:lang, :tags)
end

Instance Attribute Details

#episodeObject

Returns the value of attribute episode.



12
13
14
# File 'lib/addic7ed_downloader/search.rb', line 12

def episode
  @episode
end

#langObject

Returns the value of attribute lang.



12
13
14
# File 'lib/addic7ed_downloader/search.rb', line 12

def lang
  @lang
end

#pathObject

Returns the value of attribute path.



12
13
14
# File 'lib/addic7ed_downloader/search.rb', line 12

def path
  @path
end

#seasonObject

Returns the value of attribute season.



12
13
14
# File 'lib/addic7ed_downloader/search.rb', line 12

def season
  @season
end

#shownameObject

Returns the value of attribute showname.



12
13
14
# File 'lib/addic7ed_downloader/search.rb', line 12

def showname
  @showname
end

#tagsObject

Returns the value of attribute tags.



12
13
14
# File 'lib/addic7ed_downloader/search.rb', line 12

def tags
  @tags
end

Class Method Details

.by_filename(filename, options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/addic7ed_downloader/search.rb', line 14

def self.by_filename(filename, options = {})
  showname = filename[SHOWNAME_REGEXP, :showname].strip
  season   = filename[SEASON_EPISODE_REGEXP, :season].to_i
  episode  = filename[SEASON_EPISODE_REGEXP, :episode].to_i
  options[:tags] = filename[TAGS_REGEXP, :tags].split(TAGS_FILTER_REGEXP)

  new(showname, season, episode, options)
rescue NoMethodError
  # Failed to parse
  nil
end

Instance Method Details

#download_best(path = './') ⇒ Object



62
63
64
# File 'lib/addic7ed_downloader/search.rb', line 62

def download_best(path = './')
  download_subtitle(find_best_subtitle, path)
end

#download_subtitle(subtitle, path = './') ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/addic7ed_downloader/search.rb', line 66

def download_subtitle(subtitle, path = './')
  return unless subtitle

  # Addic7ed needs the correct Referer to be set
  response = HTTParty.get(
    BASE_URL + subtitle.url,
    headers: { 'Referer' => url, 'User-Agent' => USER_AGENTS.sample }
  )

  raise unless response.headers['content-type'].include? 'text/srt'

  # Get file name from headers
  filename = response.headers['content-disposition'][/filename=\"(.+?)\"/, 1]
  open(File.join(path, filename), 'w') { |f| f << response }

  # return subtitle filename
  filename
end

#extract_tags(filename) ⇒ Object



40
41
42
# File 'lib/addic7ed_downloader/search.rb', line 40

def extract_tags(filename)
  @tags = filename[TAGS_REGEXP, :tags].split(TAGS_FILTER_REGEXP) if filename
end

#find_best_subtitleObject



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/addic7ed_downloader/search.rb', line 48

def find_best_subtitle
  return if results.empty?

  # We can refine the search with tags
  unless @tags.empty?
    results.each do |subtitle|
      return subtitle if @tags.any? { |tag| subtitle.works_with?(tag) }
    end
  end

  # If no matches, return most downloaded
  results.first
end

#resultsObject



44
45
46
# File 'lib/addic7ed_downloader/search.rb', line 44

def results
  @results ||= build_subtitles_list
end

#to_sObject



36
37
38
# File 'lib/addic7ed_downloader/search.rb', line 36

def to_s
  "#{@showname.split.map(&:capitalize).join(' ')} #{sprintf('%02dx%02d', @season, @episode)}"
end