Skip to content

Instantly share code, notes, and snippets.

@sandrods
Created October 13, 2010 03:29
Show Gist options
  • Select an option

  • Save sandrods/623375 to your computer and use it in GitHub Desktop.

Select an option

Save sandrods/623375 to your computer and use it in GitHub Desktop.
Automate subtitles download
config.yml
downloaded.log
#!/usr/bin/env ruby
require 'rubygems'
require 'mechanize'
require 'growl'
class Logger
def self.log(msg, title="GET SUBTITLES", sticky=false, show_time=true)
time = show_time ? "\n#{Time.now.strftime('%a %d %b %H:%M:%S')}" : ''
Growl.notify "#{msg}#{time}", :title=>title, :sticky=>sticky
puts "#{time} - #{msg}"
end
end
class Subtitles
URL = "http://194.14.79.53"
#URL = "http://legendas.tv"
def initialize
@config = YAML.load_file("config.yml")
@agent = WWW::Mechanize.new
login
download
end
def login
Logger.log "Logging in..."
page = @agent.get(URL)
f = page.form_with(:action => 'login_verificar.php') do |form|
form.txtLogin = @config[:login][:username]
form.txtSenha = @config[:login][:password]
form['chkLogin'] = "1"
end
button = f.button(:value=>"Entrar")
f.submit(button)
end
def get_links
ret = {}
page = @agent.get("#{URL}/destaques.php?show=2")
page./("div.Ldestaque").each do |div|
nome = $1 if div['onmouseover']=~/gpop\('(?:.*','){2}(.*)','(.*','){5}/
id = $1 if div['onclick']=~/javascript:abredown\('(.*)'\);/
ret[nome] = id
end
ret
end
def download
links = get_links
@config[:shows].each do |leg|
rex = Regexp.new(leg['link_regex'], Regexp::IGNORECASE)
if names = links.keys.select{|l| l.match(rex)}
names.each do |name|
if was_not_downloaded?(name)
id = links[name]
file = @agent.get("#{URL}/info.php?d=#{id}&c=1")
file.save
log_download!(name)
extract_file(file.filename, leg)
end
end
end
end
end
def extract_file(file_name, leg)
folder = "#{Time.now.to_f}.tmp"
FileUtils.mkdir_p(folder)
FileUtils.mv(file_name, folder)
Dir.chdir(folder)
%x(unrar e #{file_name})
files = Dir["*.srt"]
rex = Regexp.new(leg['subtitle_regex'], Regexp::IGNORECASE)
if st = files.detect { |f| f =~ rex }
FileUtils.cp st, File.expand_path(leg['folder'], @config[:base_path])
Logger.log "#{st} extracted", "GET SUBTITLES", true
else
Logger.log "#{file_name} downloaded, but no subtitle matched: #{files.join("\n")}", "GET SUBTITLES", true
end
Dir.chdir("..")
FileUtils.rm_rf(folder)
end
def log_download!(name)
time = Time.now.strftime("%d/%m/%Y %H:%M:%S")
File.open("downloaded.log", 'a') do |f|
f.write("#{time} - #{name}\n")
end
end
def was_not_downloaded?(name)
return true unless File.exist?("downloaded.log")
lines = []
File.open("downloaded.log", 'r') do |f|
lines = f.readlines
end
lines.detect {|line| line.include?(name) }.nil?
end
end
begin
u = Subtitles.new
rescue Exception => e
Logger.log e.message, "SUBTITLES ERROR", true
raise e
end
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>local.subtitles</string>
<key>ProgramArguments</key>
<array>
<string>/Users/sandro/.subtitles/get_subtitles</string>
</array>
<key>WorkingDirectory</key>
<string>/Users/sandro/.subtitles</string>
<key>StartInterval</key>
<integer>7200</integer>
<key>StandardOutPath</key>
<string>/var/log/subtitles.log</string>
<key>StandardErrorPath</key>
<string>/var/log/subtitles.log</string>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
:login:
:username: sandrods
:password: ********
:base_path: /Users/sandro/TV Shows
:shows:
- folder: Fringe.S03
link_regex: ^Fringe
subtitle_regex: (?!.*(720|264))Fringe.*\.srt
- folder: Greys.Anatomy.S07
link_regex: ^Greys
subtitle_regex: (?!.*(720|264))Greys.Anatomy.*\.srt
- folder: 30.Rock.S05
link_regex: ^30.Rock
subtitle_regex: (?!.*(720|264))30.Rock.*\.srt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment