Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
commit new version
- Loading branch information
xdite
committed
Feb 28, 2009
1 parent
81da878
commit 4292e3d
Showing
7 changed files
with
348 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require 'rubygems' | ||
require File.join(File.dirname(__FILE__), '..', 'lib', 'plurk') | ||
username = "plurk" | ||
password = "12345" | ||
a = Plurk::Base.new(username,password) | ||
a.login |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
%w(uri cgi net/http yaml rubygems active_support mechanize json).each { |f| require f } | ||
|
||
$:.unshift(File.join(File.dirname(__FILE__))) | ||
require 'plurk/easy_class_maker' | ||
require 'plurk/base' | ||
require 'plurk/status' | ||
require 'plurk/user' | ||
require 'plurk/response' | ||
|
||
module Plurk | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,254 @@ | ||
module Plurk | ||
class Base | ||
attr_reader :logged_in, :uid, :nickname, :friend_ids, :fan_ids, :cookies , :info | ||
def initialize(nickname, password, options={}) | ||
@info , @info[:nickname], @info[:password] = {}, nickname, password | ||
@api_host = 'http://www.plurk.com' | ||
end | ||
|
||
def login | ||
agent = WWW::Mechanize.new | ||
begin | ||
agent.get(@api_host) do |login_page| | ||
timeline = login_page.form_with(:action => '/Users/login') do |form| | ||
form.nick_name = @info[:nickname] | ||
form.password = @info[:password] | ||
end.submit | ||
/var GLOBAL = \{.*"uid": ([\d]+),.*\}/imu =~ timeline.body | ||
@uid = Regexp.last_match[1] | ||
/var FRIENDS = (.*);/ =~ timeline.body | ||
@friend_ids = plurk_to_json($1).keys if Regexp.last_match[1] | ||
/var FANS = (.*);/ =~ timeline.body | ||
@fan_ids = plurk_to_json($1).keys if Regexp.last_match[1] | ||
/var SETTINGS = (.*);/ =~ timeline.body | ||
@info[:settings] = plurk_to_json($1) if Regexp.last_match[1] | ||
end | ||
|
||
@cookies = agent.cookie_jar | ||
@logged_in = true | ||
rescue | ||
false | ||
end | ||
end | ||
|
||
|
||
def add_plurk(content="", qualifier="says", limited_to=[], no_comments=false, lang="en") | ||
return false unless @logged_id | ||
no_comments = no_comments ? 1 : 0 | ||
|
||
params = { | ||
:posted => Time.now.getgm.strftime("%Y-%m-%dT%H:%M:%S"), | ||
:qualifier => qualifier, | ||
:content => content[0...140], | ||
:lang => lang, | ||
:uid => @uid, | ||
:no_comments => no_comments | ||
} | ||
|
||
params[:limited_to] = "[#{limited_to.join(",")}]" unless limited_to.empty? | ||
data = plurk_to_json(request("/TimeLine/addPlurk", :method => :post , :params => params))["plurk"] | ||
|
||
return Status.new(data) | ||
# if data =~ /anti-flood/ # ??? | ||
end | ||
|
||
def get_permalink(plurk_id) | ||
return "http://www.plurk.com/p/#{plurk_id.to_s(36)}" | ||
end | ||
|
||
|
||
def permalink_to_plurk_id(permalink) | ||
/http:\/\/www.plurk.com\/p\/([a-zA-Z0-9]*)/ =~ permalink | ||
return $1.to_i(36) | ||
end | ||
def delete_plurk(plurk_id) | ||
return false unless @logged_id | ||
params = { | ||
:plurk_id => plurk_id | ||
} | ||
data = request("/TimeLine/deletePlurk", :method => :post , :params => params ) | ||
(data == "ok")? true : false | ||
|
||
end | ||
|
||
def respond_to_plurk(plurk_id, lang, qualifier, content) | ||
return false unless @logged_id | ||
params = { | ||
:plurk_id => plurk_id, | ||
:uid => @uid, | ||
:p_uid => @uid, | ||
:lang => "en", | ||
:content => content[0...140], | ||
:qualifier => qualifier, | ||
:posted => Time.now.strftime("%Y-%m-%dT%H:%M:%S") | ||
} | ||
|
||
data = plurk_to_json(request("/Responses/add", :method => :post , :params => params ))["object"] | ||
return Response.new(data) | ||
|
||
end | ||
|
||
|
||
def nickname_to_uid(nickname) | ||
data = request("/user/#{nickname}", :method => :get ) | ||
/\{"page_user": \{.*"page_title": null, "uid": ([0-9]+),.+\}\}/imu =~ data | ||
return uid = ($1)? $1 : -1 | ||
end | ||
|
||
|
||
def get_responses(plurk_id) | ||
return false unless @logged_id | ||
params = { | ||
:plurk_id => plurk_id | ||
} | ||
data = responses(plurk_to_json(request("/Responses/get2", :method => :post , :params => params ))["responses"]) | ||
return data | ||
end | ||
|
||
def uid_to_userinfo(uid) | ||
params = { | ||
:user_id => uid | ||
} | ||
data = plurk_to_json(request("/Users/fetchUserInfo", :method => :get , :params => params )) | ||
user = User.new(data) | ||
return user | ||
end | ||
|
||
|
||
def block_user(uid) | ||
return false unless @logged_id | ||
params = { | ||
:block_uid => uid | ||
} | ||
data = request("/Friends/blockUser", :method => :post , :params => params ) | ||
(data =="ok") ? true : false | ||
|
||
end | ||
|
||
def unblock_user(uid) | ||
return false unless @logged_id | ||
params = { | ||
:friend_id => uid | ||
} | ||
data = request("/Friends/removeBlock", :method => :post , :params => params ) | ||
(data =="ok") ? true : false | ||
end | ||
|
||
def make_fan(uid) | ||
return false unless @logged_id | ||
params = { | ||
:friend_id => uid | ||
} | ||
data = request("/Notifications/allowDontFollow", :method => :post, :params => params) | ||
(data =="ok") ? true : false | ||
end | ||
|
||
def allow_friend(uid) | ||
return false unless @logged_id | ||
params = { | ||
:friend_id => uid | ||
} | ||
data = request("/Notifications/allow", :method => :post, :params => params) | ||
(data =="ok") ? true : false | ||
end | ||
|
||
def get_blocked_users(offset = 0) | ||
return false unless @logged_id | ||
params = { | ||
:offset => offset, | ||
:user_id => @uid | ||
} | ||
data = users(plurk_to_json(request("/Friends/getBlockedByOffset", :method => :post, :params => params ))) | ||
return data | ||
end | ||
|
||
def mute_plurk(plurk_id, setmute) | ||
return false unless @logged_id | ||
params = { | ||
:plurk_id => plurk_id, | ||
:value => setmute | ||
} | ||
data = request("/TimeLine/setMutePlurk", :method => :post, :params => params ) | ||
(data.body.to_i == setmute) ? true :false | ||
end | ||
|
||
def get_unread_plurks(fetch_responses=false) | ||
return false unless @logged_id | ||
params = { | ||
:fetch_responses => fetch_responses | ||
} | ||
data = statuses(plurk_to_json(request("/TimeLine/getUnreadPlurks", :method => :get, :params => params ))) | ||
return data | ||
end | ||
|
||
def get_plurks(uid=nil, date_from=nil, date_offset=nil, fetch_responses=false) | ||
return false unless @logged_id | ||
uid ||= @uid | ||
params = { | ||
:user_id =>@uid, | ||
:date_offset => date_offset.strftime("%Y-%m-%dT%H:%M:%S") || Time.now.getgm.strftime("%Y-%m-%dT%H:%M:%S"), | ||
:from_date => date_from.getgm.strftime("%Y-%m-%dT%H:%M:%S") || Time.now.getgm.strftime("%Y-%m-%dT%H:%M:%S"), | ||
:fetch_responses => fetch_responses, | ||
} | ||
data = statuses(plurk_to_json(request("/TimeLine/getPlurks", :method => :post, :params => params ))) | ||
return data | ||
end | ||
|
||
def deny_friend(uid) | ||
return false unless @logged_id | ||
params = { | ||
:friend_id => uid | ||
} | ||
data = request("/Notifications/deny", :method => :post, :params => params) | ||
if data == "ok" | ||
return true | ||
else | ||
return false | ||
end | ||
end | ||
|
||
def get_alerts | ||
return false unless @logged_id | ||
|
||
data = request("/Notifications", :method => :get) | ||
@result = [] | ||
data.scan(/DI\s*\(\s*Notifications\.render\(\s*(\d+),\s*0\)\s*\);/) { |matched| | ||
@result << matched | ||
} | ||
return @result | ||
end | ||
|
||
private | ||
|
||
def statuses(doc) | ||
doc.inject([]) { |statuses, status| statuses << Status.new(status); statuses } | ||
end | ||
|
||
# Converts an hpricot doc to an array of users | ||
def users(doc) | ||
doc.inject([]) { |users, user| users << User.new(user); users } | ||
end | ||
|
||
def responses(doc) | ||
doc.inject([]) { |responses, response| responses << Response.new(response); responses } | ||
end | ||
|
||
def request(path, options = {}) | ||
agent = WWW::Mechanize.new | ||
agent.cookie_jar = @info[:cookies] | ||
case options[:method].to_s | ||
when "get" | ||
agent.get(@api_host+path, options[:params]) | ||
when "post" | ||
agent.post(@api_host+path, options[:params]) | ||
end | ||
return agent.current_page.body | ||
end | ||
|
||
def plurk_to_json(json) | ||
/new Date\((\".+?\")\)/.match(json) | ||
json = json.gsub(/new Date\((\".+?\")\)/, Regexp.last_match[1]) if Regexp.last_match | ||
return JSON.parse(json) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module Plurk | ||
module EasyClassMaker | ||
|
||
def self.included(base) | ||
base.extend(ClassMethods) | ||
end | ||
|
||
module ClassMethods | ||
# creates the attributes class variable and creates each attribute's accessor methods | ||
def attributes(*attrs) | ||
@@attributes = attrs | ||
@@attributes.each { |a| attr_accessor a } | ||
end | ||
|
||
# read method for attributes class variable | ||
def self.attributes; @@attributes end | ||
end | ||
|
||
# allows for any class that includes this to use a block to initialize | ||
# variables instead of assigning each one seperately | ||
# | ||
# Example: | ||
# | ||
# instead of... | ||
# | ||
# s = Status.new | ||
# s.foo = 'thing' | ||
# s.bar = 'another thing' | ||
# | ||
# you can ... | ||
# | ||
# Status.new do |s| | ||
# s.foo = 'thing' | ||
# s.bar = 'another thing' | ||
# end | ||
def initialize | ||
yield self if block_given? | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Plurk | ||
class Response | ||
include EasyClassMaker | ||
attributes :lang, :content_raw, :user_id , :qualifier, :plurk_id, :content, :id, :posted, :last_ts | ||
def initialize(attributes) | ||
attributes.each do |attr, val| | ||
instance_variable_set("@#{attr}", val) | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Plurk | ||
class Status | ||
include EasyClassMaker | ||
attributes :lang , :content_raw, :user_id, :plurk_type, :plurk_id, :response_count, :owner_id, :qualifier, :id , :content , :response_count, :posted, :limited_to, :no_comments, :is_unread, :error | ||
def initialize(attributes) | ||
attributes.each do |attr, val| | ||
instance_variable_set("@#{attr}", val) | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Plurk | ||
class User | ||
include EasyClassMaker | ||
attributes :display_name, :uid, :is_channel, :nick_name, :has_profile_image, :are_friends, :location , :theme, :date_of_birth, :relationship, :avatar, :full_name, :gender, :page_title, :is_blocked, :recruited, :id, :karma | ||
def initialize(attributes) | ||
attributes.each do |attr, val| | ||
instance_variable_set("@#{attr}", val) | ||
end | ||
end | ||
end | ||
end | ||
|