Skip to content

Instantly share code, notes, and snippets.

@D3MZ
Created August 1, 2013 21:19
Show Gist options
  • Save D3MZ/6135445 to your computer and use it in GitHub Desktop.
Save D3MZ/6135445 to your computer and use it in GitHub Desktop.
unnecessarily complicated, but W/E.
require 'mechanize'
require 'pp'
require 'logger'
require 'pry'
require 'youtube_it'
#This is a great lesson of overcoding, I started with RubyTapas's boiler plate code thinking it'll be faster to code
#on top of his own stuff. But it ended up being overkill and terribly verbose.
#Abstracting the session is not necessary in this case and a terrible amount of overhead for a simple script.
#Especially when I use "click" on Mechanize::Link class. Which uses the same fucking session anyway!
module RubyTapas
class Session
def initialize email_address, password, options={}
@username = email_address
@password = password
@logger = options.fetch(:logger) { Logger.new($stderr) }
@agent = Mechanize.new {|agent| agent.user_agent_alias = 'Mac Safari' }
@agent.log = @logger
end
def agent
establish unless established?
@agent
end
def established?
@status == :established
end
private
def establish username=@username, password=@password
login_page = @agent.get("https://rubytapas.dpdcart.com/subscriber/content")
form = login_page.form_with(id: 'login-form')
form.username = username
form.password = password
home_page = @agent.submit(form)
raise "Subscriber login failed for user #{username}" if home_page.title =~ /login/i
@status = :established
end
end
end
module RubyTapas
class ContentPostGateway
def initialize(rubytapas_session, youtube_it_client, options={})
@session = rubytapas_session
@youtube_it_client = youtube_it_client
@logger = options.fetch(:logger) { Logger.new($stderr) }
end
def get_and_upload_all_videos
download_and_upload_video = lambda do |video_link|
page = video_link.click
video = page.links.select {|link| link.to_s[/\.mp4/i] }.first
path = ENV['HOME']+"/#{page.title}.mp4"
agent.get(video.uri).save_as path
youtube_video = @youtube_it_client.video_upload File.open(path), :title => page.title,
:description => page.search('//*[@id="blog-container"]/div/div/p').text,
:list => "denied"
puts youtube_video.player_url
end
list_page = agent.get "https://rubytapas.dpdcart.com/subscriber/content"
list_page.links.select {|link| link.to_s[/Read More/i] }.reverse.map(&download_and_upload_video)
end
private
def agent
@session.agent
end
end
end
youtube_client = YouTubeIt::Client.new dev_key: dev
username: email,
password: pass
rubytapas_client = RubyTapas::Session.new email, pass
RubyTapas::ContentPostGateway.new(rubytapas_client, youtube_client).get_and_upload_all_videos #this outputs the urls for the videos.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment