Last active
September 23, 2019 02:23
-
-
Save ericboehs/098df8bddfedeb934157d9069ff92755 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
require 'cgi' | |
require 'net/http' | |
require 'json' | |
# Adds a movie to put.io based on search term | |
class MovieDownloader | |
attr_reader :term | |
def initialize term | |
@term = term | |
end | |
def download | |
PutIo.add_transfer magnet | |
end | |
private | |
def magnet | |
yts_magnet || rarbg_magnet | |
end | |
def yts_magnet | |
YTS.new(term).magnet | |
end | |
def rarbg_magnet | |
Rarbg.new(term).magnet | |
end | |
end | |
# Searches https://yts.lt for movies | |
class YTS | |
attr_reader :term | |
def initialize term | |
@term = term | |
end | |
def magnet | |
raise "Status not ok: #{search_results['status']}" unless search_results['status'] == 'ok' | |
return unless search_results['data']['movie_count'].positive? | |
return unless movies.first | |
return unless torrent | |
show_torrent_info | |
generate_magnet | |
end | |
private | |
def show_torrent_info | |
puts "Downloading #{movie['title_long']} from YTS in #{torrent['quality']} (#{torrent['type']})." | |
puts "IMDB Rating: #{movie['rating']}" | |
puts "YTS URL: #{movie['url']}" | |
puts "Trailer: https://youtu.be/#{movie['yt_trailer_code']}" if movie['yt_trailer_code'] | |
end | |
def generate_magnet | |
[ | |
"magnet:?xt=urn:btih:#{torrent['hash']}", | |
"dn=#{CGI.escape movie['title_long']}", | |
"tr=udp%3A%2F%2Fglotorrents.pw%3A6969%2Fannounce", | |
"tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80", | |
"tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969", | |
"tr=udp%3A%2F%2Fp4p.arenabg.ch%3A1337", | |
"tr=udp%3A%2F%2Ftracker.internetwarriors.net%3A1337" | |
].join '&' | |
end | |
def torrent | |
movie['torrents'].find { |torrent| torrent['quality'] == '1080p' } | |
end | |
def movie | |
@movie ||= ( | |
return movies.first if movies.count == 1 | |
puts movies.map.with_index { |movie, i| | |
["\033[1m#{i}:\033[0m #{movie['title_long']} - #{movie['rating']} - #{movie['url']} - https://youtu.be/#{movie['yt_trailer_code']}"] | |
} | |
print "Which movie? [0] " | |
index = STDIN.gets.chomp.to_i | |
movies[index] | |
) | |
end | |
def movies | |
search_results['data']['movies'] | |
end | |
def search_results | |
@search_results ||= Http.get "https://yts.lt/api/v2/list_movies.json?quality=1080p&query_term=#{term.split.join '+'}&limit=50" | |
end | |
end | |
# Searches https://rarbg.org for movies | |
class Rarbg | |
attr_reader :term | |
def initialize term | |
@term = term | |
end | |
def magnet | |
`open "#{link}"` | |
puts "Opening #{link} for manual torrent retrieval." | |
prompt_for_clipboard_retrieval | |
end | |
private | |
def link | |
"https://rarbg.to/torrents.php?category=44&search=#{term.split.join '+'}&order=seeders&by=DESC" | |
end | |
def prompt_for_clipboard_retrieval | |
print "\nDownload URL on clipboard? [y] " | |
IO.popen('pbpaste', 'r+').read unless STDIN.gets.chomp == 'n' | |
end | |
end | |
# Adds magnet or torrent URL to https://put.io for download | |
class PutIo | |
def self.add_transfer url | |
uri = URI 'https://api.put.io/v2/transfers/add' | |
header = {'Authorization': "Bearer #{ENV.fetch 'PUTIO_APP_TOKEN'}"} | |
Net::HTTP.post uri, "url=#{url}", header | |
end | |
end | |
# HTTP helper method to fetch a url and parse the response as JSON | |
class Http | |
def self.get uri | |
uri = URI uri unless uri.is_a? URI | |
response = Net::HTTP.get uri | |
JSON.parse response | |
end | |
end | |
class Cli | |
def initialize | |
unless search_term | |
puts "Missing search term!\n\nSyntax: yif_downloader [search term]" | |
exit 1 | |
end | |
end | |
def perform | |
MovieDownloader.new(search_term).download | |
end | |
def search_term | |
ARGV[0] | |
end | |
end | |
Cli.new.perform |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment