Last active
February 7, 2016 17:26
-
-
Save itkq/579dcf3d686f1bbc6816 to your computer and use it in GitHub Desktop.
Eventernote
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
events.txt |
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
require 'mechanize' | |
class Crawler | |
BASE_URL = 'http://www.eventernote.com' | |
EVENT_FILE = 'events.txt' | |
def retrieve_events username | |
mech = init_mech | |
event_url = "#{BASE_URL}/users/#{username}/events" | |
page_no = 1 | |
url_list = [] | |
loop do | |
mech.get "#{event_url}?page=#{page_no}" | |
events = mech.page.search('.event') | |
break if events.empty? | |
events.each do |e| | |
url_list << (e/'h4'/'a').attr('href').value | |
end | |
page_no += 1 | |
end | |
puts "#{url_list.size} events retrieved." | |
content = '' | |
while (url = url_list.pop) | |
content += "#{url}\n" | |
end | |
File.write(EVENT_FILE, content) | |
end | |
def register_events account | |
mech = init_mech | |
login_to_twitter mech, account | |
success_cnt = 0 | |
fail_cnt = 0 | |
events = File.read(EVENT_FILE).split("\n") | |
events.each do |event| | |
mech.get "#{BASE_URL}#{event}" | |
crumb = mech.page.search('#crumb').first.attr('content') | |
event_id = event.gsub('/events/', '') | |
target = "#{BASE_URL}/api/notes/add?crumb=#{crumb}&event_id=#{event_id}" | |
puts target | |
mech.get target | |
if mech.page.response['status'] == "200 OK" | |
success_cnt += 1 | |
else | |
fail_cnt += 1 | |
end | |
sleep 1 | |
end | |
puts '[register events result]' | |
puts "success: #{success_cnt}" | |
puts " fail: #{fail_cnt}" | |
end | |
def delete_events account | |
mech = init_mech | |
login_to_twitter mech, account | |
events = File.read(EVENT_FILE).split("\n") | |
events.each do |event| | |
mech.get "#{BASE_URL}#{event}" | |
note_id = mech.page.search('.right').first.children.attr('href').value.gsub('/notes/','') | |
mech.get "#{BASE_URL}/notes/#{note_id}/remove" | |
end | |
end | |
private | |
def init_mech | |
mech = Mechanize.new | |
mech.user_agent_alias = 'Windows IE 7' | |
mech.follow_meta_refresh = true | |
return mech | |
end | |
def login_to_twitter mech, account | |
login_url = "#{BASE_URL}/auth/twitter" | |
mech.get login_url | |
mech.page.form do |f| | |
f.field_with(:name => 'session[username_or_email]').value = account[:username] | |
f.field_with(:name => 'session[password]').value = account[:password] | |
end.click_button | |
if mech.page.title.match("Authorize an application") | |
puts "Invalid twitter username or password" | |
exit | |
end | |
puts "Logined." | |
end | |
end |
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
source 'https://rubygems.org' | |
gem 'mechanize' |
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
require_relative './crawler' | |
accounts = {} | |
types = [:old, :new] | |
types.each do |type| | |
info = {} | |
print "Enter #{type.to_s} account's username: " | |
info[:username] = gets.chomp! | |
print "Enter #{type.to_s} account's password: " | |
info[:password] = gets.chomp! | |
accounts[type] = info | |
end | |
crawler = Crawler.new | |
crawler.retrieve_events(accounts[:old][:username]) | |
crawler.register_events(accounts[:new]) | |
crawler.delete_events(accounts[:old]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment