module Cinema
Constants
- APP_ID
- APP_SECRET
- VERSION
Public Class Methods
ask_and_play()
click to toggle source
# File lib/cinema.rb, line 39 def ask_and_play watchlist = get_watchlist sorting_options = get_sorting_options(watchlist.first) sort_getter = select("Order movie list by", sorting_options, &:first).last imdb_id = select("Select movie", watchlist.sort_by{|x| sort_getter.(x)}){ |x| "#{sort_getter.(x)}: #{x["movie"]["title"]}" }["movie"]["ids"]["imdb"] torrent = select("Select quality", torrents(imdb_id)){|x| x["quality"] }["url"] downloader = select("Select downloader", ["peerflix", "qbittorrent", "wget", "echo"]) run_torrent(downloader, torrent) end
capture_stderr() { || ... }
click to toggle source
# File lib/cinema.rb, line 176 def capture_stderr backup_stderr = STDERR.dup begin Tempfile.open("captured_stderr") do |f| STDERR.reopen(f) yield f.rewind f.read end ensure STDERR.reopen backup_stderr end end
config()
click to toggle source
# File lib/cinema.rb, line 155 def config unless File.exist? config_file initial_authorize_and_save_config end YAML.load_file config_file end
config_file()
click to toggle source
# File lib/cinema.rb, line 162 def config_file "#{ENV['HOME']}/.config/cinema.yml" end
get_sorting_options(sample)
click to toggle source
# File lib/cinema.rb, line 14 def get_sorting_options(sample) sample.reduce([]) do |result, (k,v)| if v.is_a? Hash child_sorting_options = get_sorting_options(v).map do |name, accessor| ["#{k} #{name}", ->(x){ accessor.(x[k]) }] end result.concat child_sorting_options else result << [k.to_s, ->(x){ x[k] }] end end end
get_watchlist()
click to toggle source
# File lib/cinema.rb, line 68 def get_watchlist with_unreliable_api do puts "Requesting watchlist..." trakt_request(:get, "sync/watchlist/movies") end end
pin_to_token(pin)
click to toggle source
# File lib/cinema.rb, line 91 def pin_to_token(pin) response = RestClient.post('https://trakt.tv/oauth/token', { code: pin, client_id: APP_ID, client_secret: APP_SECRET, redirect_uri: redirect_uri, grant_type: 'authorization_code' }).body JSON.parse(response).values_at('access_token', 'refresh_token') end
redirect_uri()
click to toggle source
# File lib/cinema.rb, line 87 def redirect_uri 'urn:ietf:wg:oauth:2.0:oob' end
refresh_access_token()
click to toggle source
# File lib/cinema.rb, line 124 def refresh_access_token response = RestClient.post('https://trakt.tv/oauth/token', { refresh_token: config[:refresh_token], client_id: APP_ID, client_secret: APP_SECRET, redirect_uri: redirect_uri, grant_type: 'refresh_token' }).body write_config *JSON.parse(response).values_at('access_token', 'refresh_token') end
run_torrent(downloader, torrent)
click to toggle source
# File lib/cinema.rb, line 27 def run_torrent(downloader, torrent) case downloader when 'peerflix' player = select("Select player", ["mplayer","vlc"]) system downloader, torrent, "--#{player}" when 'qbittorrent' system "sh", "-c", "#{downloader} #{torrent} &" else system downloader, torrent end end
select(title, items, &title_proc)
click to toggle source
# File lib/cinema.rb, line 53 def select(title, items, &title_proc) menu_items = items.each_with_index.map do |x,i| [i.to_s, (title_proc ? title_proc.(x) : x)] end index = capture_stderr do success = system 'dialog', '--title', title, '--menu', '', '0', '0', '0', *menu_items.flatten unless success exit 0 end end items[index.to_i] if index end
torrents(imdb_id)
click to toggle source
# File lib/cinema.rb, line 75 def torrents(imdb_id) with_unreliable_api do puts "Searching torrents..." response = RestClient.get("http://yts.ag/api/v2/list_movies.json?query_term=#{imdb_id}").body JSON.parse(response)["data"]["movies"].first["torrents"] end end
trakt_request(method, path, payload=nil)
click to toggle source
# File lib/cinema.rb, line 136 def trakt_request(method, path, payload=nil) params = { method: method, url: "https://api-v2launch.trakt.tv/#{path}", payload: payload, headers: { "Content-Type" => "application/json", "Authorization" => "Bearer #{config[:access_token]}", "trakt-api-version" => "2", "trakt-api-key" => APP_ID, } } JSON.parse RestClient::Request.execute(params).body rescue RestClient::Unauthorized puts 'Unauthorized. Trying to refresh token.' refresh_access_token retry end
with_unreliable_api(delay=3) { || ... }
click to toggle source
# File lib/cinema.rb, line 166 def with_unreliable_api(delay=3) begin yield rescue puts "Request failed. Retrying in #{delay} seconds..." sleep delay retry end end
write_config(access_token, refresh_token)
click to toggle source
# File lib/cinema.rb, line 116 def write_config(access_token, refresh_token) File.write config_file, {access_token: access_token, refresh_token: refresh_token}.to_yaml end
yify()
click to toggle source
# File lib/cinema.rb, line 83 def yify @yify ||= Yify::Client.new end