Skip to content

Instantly share code, notes, and snippets.

@itkq
Last active November 8, 2015 18:04
Show Gist options
  • Save itkq/8b1a2249409da1faa014 to your computer and use it in GitHub Desktop.
Save itkq/8b1a2249409da1faa014 to your computer and use it in GitHub Desktop.
require 'mechanize'
require 'dotenv'
require 'date'
class AmebaNow
BASE_URL = 'http://now.ameba.jp'
LIMIT = 10
def initialize
print 'Enter ameba id: '
@ameba_id = gets.chomp
@mech = init_mech
end
def main
scrape_archive
end
def scrape_archive
now_year = Time.now.year
years = [now_year]
months = (1..12).to_a
url = "http://now.ameba.jp/#{@ameba_id}/archive/"
loop do
page = @mech.get(url)
pre = page.search('.archiveHeading > .pre').first
break if pre.css('a').empty?
url = pre.css('a').attr('href').value
/\?year=(?<pre_year>\d+)$/ =~ url
years << pre_year.to_i
end
years.each do |year|
months.reverse.each do |month|
puts "[#{year}/#{month}]"
entry_ids = get_entry_ids(year, month)
token = get_token(year, month)
iine(token, entry_ids)
end
end
end
def iine(token, entry_ids)
endpoint = BASE_URL + '/api/curious/like'
success = 0
data = {token: token, amebaId: @ameba_id}
entry_ids.each do |id|
print "#{id} ==> "
data[:entryId] = id
page = @mech.post(endpoint, data)
if page.code == '200' &&
Nokogiri::XML.parse(page.body).xpath('//status').text == "success"
success += 1
puts 'success'
else
puts 'failed'
end
sleep(1)
end
end
def get_entry_ids(year, month)
page = 1
entry_ids = []
while ids = retrieve_entry_ids(year, month, page)
entry_ids += ids
page += 1
end
entry_ids
end
def retrieve_entry_ids(year, month, page)
url = BASE_URL + '/api/archive/' + "#{@ameba_id}/#{year}/#{sprintf('%02d', month)}" + "?offset=#{(page-1)*LIMIT}&limit=#{LIMIT}"
@mech.get(url)
xml = Nokogiri::XML.parse(@mech.page.body)
html = xml.xpath('//htmlData').children.to_html
nows = Nokogiri::HTML.parse(html).css('.now')
return nil if nows.empty?
nows.map{|now|
unless now.css('.curiousBtn.def').empty?
now.attr('data-entry-id')
end
}.compact
end
def get_token(year, month)
url = BASE_URL + "/#{@ameba_id}/archive/#{year}/#{sprintf('%02d', month)}"
page = @mech.get(url)
/NOW\.TOKEN\s=\s"(?<token>.+)"/ =~ page.body
token
end
def init_mech (ua = "Windows IE 7")
Dotenv.load
mech = Mechanize.new
mech.user_agent_alias = ua
print 'Logining ... '
login_url = 'http://now.ameba.jp/displayLogin/'
mech.get login_url
mech.page.form do |f|
f.amebaId = ENV['AMEBA_ID']
f.password = ENV['AMEBA_PASSWORD']
end.click_button
puts 'logined.'
return mech
end
end
ameba_now = AmebaNow.new
ameba_now.main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment