Created
February 23, 2014 04:08
-
-
Save ilake/9166693 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 'rubygems' | |
require 'mechanize' | |
require 'debugger' | |
require 'sidekiq' | |
# Sidekiq server is multi-threaded so our Redis connection pool size defaults to concurrency (-c) | |
Sidekiq.configure_server do |config| | |
config.redis = { :namespace => 'treehouse', :url => 'redis://127.0.0.1:6379' } | |
end | |
class TreehouseWorker | |
include Sidekiq::Worker | |
def perform(topic) | |
a = Mechanize.new | |
a.get('https://teamtreehouse.com/') do |page| | |
# Click the login link | |
login_page = a.click(page.link_with(:text => /Sign in/)) | |
# Submit the login form | |
my_page = login_page.form_with(:action => 'https://teamtreehouse.com/person_session') do |f| | |
f.send('user_session[email]=', YOUR_EMAIL) | |
f.send('user_session[password]=', YOUR_PASSWORD) | |
end.click_button | |
library_page = my_page.link_with(:href => "/library").click | |
achievements_page = library_page.links_with(:href => /#{topic}/)[0].click | |
index_number = 0 | |
system("mkdir '#{achievements_page.title}'") | |
achievements_page.search('.grid-75 div.contained a').each_with_index do |link, i| | |
if link.attributes["href"] | |
lesson_link = link.attributes["href"].value | |
puts lesson_link | |
lesson_page = achievements_page.link_with(:href => lesson_link).click | |
next unless lesson_page.respond_to?(:title) | |
formatted_index_number = sprintf("%03d", index_number) | |
file_name = "#{formatted_index_number}_" + lesson_page.title.gsub(/\W/, '_') | |
lesson_page.search('.module-downloads a strong').each do |name_tag| | |
begin | |
download_link = name_tag.parent.attributes["href"].value | |
if name_tag.content == "High Definition Video" | |
lesson_page.link_with(:href => download_link).click.save("#{file_name}.mp4") | |
system("mv #{file_name}.mp4 '#{achievements_page.title}'") | |
index_number += 1 | |
elsif name_tag.content == "Video Transcript" | |
lesson_page.link_with(:href => download_link).click.save("#{file_name}.srt") | |
system("mv #{file_name}.srt '#{achievements_page.title}'") | |
end | |
rescue | |
redo | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
%w( | |
how-to-make-a-website-with-wordpress | |
local-wordpress-development | |
how-to-build-your-business-through-blogging | |
website-optimization | |
build-a-responsive-website | |
how-to-build-a-wordpress-plugin | |
git-basics | |
wordpress-for-website-owners | |
illustrator-foundations-3 | |
android-tools | |
build-a-simple-android-app | |
build-an-interactive-website | |
how-to-start-a-business | |
photoshop-foundations | |
build-a-blog-reader-android-app | |
console-foundations-2 | |
how-to-build-your-company | |
how-to-make-a-wordpress-blog | |
how-to-market-your-business | |
how-to-write-a-business-plan | |
framework-basics | |
how-to-build-a-wordpress-theme | |
).each do |topic| | |
TreehouseWorker.perform_async(topic) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment