Skip to content

Instantly share code, notes, and snippets.

@ftnk
Created December 10, 2009 17:27
Show Gist options
  • Save ftnk/253500 to your computer and use it in GitHub Desktop.
Save ftnk/253500 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
# -*- coding: utf-8 -*-
$KCODE = "UTF8"
require 'rubygems'
require "mechanize"
require "rexml/document"
require "time"
class AmebaNow
def initialize(id, pass)
@id = id
@pass = pass
@agent = WWW::Mechanize.new
@tl = []
login
end
def set_proxy(ip, port)
@agent.set_proxy(ip, port)
end
def login
login_page = @agent.get("http://www.ameba.jp/")
login_form = login_page.forms.first
login_form['amebaId'] = @id
login_form['password'] = @pass
@agent.submit(login_form)
end
def get_timeline(limit = 10, offset = 0)
url = "http://now.ameba.jp/api/timeline?limit=#{limit}&offset=#{offset}"
page = @agent.get(url)
doc = REXML::Document::new(page.body).root
now = Time.now
a = []
doc.elements.to_a('//entry').each do |i|
entry = Entry.new(i.elements['amebaId'].text,
i.elements['entryId'].text,
i.elements['entryText'].text,
i.elements['replyAmebaId'].text,
i.elements['replyEntryId'].text,
i.elements['replyNickname'].text,
i.elements['thumbnailNickname'].text,
i.elements['thumbnailImagePath'].text,
i.elements['thumbnailImageWidth'].text,
i.elements['thumbnailImageHeight'].text,
i.elements['originalImageUrl'].text,
i.elements['thumbnailImageUrl'].text,
normalize_time(i.elements['registDateStr'].text, now),
i.elements['entryDeviceStatus'].text
)
@tl << entry
end
end
def tl
@tl
end
def post(text)
page = @agent.get('http://now.ameba.jp/')
form = page.forms.first
form['entryText'] = text
@agent.submit(form)
end
def normalize_time(time, now)
case time
when /(\d+)分前/
now - $1.to_i * 60
when /(\d+)時間前/
now - $1.to_i * 60 * 60
end
end
class Entry
def initialize(*a)
@amebaId = a.shift
@entryId = a.shift
@entryText = a.shift
@replyAmebaId = a.shift
@replyEntryId = a.shift
@replyNickname = a.shift
@thumbnailNickname = a.shift
@thumbnailImagePath = a.shift
@thumbnailImageWidth = a.shift
@thumbnailImageHeight = a.shift
@originalImageUrl = a.shift
@thumbnailImageUrl = a.shift
@time = a.shift
@entryDeviceStatus = a.shift
end
attr_reader :amebaId, :entryId, :entryText, :replyAmebaId,
:replyEntryId, :replyNickname, :thumbnailNickname, :thumbnailImagePath,
:thumbnailImageWidth, :thumbnailImageHeight, :originalImageUrl,
:thumbnailImageUrl, :time, :entryDeviceStatus
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment