Class: Suby::Downloader::OpenSubtitles
- Inherits:
-
Suby::Downloader
- Object
- Suby::Downloader
- Suby::Downloader::OpenSubtitles
- Defined in:
- lib/suby/downloader/opensubtitles.rb
Overview
Constant Summary collapse
- SITE =
'dl.opensubtitles.org'
- FORMAT =
:gz
- XMLRPC_HOST =
'api.opensubtitles.org'
- XMLRPC_PATH =
'/xml-rpc'
- SUBTITLE_TYPES =
[:tvshow, :movie, :unknown]
- USERNAME =
''
- PASSWORD =
''
- LOGIN_LANGUAGE =
'eng'
- USER_AGENT =
'Suby v0.4'
- SEARCH_QUERIES_ORDER =
[:hash, :name]
- LANG_MAPPING =
OpenSubtitles needs ISO 639-22B language codes for subtitles search See www.opensubtitles.org/addons/export_languages.php and en.wikipedia.org/wiki/List_of_ISO_639-2_codes
{ ar: "ara", bg: "bul", bn: "ben", br: "bre", bs: "bos", ca: "cat", cs: "cze", da: "dan", de: "ger", el: "ell", en: "eng", eo: "epo", es: "spa", et: "est", eu: "baq", fa: "per", fi: "fin", fr: "fre", gl: "glg", he: "heb", hi: "hin", hr: "hrv", hu: "hun", hy: "arm", id: "ind", is: "ice", it: "ita", ja: "jpn", ka: "geo", kk: "kaz", km: "khm", ko: "kor", lb: "ltz", lt: "lit", lv: "lav", mk: "mac", mn: "mon", ms: "may", nl: "dut", no: "nor", oc: "oci", pb: "pob", pl: "pol", pt: "por", ro: "rum", ru: "rus", si: "sin", sk: "slo", sl: "slv", sq: "alb", sr: "scc", sv: "swe", sw: "swa", th: "tha", tl: "tgl", tr: "tur", uk: "ukr", ur: "urd", vi: "vie", zh: "chi" }
Constants inherited from Suby::Downloader
Instance Attribute Summary
Attributes inherited from Suby::Downloader
#episode, #file, #lang, #season, #show, #video_data
Instance Method Summary collapse
- #download_url ⇒ Object
- #language(lang) ⇒ Object
- #login ⇒ Object
- #search_query(type = :hash) ⇒ Object
- #search_query_by_hash ⇒ Object
- #search_query_by_imdbid ⇒ Object
- #search_query_by_name ⇒ Object
- #search_subtitles(query) ⇒ Object
- #success_message ⇒ Object
- #token ⇒ Object
Methods inherited from Suby::Downloader
#convert_to_utf8_from_latin1, #download, #encode, #extract, #find_nfo_file, #get, #get_redirection, #http, #imdbid, inherited, #initialize, #post, #sub_extension, #sub_name, #subtitles, #support_video_type?, #to_s, #xmlrpc
Constructor Details
This class inherits a constructor from Suby::Downloader
Instance Method Details
#download_url ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/suby/downloader/opensubtitles.rb', line 31 def download_url SEARCH_QUERIES_ORDER.find(lambda { raise NotFoundError, "no subtitles available" }) { |type| if subs = search_subtitles(search_query(type))['data'] @type = type break subs end }.first['SubDownloadLink'] end |
#language(lang) ⇒ Object
76 77 78 |
# File 'lib/suby/downloader/opensubtitles.rb', line 76 def language(lang) LANG_MAPPING[lang.to_sym] end |
#login ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/suby/downloader/opensubtitles.rb', line 50 def login response = xmlrpc.call('LogIn', USERNAME, PASSWORD, LOGIN_LANGUAGE, USER_AGENT) unless response['status'] == '200 OK' raise DownloaderError, "Failed to login with #{USERNAME}:#{PASSWORD}. " + "Server return code: #{response['status']}" end response['token'] end |
#search_query(type = :hash) ⇒ Object
59 60 61 62 |
# File 'lib/suby/downloader/opensubtitles.rb', line 59 def search_query(type = :hash) return nil unless query = send("search_query_by_#{type}") query.merge(sublanguageid: language(lang)) end |
#search_query_by_hash ⇒ Object
64 65 66 |
# File 'lib/suby/downloader/opensubtitles.rb', line 64 def search_query_by_hash { moviehash: MovieHasher.compute_hash(file), moviebytesize: file.size.to_s } if file.exist? end |
#search_query_by_imdbid ⇒ Object
72 73 74 |
# File 'lib/suby/downloader/opensubtitles.rb', line 72 def search_query_by_imdbid { imdbid: imdbid } if imdbid end |
#search_query_by_name ⇒ Object
68 69 70 |
# File 'lib/suby/downloader/opensubtitles.rb', line 68 def search_query_by_name season && episode ? { query: show, season: season, episode: episode } : { query: file.base.to_s } end |
#search_subtitles(query) ⇒ Object
40 41 42 43 44 |
# File 'lib/suby/downloader/opensubtitles.rb', line 40 def search_subtitles(query) return {} unless query query = [query] unless query.kind_of? Array xmlrpc.call('SearchSubtitles', token, query) end |
#success_message ⇒ Object
80 81 82 |
# File 'lib/suby/downloader/opensubtitles.rb', line 80 def "Found by #{@type}" end |
#token ⇒ Object
46 47 48 |
# File 'lib/suby/downloader/opensubtitles.rb', line 46 def token @token ||= login end |