Skip to content

Instantly share code, notes, and snippets.

@theareba
Created April 7, 2015 15:07
Show Gist options
  • Save theareba/b56d2e964020ecbc5787 to your computer and use it in GitHub Desktop.
Save theareba/b56d2e964020ecbc5787 to your computer and use it in GitHub Desktop.
Ruby code that crawled some social site, favoriting and commenting on postings among others
#!/usr/bin/ruby
require 'rubygems'
require 'mechanize'
class Bubblews
@rational = Mechanize.new { |agent|
agent.user_agent_alias = 'Windows Mozilla'
}
@rational.get("http://www.bubblews.com/")
form = @rational.page.forms[1]
form.fields[0].value = "username"
form.fields[1].value = "password2011"
puts "Autologging..."
form.submit
@rational.cookie_jar.save_as 'cookies', :session => true, :format => :yaml
def self.crawl
unfollow
crawl_trends
@rational.page.search(".block-post").each do |t|
begin
crawl_home(t)
rescue Mechanize::ResponseCodeError, Net::HTTPNotFound
puts "An Error Occured while retrieving"
puts "-"*80
next
end
end
end
def self.crawl_home(post)
block(post)
end
def self.crawl_trends
@rational.page.search('.block.block-topic a').each do |piece|
@rational.get(piece["href"])
puts piece.at_css(".block-topic-title").text
puts '.'*40
@rational.page.search(".block-post").each do |t|
begin
block(t)
rescue Mechanize::ResponseCodeError, Net::HTTPNotFound
puts "An Error Occured while retrieving"
puts "-"*80
next
end
end
end
end
def self.block(t)
bubblews_post = t.at_css(".block-post-title a")
user = t.css('.block-author-title a').text
@rational.click(bubblews_post)
doc = @rational.page
doc.forms[1].submit
title = doc.at('.post-title')
puts "Favoriting #{title.text}"
sleep(2)
@rational.get(user_link)
doc = @rational.page
follow_button = doc.at('.btn-casual')
if follow_button.nil?
puts "Already followed #{user}"
else
@rational.click(follow_button)
puts "Follwing #{user} ..."
end
sleep(2)
comments = [
"Hello there #{user}, nice post. Let us stay connected and share amazing posts.",
"Nice post. Looking forward to more from you #{user}. Stay connected and let us bubble",
"#{user} so cool. Guess I'm going to stick here for more of this! Let's connect",
"Will be looking forward #{user} to more of such articles. I liked it.",
"Maybe a daily dose of your writing #{user} is just what I need, let us stay connected.",
"Hi #{user}, let us stay connected and share the love. Keep on bubbling",
"#{user}, that was one informative post. Keep bubbling and share the Love.",
"Nice piece there #{user}, I will for sure stay connected. Rock on",
"#{user} greetings. Bubble on, keep the connection, share, rock the world!",
"Hi #{user} great work. Keep in touch, let us stay connected. Thanks.",
"Nice read #{user}. Will be looking forward to more of this. Stay connected :)",
"Great work, nice read. Cheers #{user}. Stay connected, let us bubble.",
"Thanks for sharing this post #{user}, happy bubbling and have a nice day.",
"Loving the bubbly #{user}, very cool post, thanks for sharing :)",
"Always a pleasure reading your articles #{user}. I will surely stick around for more. Keep in touch.",
"Another day, another great article there #{user}! Keep up and happy sharing :)",
"I like your article #{user}, thanks for sharing the post :)",
"Hey #{user}, stumbled upon this interesting post while surfing my homepage. Keep it up, happy bubbling :)",
"#{user} thanks for sharing your post. Open to more of your work. Stay connected :)",
"Clear cut, nice post #{user}. Thanks for sharing. Bubble on! :)",
"Exlored Bubblews world then landed on this article. Great post there #{user}.",
"I like this article. Hey #{user} rock on, thanks for sharing :)",
"Then I land on your post #{user} and feel like the day has been worth it",
"Hello #{user} Well penned, great piece, bubble on, stay connected.",
"Just in time to catch this post. #{user} I like it, thanks for sharing."
]
@rational.click(bubblews_post)
commentors = []
@rational.page.search(".comment-author-username").each do |commentor|
commentors << commentor.text
end
unless commentors.include? '&theareba'
form = @rational.page.form_with(:class => "js-comment-form")
unless ['&theareba', '&BubblewsMemberSupport', '&Arvind', '&Bubblews', '&pabzikem', '&dexterous85', '&Jason', '&Tyler', '&pcunix'].include? user
form.fields[0].value = comments.sample
puts "Submitting comment for #{user}'s post #{bubblews_post.text}"
button = form.button_with(:value => "Submit")
@rational.submit(form, button)
end
end
puts "-"*80
sleep(2)
end
def self.unfollow
profile_link = @rational.page.at(".header-featured-content a").attr('href')
@rational.get(profile_link )
following_link = @rational.page.at(".profile-stats li~ li a").attr('href')
@rational.get(following_link)
@rational.page.search('.page-main , .user-row').each do |user|
unfollow_button = user.at_css(".btn-active").attr('href')
username = user.at_css('.user-name').text
puts "Unfollowing #{username}"
@rational.get(unfollow_button)
puts "-"*80
sleep(2)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment