-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlegenda_downloader.rb
113 lines (105 loc) · 3.34 KB
/
legenda_downloader.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#CONFIGURE
# Voce não precisa preencher os campos abaixo se tiver o arquivo ltv-account.cfg ;)
#
USUARIO = 'user'
SENHA = 'senha'
# Uso : ruby legenda_downloader.rb "The Matrix"
require 'net/http'
require 'uri'
class LegendaDownloader
def initialize
if File.exists?('ltv-account.cfg')
File.open('ltv-account.cfg').read.each_line do |line|
line = line.split('=')
if line[0] == 'username' then @login = line[1] end
if line[0] == 'password' then @senha = line[1] end
end
else
@login = USUARIO
@senha = SENHA
end
end
def login
if $standalone then puts 'Logando no site' end
url = URI.parse('http://legendas.tv/login_verificar.php')
req = Net::HTTP::Post.new(url.path)
req.set_form_data({'txtLogin'=>@login,'txtSenha'=>@senha})
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
case res
when Net::HTTPSuccess, Net::HTTPRedirection
cookie = res.response['set-cookie']
@cookie = cookie.split(';')[0]
else
res.error!
end
end
def buscar(searchParameters)
if $standalone then puts 'Buscando no site' end
url = URI.parse('http://legendas.tv/index.php?opcao=buscarlegenda')
req = Net::HTTP::Post.new('/index.php?opcao=buscarlegenda',@headers)
req.set_form_data({'txtLegenda'=>searchParameters,'btn_buscar'=>['x'=>0,'y'=>0],'selTipo'=>1,'int_idioma'=>1})
req['Cookie'] = @cookie
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
case res
when Net::HTTPSuccess, Net::HTTPRedirection
lista_de_filmes = Hash.new
res.body.each_line do |line|
if line.match(/abredown/) then
nome_do_release = line.split(',')[2]
id_para_download = line.split('abredown(')[1].split('\'')[1]
lista_de_filmes[nome_do_release] = id_para_download
end
end
lista_de_filmes
else
raise res.error!
end
end
def get_download_link(hash)
if $standalone then puts "Legenda sendo baixada" end
path= "/info.php?d=#{hash}&c=1"
url = URI.parse('http://legendas.tv' << path)
req = Net::HTTP::Get.new(path)
req['Cookie'] = @cookie
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
case res
when Net::HTTPSuccess, Net::HTTPRedirection
link = res['location']
return '/' << link
else
raise res.error!
end
end
def download(hash)
link = get_download_link(hash)
filename = link.split('/')
filename = filename[2]
url = URI.parse('http://legendas.tv' << link)
req = Net::HTTP::Get.new(link)
req['Cookie'] = @cookie
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
case res
when Net::HTTPSuccess, Net::HTTPRedirection
file = File.open(filename,'wb')
file.write(res.body)
file.close
if $standalone then puts "Legenda Salva em #{filename}" end
return res
else
raise res.error!
end
end
def self.legenda_for(filme)
#Pega a primeira legenda encontrada e baixa =P I'm Feeling Lucky.
baixador = LegendaDownloader.new
baixador.login
filme = baixador.buscar(filme).to_a[0]
hash_do_filme = filme[1]
baixador.download(hash_do_filme)
end
end
if ARGV.size != 0
$standalone = true
puts "Buscando Legenda para #{ARGV[0]}"
LegendaDownloader.legenda_for(ARGV[0])
end