Class: Addic7edDownloader::Search
- Inherits:
-
Object
- Object
- Addic7edDownloader::Search
- 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
-
#episode ⇒ Object
Returns the value of attribute episode.
-
#lang ⇒ Object
Returns the value of attribute lang.
-
#path ⇒ Object
Returns the value of attribute path.
-
#season ⇒ Object
Returns the value of attribute season.
-
#showname ⇒ Object
Returns the value of attribute showname.
-
#tags ⇒ Object
Returns the value of attribute tags.
Class Method Summary collapse
Instance Method Summary collapse
- #download_best(path = './') ⇒ Object
- #download_subtitle(subtitle, path = './') ⇒ Object
- #extract_tags(filename) ⇒ Object
- #find_best_subtitle ⇒ Object
-
#initialize(showname, season, episode, options = {}) ⇒ Search
constructor
A new instance of Search.
- #results ⇒ Object
- #to_s ⇒ Object
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, = {}) # 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() @lang, = opts.values_at(:lang, :tags) end |
Instance Attribute Details
#episode ⇒ Object
Returns the value of attribute episode.
12 13 14 |
# File 'lib/addic7ed_downloader/search.rb', line 12 def episode @episode end |
#lang ⇒ Object
Returns the value of attribute lang.
12 13 14 |
# File 'lib/addic7ed_downloader/search.rb', line 12 def lang @lang end |
#path ⇒ Object
Returns the value of attribute path.
12 13 14 |
# File 'lib/addic7ed_downloader/search.rb', line 12 def path @path end |
#season ⇒ Object
Returns the value of attribute season.
12 13 14 |
# File 'lib/addic7ed_downloader/search.rb', line 12 def season @season end |
#showname ⇒ Object
Returns the value of attribute showname.
12 13 14 |
# File 'lib/addic7ed_downloader/search.rb', line 12 def showname @showname end |
#tags ⇒ Object
Returns the value of attribute tags.
12 13 14 |
# File 'lib/addic7ed_downloader/search.rb', line 12 def 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, = {}) showname = filename[SHOWNAME_REGEXP, :showname].strip season = filename[SEASON_EPISODE_REGEXP, :season].to_i episode = filename[SEASON_EPISODE_REGEXP, :episode].to_i [:tags] = filename[TAGS_REGEXP, :tags].split(TAGS_FILTER_REGEXP) new(showname, season, episode, ) 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 (filename) = filename[TAGS_REGEXP, :tags].split(TAGS_FILTER_REGEXP) if filename end |
#find_best_subtitle ⇒ Object
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 .empty? results.each do |subtitle| return subtitle if .any? { |tag| subtitle.works_with?(tag) } end end # If no matches, return most downloaded results.first end |
#results ⇒ Object
44 45 46 |
# File 'lib/addic7ed_downloader/search.rb', line 44 def results @results ||= build_subtitles_list end |
#to_s ⇒ Object
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 |