Created
September 29, 2010 18:34
-
-
Save digilord/603278 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
#!/home/digilord/.rvm/rubies/ruby-1.8.7-p302/bin/ruby | |
require 'rubygems' | |
require 'open-uri' | |
require 'nokogiri' | |
require 'action_mailer' | |
require 'active_support' | |
require 'yaml' | |
trap("SIGINT") { exit! } | |
script_name = $0.gsub('./','').gsub('.rb','') | |
sample_config_data = <<EOS | |
:mail: # Parameters for GMail or Google Apps for your Domain | |
:smtp_server: smtp.gmail.com | |
:smtp_port: 587 | |
:domain: example.com | |
:user_name: john@example.com | |
:password: secretpassword | |
:authentication: plain | |
:enable_starttls_auto: true | |
:general: | |
# Make this something unique that stands out to you. | |
# As a suggestion use the title of the site you are | |
# pulling videos from. | |
:email_title: MyVideoSite | |
# Domain is used to build up the links to download the videos. | |
# This MIGHT be different from the video archive URL, i.e., | |
# http://media.example.com. | |
:domain: http://www.example.com | |
# Use the selector gadget from http://selectorgadget.com | |
# to find the anchor that represents the video. | |
:video_css_selector: .download a | |
# Use this selector to find all the links to the pages that | |
# have the videos. | |
:archive_page_css_selector: "#main a" | |
# This is the text that is inside the anchor tag. | |
:video_link_text: Download | |
# Most video sites have an archive page. You want that here. | |
:video_archive_url: http://www.example.com/videos/archive | |
# Who do you want the emails to go to? This can be a comma | |
# separated list. | |
:mail_to: me@example.com | |
EOS | |
config_file = ENV['HOME'] + "/.#{script_name}.yml" | |
if not File.file?(config_file) | |
# Write out a sample config file for the user to fill in. | |
File.open(config_file, 'w') {|f| f.write(sample_config_data) } | |
puts "Wrote a sample configuration to #{config_file}." | |
puts "Please edit it to tailor it to your requirements" | |
exit! | |
else | |
raw_config = File.read(config_file) | |
APP_CONFIG = YAML.load(raw_config) | |
end | |
video_name = APP_CONFIG[:general][:email_title] | |
domain = APP_CONFIG[:general][:domain] | |
video_css_selector = APP_CONFIG[:general][:video_css_selector] | |
archive_page_css_selector = APP_CONFIG[:general][:video_css_selector] | |
video_link_text = APP_CONFIG[:general][:video_link_text] | |
archive_url = APP_CONFIG[:general][:video_archive_url] | |
archive_page_css_selector = APP_CONFIG[:general][:archive_page_css_selector] | |
video_path = ENV['HOME'] + "/Videos/#{video_name}" | |
files_downloaded = [] | |
# ActionMailer parameters for GMail or Google Apps for your Domain | |
ActionMailer::Base.smtp_settings = { | |
:address => APP_CONFIG[:mail][:smtp_server], | |
:port => APP_CONFIG[:mail][:smtp_port], | |
:domain => APP_CONFIG[:mail][:domain], | |
:user_name => APP_CONFIG[:mail][:user_name], | |
:password => APP_CONFIG[:mail][:password], | |
:authentication => APP_CONFIG[:mail][:authentication], | |
:enable_starttls_auto => APP_CONFIG[:mail][:enable_starttls_auto] | |
} | |
# Setup the mailer we want to use. | |
class VideoMailer < ActionMailer::Base | |
default :from => ::APP_CONFIG[:mail][:user_name] | |
def new_video(*args) | |
options = args.extract_options! | |
message = options[:message] | |
date = Time.new.strftime("%Y-%m-%d") | |
to = ::APP_CONFIG[:general][:mail_to] | |
title = ::APP_CONFIG[:general][:email_title] | |
mail(:to => to, | |
:subject => "[#{title}] New Videos for #{date}", | |
:body => message | |
) | |
end | |
end | |
# Make sure that the video path exists. If not make it. | |
if not File::directory?(video_path) | |
Dir.mkdir(video_path) | |
end | |
# For each link in the page download the video and place it in @video_path. | |
def fetch_video(*args) | |
options = args.extract_options! | |
link = options[:link] | |
video_path = options[:video_path] | |
domain = options[:domain] | |
video_css_selector = options[:video_css_selector] | |
video_link_text = options[:video_link_text] | |
file_downloaded = nil | |
download_link = nil | |
page_url = "#{domain}#{link[:href]}" | |
page = Nokogiri::HTML(open(page_url)) | |
p page_url | |
page.css(video_css_selector).each do |download_a| | |
if download_a.text == video_link_text | |
download_link = download_a[:href] | |
file_name = download_link.split('/')[-1] | |
file_path = "#{video_path}/#{file_name}" | |
p file_name | |
if not File.file?(file_path) | |
file = open(download_link).read() | |
File.open(file_path, 'w') {|f| f.write(file) } | |
file_downloaded = file_name | |
end | |
end | |
end | |
return file_downloaded | |
end | |
puts "Processing" | |
archive = Nokogiri::HTML(open(archive_url)) | |
threads = [] | |
archive.css(archive_page_css_selector).each do |link| | |
threads << Thread.new(link) do |thread_link| | |
files_downloaded.push fetch_video :link => link, | |
:video_path => video_path, | |
:domain => domain, | |
:video_css_selector => video_css_selector, | |
:video_link_text => video_link_text | |
end | |
end | |
threads.each do |aThread| | |
aThread.join | |
end | |
puts files_downloaded | |
puts "Sending EMail" | |
# Mail the user something to tell them what was done. | |
if files_downloaded | |
downloaded_list_as_string = files_downloaded.join("\n") | |
message = <<EOS | |
Hello Master, | |
Files Downloaded: #{files_downloaded.count} | |
#{downloaded_list_as_string} | |
Thank you! | |
Your Agent | |
EOS | |
else | |
message = <<EOS | |
Hello Master, | |
No new files today. | |
Thank you | |
Your Agent | |
EOS | |
end | |
VideoMailer.new_video(:message => message).deliver | |
puts "Complete" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment