tjplurker 1.3 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,726 +0,0 @@
1
- # :title:TJPlurker API Documentation
2
- # encoding: utf-8
3
- require 'oauth'
4
- require 'json'
5
- require 'logger'
6
- require 'open-uri'
7
-
8
- $tjpLog = Logger.new(STDOUT)
9
- $tjpLog.level = Logger::INFO
10
- $tjpLog.formatter = proc{ |severity, datetime, progname, msg|
11
- "#{severity} [#{datetime}] in #{progname} -- #{msg}\n"
12
- }
13
-
14
- module TJP
15
- # It's core class of TJPlurker based on {Plurk API 2.0}[http://www.plurk.com/API].
16
- # Note that every API wrapped methods in this class returns a JSON object
17
- # All mehtods will retry again when request timeout occurs.
18
- class TJPlurker
19
- # It will automatically authorize if +access_token+ and +access_token_secret+ are both given.
20
- # If you do not have access token and secret, leave the parameters blank,
21
- # and you will be asked to enter verification code to get access token and secret.
22
- # key:: consumer key
23
- # secret:: consumer secret
24
- # return:: OAuth::AccessToken
25
- def initialize(key, secret, token=nil, token_secret=nil)
26
- @key, @secret, @token, @token_secret = key, secret, token, token_secret
27
- @consumer = OAuth::Consumer.new(@key, @secret, {
28
- :site => 'http://www.plurk.com',
29
- :scheme => :header,
30
- :http_method => :post,
31
- :request_token_path => '/OAuth/request_token',
32
- :access_token_path => '/OAuth/access_token',
33
- :authorize_path => '/OAuth/authorize'
34
- })
35
- end
36
-
37
-
38
- # return:: authorize URL
39
- def get_authorize_url
40
- @request_token = @consumer.get_request_token
41
- return @request_token.authorize_url
42
- end
43
-
44
- # Change or get access token and secret.
45
- # If parameters are not both given, user will be asked to enter verfication code,
46
- # otherwise, replace the access token and secret.
47
- # return:: access token and access token secret in Array
48
- def authorize(access_token=nil, access_token_secret=nil)
49
- if access_token && access_token_secret
50
- #@access_token = OAuth::AccessToken.new(@consumer, access_token, access_token_secret)
51
- @token, @token_secret = access_token, access_token_secret
52
- else
53
- puts "Authorize URL: #{get_authorize_url}"
54
- print "Please Enter your verification code: "
55
- verify = gets.chomp!
56
- access_token = @request_token.get_access_token :oauth_verifier=>verify
57
- @token, @token_secret = access_token.token, access_token.secret
58
- end
59
- $tjpLog.info(self.class){"Access Token: #{@token}, Access Token Secret: #{@token_secret}"}
60
- return @token, @token_secret
61
- end
62
-
63
- # url:: plurk APP url in String
64
- # body:: options in Hash
65
- # return:: result in JSON
66
- def api(url, body=nil, headers=nil)
67
- # clone consumer for supporting thread
68
- access_token = OAuth::AccessToken.new(@consumer.clone, @token, @token_secret)
69
- resp = access_token.post(url, body, headers).body
70
- json = JSON.parse(resp)
71
- $tjpLog.debug(self.class){"Response: #{json}"}
72
- $tjpLog.error(self.class){json["error_text"]} if json.is_a?(Hash) && json["error_text"]
73
- return json
74
- end
75
-
76
- # :section: Users
77
-
78
- # Returns info about current user's karma, including current karma,
79
- # karma growth, karma graph and the latest reason why the karma has dropped.
80
- def get_karma_stats
81
- method = "/APP/Users/getKarmaStats"
82
- api(method)
83
- end
84
-
85
- # :section: Profile
86
-
87
- # Returns data that's private for the current user.
88
- # This can be used to construct a profile and render a timeline of the latest plurks.
89
- def get_own_profile
90
- method = "/APP/Profile/getOwnProfile"
91
- api(method)
92
- end
93
-
94
- # Fetches public information such as a user's public plurks and basic information.
95
- # Fetches also if the current user is following the user, are friends with or is a fan.
96
- def get_public_profile user_id
97
- method = "/APP/Profile/getPublicProfile"
98
- attr = {:user_id=>user_id}
99
- api(method, attr)
100
- end
101
-
102
- # :section: Real Time Notifications
103
-
104
- # Get instant notifications when there are new plurks and responses on a user's timeline.
105
- # Generally you don't need call this because comet_channel will automatically do that for you.
106
- # For details, see http://www.plurk.com/API#realtime
107
- # return:: channel name and comet server in Hash
108
- def get_user_channel
109
- method = "/APP/Realtime/getUserChannel"
110
- api(method)
111
- end
112
-
113
- # It will do GET requests to user's comet server to get new notification data.
114
- # The request will sleep for about 50 seconds before returning a response if there is
115
- # no new data added to your channel.
116
- # It will automatically get comet server URL from /APP/Realtime/getUserChannel
117
- # if there is no comet server yet, and renew +offset+ each time it was called.
118
- # You don't need to worry about URL and +offset+.
119
- # For details, see http://www.plurk.com/API#realtime
120
- # return:: plurk notification in Hash
121
- def comet_channel
122
- #Assign new channel if @user_channel is nil
123
- @user_channel ||= get_user_channel
124
- begin
125
- res = Net::HTTP.get_response(URI.parse(@user_channel['comet_server']))
126
- json = JSON.parse(res.body.sub!(/CometChannel\.scriptCallback\((.*)\);/){|match| $1})
127
- rescue Timeout::Error => ex
128
- $tjpLog.warn(self.class){"Request timeout, retry."}
129
- retry
130
- rescue Errno::ECONNRESET => ex
131
- $tjpLog.warn(self.class){"Connection reset, retry."}
132
- retry
133
- rescue => ex
134
- $tjpLog.fatal(self.class){"Unknown error, skip."}
135
- puts ex.class, ex.message, ex.backtrace
136
- return
137
- end
138
- #Update the offset
139
- @user_channel['comet_server'].sub!(/[\d]+$/, json["new_offset"].to_s)
140
- if json['data']
141
- json['data'].each{|member|
142
- notification = NotificationData.new(member)
143
- # Prevent duplicate notification
144
- return if notification.id == @id_buffer
145
- $tjpLog.info(self.class){"Notification: #{notification.user_id}: \"#{notification.content}\""}
146
- @id_buffer = notification.id
147
- yield(notification)
148
- }
149
- end
150
- json
151
- end
152
-
153
- # :section: Polling
154
-
155
- # You should use this call to find out if there any new plurks posted to the user's timeline.
156
- # It's much more efficient than doing it with timeline_get_plurks, so please use it :)
157
- # offset:: Return plurks newer than offset, formatted as 2009-6-20T21:55:34 in String or in Time.
158
- # limit:: The max number of plurks to be returned (default: 20)
159
- def get_plurks offset, limit=nil
160
- method = "/APP/Polling/getPlurks"
161
- offset = offset.utc.strftime("%Y-%m-%dT%H:%M:%S") if offset.is_a? Time
162
- attr = {:offset=>offset, :limit=>limit}
163
- api(method, attr)
164
- end
165
-
166
- # Use this call to find out if there are unread plurks on a user's timeline.
167
- def get_unread_count
168
- method = "/APP/Polling/getUnreadCount"
169
- api(method)
170
- end
171
-
172
- # :section: Timeline
173
-
174
- # Get one plurk from user's timeline
175
- # plurk_id:: Can be Integer or String
176
- def get_plurk plurk_id
177
- method = "/APP/Timeline/getPlurk"
178
- attr = {:plurk_id=>plurk_id}
179
- api(method, attr)
180
- end
181
-
182
- # Get plurks from user's timeline
183
- # offset:: Return plurks older than offset, formatted as 2009-6-20T21:55:34 in String or in Time.
184
- # limit:: The max number of plurks to be returned (default: 20)
185
- # filter:: Can be only_user, only_responded, only_private or only_favorite
186
- def timeline_get_plurks offset=nil, limit=nil, filter=nil
187
- method = "/APP/Timeline/getPlurks"
188
- offset = offset.utc.strftime("%Y-%m-%dT%H:%M:%S") if offset.is_a? Time
189
- attr = {:offset=>offset, :limit=>limit, :filter=>filter}
190
- api(method, attr)
191
- end
192
-
193
- # Get unread plurks from user's timeline
194
- # offset:: Return plurks newer than offset, formatted as 2009-6-20T21:55:34 in String or in Time.
195
- # limit:: The max number of plurks to be returned (default: 20)
196
- # filter:: Can be only_user, only_responded, only_private or only_favorite
197
- def get_unread_plurks offset=nil, limit=nil, filter=nil
198
- method = "/APP/Timeline/getUnreadPlurks"
199
- offset = offset.utc.strftime("%Y-%m-%dT%H:%M:%S") if offset.is_a? Time
200
- attr = {:offset=>offset, :limit=>limit, :filter=>filter}
201
- api(method, attr)
202
- end
203
-
204
- # Get public plurks from user's timeline
205
- # user_id:: The user_id of the public plurks owner to get. Can be integer (like 34) or nick name (like amix).
206
- # offset:: Return plurks newer than offset, formatted as 2009-6-20T21:55:34 in String or in Time.
207
- # limit:: The max number of plurks to be returned (default: 20)
208
- # filter:: Can be only_user, only_responded, only_private or only_favorite
209
- def get_public_plurks user_id, offset=nil, limit=nil, filter=nil
210
- method = "/APP/Timeline/getPublicPlurks"
211
- offset = offset.utc.strftime("%Y-%m-%dT%H:%M:%S") if offset.is_a? Time
212
- attr = {:user_id=>user_id, :offset=>offset, :limit=>limit, :filter=>filter}
213
- api(method, attr)
214
- end
215
-
216
- # qualifier:: The Plurk's qualifier, must be in English. Can be following:
217
- # loves, likes, shares, gives, hates, wants, has, will, asks, wishes, was,
218
- # feels, thinks, says, is, :, freestyle, hopes, needs, wonders
219
- # limited_to:: Limit the plurk only to some users (also known as private plurking).
220
- # Should can be a JSON list of friend ids in String formatting or Ruby Array, e.g.
221
- # \[3,4,66,34] or "\[3,4,66,34]" will only be plurked to these user ids.
222
- # If it's [0] then the Plurk is privatley posted to the poster's friends.
223
- # no_comments:: If set to 1 or "1", then responses are disabled for this plurk.
224
- # If set to 2 or "2", then only friends can respond to this plurk.
225
- # lang:: The plurk's language. Can be following:
226
- # 'en': 'English', 'pt_BR': 'Português', 'cn': '中文 (中国)', 'ca': 'Català', 'el': 'Ελληνικά'
227
- # 'dk': 'Dansk', 'de': 'Deutsch', 'es': 'Español', 'sv': 'Svenska', 'nb': 'Norsk bokmål'
228
- # 'hi': 'Hindi', 'ro': 'Română', 'hr': 'Hrvatski', 'fr': 'Français', 'ru': 'Pусский'
229
- # 'it': 'Italiano', 'ja': '日本語', 'he': 'עברית', 'hu': 'Magyar', 'ne': 'Nederlands', 'th': 'ไทย'
230
- # 'ta_fp': 'Filipino', 'in': 'Bahasa Indonesia', 'pl': 'Polski', 'ar': 'العربية', 'fi': 'Finnish'
231
- # 'tr_ch': '中文 (繁體中文)', 'tr': 'Türkçe', 'ga': 'Gaeilge', 'sk': 'Slovenský'
232
- # 'uk': 'українська', 'fa': 'فارسی
233
- def plurk_add content, qualifier=':', limited_to=nil, no_comments=nil, lang='tr_ch'
234
- method = "/APP/Timeline/plurkAdd"
235
- limited_to = limited_to.to_s if limited_to.is_a? Array
236
- attr = {:content=>content, :qualifier=>qualifier, :limited_to=>limited_to, :no_comments=>no_comments, :lang=>lang}
237
- api(method, attr)
238
- end
239
-
240
- # plurk_id:: The id of the plurk.
241
- def plurk_delete plurk_id
242
- method = "/APP/Timeline/plurkDelete"
243
- attr = {:plurk_id=>plurk_id}
244
- api(method, attr)
245
- end
246
-
247
- # plurk_id:: The id of the plurk.
248
- # content:: The content of plurk.
249
- def plurk_edit plurk_id, content
250
- method = "/APP/Timeline/plurkEdit"
251
- attr = {:plurk_id=>plurk_id, :content=>content}
252
- api(method, attr)
253
- end
254
-
255
- # ids:: The plurk ids, formated as JSON String or Ruby Array, e.g. "[342,23242,2323]" or [342,23242,2323]
256
- def mute_plurks ids
257
- method = "/APP/Timeline/mutePlurks"
258
- ids = ids.to_s if ids.is_a? Array
259
- attr = {:ids=>ids}
260
- api(method, attr)
261
- end
262
-
263
- # ids:: The plurk ids, formated as JSON String or Ruby Array, e.g. "[342,23242,2323]" or [342,23242,2323]
264
- def unmute_plurks
265
- method = "/APP/Timeline/unmutePlurks"
266
- ids = ids.to_s if ids.is_a? Array
267
- attr = {:ids=>ids}
268
- api(method, attr)
269
- end
270
-
271
- # ids:: The plurk ids, formated as JSON String or Ruby Array, e.g. "[342,23242,2323]" or [342,23242,2323]
272
- def favorite_plurks ids
273
- method = "/APP/Timeline/favoritePlurks"
274
- ids = ids.to_s if ids.is_a? Array
275
- attr = {:ids=>ids}
276
- api(method, attr)
277
- end
278
-
279
- # ids:: The plurk ids, formated as JSON String or Ruby Array, e.g. "[342,23242,2323]" or [342,23242,2323]
280
- def unfavorite_plurks ids
281
- method = "/APP/Timeline/unfavoritePlurks"
282
- ids = ids.to_s if ids.is_a? Array
283
- attr = {:ids=>ids}
284
- api(method, attr)
285
- end
286
-
287
- # ids:: The plurk ids, formated as JSON String or Ruby Array, e.g. "[342,23242,2323]" or [342,23242,2323]
288
- # note_position:: If true responses_seen of the plurks will be updated as well (to match response_count).
289
- def mark_as_read ids, note_position=nil
290
- method = "/APP/Timeline/markAsRead"
291
- ids = ids.to_s if ids.is_a? Array
292
- attr = {:ids=>ids, :note_position=>note_position}
293
- api(method, attr)
294
- end
295
-
296
- # :section: Response
297
-
298
- # Fetches responses for plurk with plurk_id and some basic info about the users.
299
- # plurk_id:: The plurk that the responses belong to.
300
- # from_response:: Only fetch responses from an offset - could be 5, 10 or 15 (default: 0)
301
- def response_get plurk_id, from_response=nil
302
- method = "/APP/Responses/get"
303
- attr = {:plurk_id=>plurk_id, :from_response=>from_response}
304
- api(method, attr)
305
- end
306
-
307
- # Adds a responses to plurk_id. Language is inherited from the plurk.
308
- # plurk_id:: The plurk that the responses belong to.
309
- # content:: The response's text.
310
- # qualifier:: The Plurk's qualifier, must be in English. Refers to plurk_add.
311
- def response_add plurk_id, content, qualifier=':'
312
- method = "/APP/Responses/responseAdd"
313
- attr = {:plurk_id=>plurk_id, :content=>content, :qualifier=>qualifier}
314
- api(method, attr)
315
- end
316
-
317
- #Deletes a response. A user can delete own responses or responses that are posted to own plurks.
318
- # response_id:: The id of the response to delete.
319
- # plurk_id:: The plurk that the response belongs to.
320
- def response_delete response_id, plurk_id
321
- method = "/APP/Responses/responseDelete"
322
- attr = {:response_id=>response_id, :plurk_id=>plurk_id}
323
- api(method, attr)
324
- end
325
-
326
- # :section: Friends and Fans
327
-
328
- # Returns user_id's friend list in chucks of 100 friends at a time.
329
- # However, We suggest you use get_friends instead of get_friends_by_offset
330
- # limit:: The max number of friends to be returned.
331
- # offset:: Can be Numeric or String, e.g. 10, "20", 30, "40" etc.
332
- # return:: An Array
333
- def get_friends_by_offset user_id, limit=100, offset=0
334
- method = "/APP/FriendsFans/getFriendsByOffset"
335
- attr = {:user_id=>user_id, :offset=>offset, :limit=>limit}
336
- api(method, attr)
337
- end
338
-
339
- # Returns user_id's fans list in chucks of 100 fans at a time.
340
- # However, We suggest you use get_fans instead of get_fans_by_offset
341
- # limit:: The max number of friends to be returned.
342
- # offset:: Can be Numeric or String, e.g. 10, "20", 30, "40" etc.
343
- # return:: An Array
344
- def get_fans_by_offset user_id, limit=100, offset=0
345
- method = "/APP/FriendsFans/getFansByOffset"
346
- attr = {:user_id=>user_id, :offset=>offset, :limit=>limit}
347
- api(method, attr)
348
- end
349
-
350
- # Returns users that the current logged in user follows as fan - in chucks of 100 fans at a time.
351
- # However, We suggest you use get_following instead of get_following_by_offset
352
- # limit:: The max number of friends to be returned.
353
- # offset:: Can be Numeric or String, e.g. 10, "20", 30, "40" etc.
354
- # return:: An Array
355
- def get_following_by_offset limit=100, offset=0
356
- method = "/APP/FriendsFans/getFollowingByOffset"
357
- attr = {:offset=>offset, :limit=>limit}
358
- api(method, attr)
359
- end
360
-
361
- # Create a friend request to friend_id. User with friend_id has to accept a friendship.
362
- # friend_id:: The ID of the user you want to befriend.
363
- def become_friend friend_id
364
- method = "/APP/FriendsFans/becomeFriend"
365
- attr = {:friend_id=>friend_id}
366
- api(method, attr)
367
- end
368
-
369
- # Remove friend with ID friend_id. friend_id won't be notified.
370
- # friend_id:: The ID of the user you want to remove
371
- def remove_as_friend friend_id
372
- method = "/APP/FriendsFans/removeAsFriend"
373
- attr = {:friend_id=>friend_id}
374
- api(method, attr)
375
- end
376
-
377
- # Become fan of fan_id. To stop being a fan of someone, use set_following
378
- # fan_id:: The ID of the user you want to become fan of
379
- def become_fan fan_id
380
- method = "/APP/FriendsFans/becomeFan"
381
- attr = {:fan_id=>fan_id}
382
- api(method, attr)
383
- end
384
-
385
- # Update following of user_id. A user can befriend someone, but can unfollow them.
386
- # This request is also used to stop following someone as a fan.
387
- # user_id:: The ID of the user you want to follow/unfollow
388
- # follow:: Normally, true if the user should be followed, and false if the user should be unfollowed.
389
- # Actually, toggle the follwing state no matter you give true or false
390
- def set_following user_id, follow=true
391
- method = "/APP/FriendsFans/setFollowing"
392
- attr = {:user_id=>user_id, :follow=>follow}
393
- api(method, attr)
394
- end
395
-
396
- # Returns a JSON object of the logged in users friends (nick name and full name).
397
- # This information can be used to construct auto-completion for private plurking.
398
- # Notice that a friend list can be big, depending on how many friends a user has,
399
- # so this list should be lazy-loaded in your application.
400
- def get_completion
401
- method = "/APP/FriendsFans/getCompletion"
402
- api(method)
403
- end
404
-
405
- # :section: Alerts
406
- # ===General data structures
407
- # The data returned by getActive and getHistory can be of following nature:
408
- # Friendship request:
409
- # {"type": "friendship_request", "from_user": {"nick_name": ...}, "posted": ...}
410
- #
411
- # Friendship pending:
412
- # {"type": "friendship_pending", "to_user": {"nick_name": ...}, "posted": ...}
413
- #
414
- # New fan notification: (does not require actions from the user)
415
- # {"type": "new_fan", "new_fan": {"nick_name": ...}, "posted": ...}
416
- #
417
- # Friendship accepted notification: (does not require actions from the user)
418
- # {"type": "friendship_accepted", "friend_info": {"nick_name": ...}, "posted": ...}
419
- #
420
- # New friend notification: (does not require actions from the user)
421
- # {"type": "new_friend", "new_friend": {"nick_name": ...}, "posted": ...}
422
-
423
- # Return a JSON list of current active alerts.
424
- def get_active
425
- method = "/APP/Alerts/getActive"
426
- api(method)
427
- end
428
-
429
- # Return a JSON list of past 30 alerts.
430
- def get_history
431
- method = "/APP/Alerts/getHistory"
432
- api(method)
433
- end
434
-
435
- # user_id:: The user_id that has asked for friendship.
436
- def add_as_fan user_id
437
- method = "/APP/Alerts/addAsFan"
438
- attr = {:user_id=>user_id}
439
- api(method, attr)
440
- end
441
-
442
- # Accept all friendship requests as fans.
443
- def add_all_as_fan
444
- method = "/APP/Alerts/addAllAsFan"
445
- api(method)
446
- end
447
-
448
- # Accept all friendship requests as friends.
449
- def add_all_as_friends
450
- method = "/APP/Alerts/addAllAsFriends"
451
- api(method)
452
- end
453
-
454
- # Accept user_id as friend.
455
- # user_id:: The user_id that has asked for friendship.
456
- def add_as_friend user_id
457
- method = "/APP/Alerts/addAsFriend"
458
- attr = {:user_id=>user_id}
459
- api(method, attr)
460
- end
461
-
462
- # Deny friendship to user_id.
463
- # user_id:: The user_id that has asked for friendship.
464
- def deny_friendship user_id
465
- method = "/APP/Alerts/denyFriendship"
466
- attr = {:user_id=>user_id}
467
- api(method, attr)
468
- end
469
-
470
- # Remove notification to user with id user_id.
471
- # user_id:: The user_id that the current user has requested friendship for.
472
- def remove_notification user_id
473
- method = "/APP/Alerts/removeNotification"
474
- attr = {:user_id=>user_id}
475
- api(method, attr)
476
- end
477
-
478
- # :section: Search
479
-
480
- # Returns the latest 20 plurks on a search term.
481
- # query:: The query after Plurks.
482
- # offset:: A plurk_id of the oldest Plurk in the last search result.
483
- def plurk_search query, offset=nil
484
- method = "/APP/PlurkSearch/search"
485
- attr = {:query=>query, :offset=>offset}
486
- api(method, attr)
487
- end
488
-
489
- # Returns 10 users that match query, users are sorted by karma.
490
- # query:: The query after users.
491
- # offset:: Page offset, like 10, 20, 30 etc.
492
- def user_search query, offset=0
493
- method = "/APP/UserSearch/search"
494
- attr = {:query=>query, :offset=>offset}
495
- api(method, attr)
496
- end
497
-
498
- # :section: Emoticons
499
-
500
- # Emoticons are a big part of Plurk since they make it easy to express feelings.
501
- # Check out current Plurk emoticons. This call returns a JSON object that looks like:
502
- # {"karma": {"0": [[":-))", "http:\/\/statics.plurk.com\/XXX.gif"], ...], ...},
503
- # "recuited": {"10": [["(bigeyes)", "http:\/\/statics.plurk.com\/XXX.gif"], ...], ...} }
504
- # emoticons["karma"][25] denotes that the user has to have karma over 25 to use these emoticons.
505
- # emoticons["recuited"][10] means that the user has to have user.recuited >= 10 to use these emoticons.
506
- # It's important to check for these things on the client as well,
507
- # since the emoticon levels are checked in the models.
508
- def emoticons_get
509
- method = "/APP/Emoticons/get"
510
- api(method)
511
- end
512
-
513
- # :section: Blocks
514
-
515
- # offset:: What page should be shown, e.g. 0, 10, 20.
516
- def blocks_get offset=0
517
- method = "/APP/Blocks/get"
518
- attr = {:offset=>offset}
519
- api(method, attr)
520
- end
521
-
522
- # user_id:: The id of the user that should be blocked.
523
- def block user_id
524
- method = "/APP/Blocks/block"
525
- attr = {:user_id=>user_id}
526
- api(method, attr)
527
- end
528
-
529
- # user_id: The id of the user that should be unblocked.
530
- def unblock user_id
531
- method = "/APP/Blocks/unblock"
532
- attr = {:user_id=>user_id}
533
- api(method, attr)
534
- end
535
-
536
- # :section: Cliques
537
-
538
- def get_cliques
539
- method = "/APP/Cliques/getCliques"
540
- api(method)
541
- end
542
-
543
- # clique_name:: The name of the new clique
544
- def get_clique clique_name
545
- method = "/APP/Cliques/getClique"
546
- attr = {:clique_name=>clique_name}
547
- api(method, attr)
548
- end
549
-
550
- # clique_name:: The name of the new clique
551
- def create_clique clique_name
552
- method = "/APP/Cliques/createClique"
553
- attr = {:clique_name=>clique_name}
554
- api(method, attr)
555
- end
556
-
557
- # clique_name:: The name of the clique to rename
558
- # new_name:: The name of the new clique
559
- def rename_clique clique_name, new_name
560
- method = "/APP/Cliques/renameClique"
561
- attr = {:clique_name=>clique_name, :new_name=>new_name}
562
- api(method, attr)
563
- end
564
-
565
- # clique_name:: The name of the clique
566
- # user_id:: The user to add to the clique
567
- def cliques_add clique_name, user_id
568
- method = "/APP/Cliques/add"
569
- attr = {:clique_name=>clique_name, :user_id=>user_id}
570
- api(method, attr)
571
- end
572
-
573
- # clique_name:: The name of the clique
574
- # user_id:: The user to remove from the clique
575
- def cliques_remove clique_name, user_id
576
- method = "/APP/Cliques/remove"
577
- attr = {:clique_name=>clique_name, :user_id=>user_id}
578
- api(method, attr)
579
- end
580
-
581
- # :section: PlurkTop
582
-
583
- def get_collections
584
- method = "/APP/PlurkTop/getCollections"
585
- api(method)
586
- end
587
-
588
- # collection_name:: only get plurks in specified collection
589
- # offset:: offset of Plurks in PlurkTop, should be an float, e.g. 0.99.
590
- # limit:: number of plurks returned (default: 30)
591
- # sorting:: the way to sort plurks in PlurkTop, can be "hot" for sorting by popularity or "new" for posted time.
592
- def plurk_top_get_plurks collection_name, limit=nil, offset=nil, sorting=nil
593
- method = "/APP/PlurkTop/getPlurks"
594
- attr = {:collection_name=>collection_name, :limit=>limit, :offset=>offset, :sorting=>sorting}
595
- api(method, attr)
596
- end
597
-
598
- # :section: TJPlurker Provided
599
- # Those methos were all provided by TJPlukrer instead of Plurk
600
-
601
- # Improved from get_friends_by_offset for getting all friends from a user
602
- # return:: An Array
603
- def get_friends user_id, at_least=nil
604
- list = []
605
- offset = 0;
606
- loop{
607
- set = get_friends_by_offset(user_id, 100, offset)
608
- size = set.size
609
- break if size == 0
610
- list |= set
611
- break if at_least && list.size>=at_least
612
- offset += size
613
- }
614
- return list
615
- end
616
-
617
- # Improved from get_fans_by_offset for getting all fans from a user
618
- # return:: An Array
619
- def get_fans user_id, at_least=nil
620
- list = []
621
- offset = 0;
622
- loop{
623
- set = get_fans_by_offset(user_id, 100, offset)
624
- size = set.size
625
- break if size == 0
626
- list |= set
627
- break if at_least && list.size>=at_least
628
- offset += size
629
- }
630
- return list
631
- end
632
-
633
- # Improved from get_following_by_offset for getting all fans from a user
634
- # return:: An Array
635
- def get_following at_least=nil
636
- list = []
637
- offset = 0;
638
- loop{
639
- set = get_following_by_offset(100, offset)
640
- size = set.size
641
- break if size == 0
642
- list |= set
643
- break if at_least && list.size>=at_least
644
- offset += size
645
- }
646
- return list
647
- end
648
-
649
- # Used to follow a lot of users.
650
- # level:: e.g. 1 for following user's all friends, 2 for following user's all friends and all friends of all friends.
651
- def super_become_fan fan_id, level=1
652
- return if level<=0
653
- become_fan(fan_id)
654
- list = []
655
- offset = 0;
656
- loop{
657
- set = get_friends_by_offset(fan_id, 100, offset)
658
- size = set.size
659
- break if size == 0
660
- tg = ThreadGroup.new
661
- set.each{|i|
662
- tg.add Thread.new{
663
- wait_sec = 1
664
- until become_fan(i["uid"]).key?("success_text")
665
- $tjpLog.info(self.class){"Trying to become #{i["uid"]}'s fan in #{wait_sec} seconds"}
666
- sleep(wait_sec)
667
- wait_sec *= 2
668
- end
669
- $tjpLog.info(self.class){"Become #{i["uid"]}'s fan successfully"}
670
- }
671
- }
672
- tg.list.each{|i| i.join }
673
- list |= set
674
- offset += size
675
- }
676
- list.each{|i|
677
- super_become_fan(i["uid"], level-1)
678
- }
679
- end
680
- end
681
-
682
- # Format plurk notification. See TJPlurker#comet_channel
683
- class NotificationData
684
- # Can be +new_plurk+ or +new_response+.
685
- attr_reader :type
686
- attr_reader :response_count
687
- attr_reader :plurk_id
688
- attr_reader :lang
689
- attr_reader :user_id
690
- attr_reader :qualifier
691
- attr_reader :content
692
- # Notification ID.
693
- attr_reader :id
694
- # Time of the listened post.
695
- attr_reader :posted
696
- attr_reader :owner_id
697
- # The original JSON object from server response.
698
- attr_reader :origin
699
-
700
- def initialize member
701
- @type = member['type']
702
- @response_count = member['response_count']
703
- @plurk_id = member['plurk_id']
704
-
705
- @lang = @type=="new_plurk" ? member['lang'] : member['response']['lang']
706
- @user_id = @type=="new_plurk" ? member['user_id'] : member['response']['user_id']
707
- @qualifier = @type=="new_plurk" ? member['qualifier'] : member['response']['qualifier']
708
- @content = @type=="new_plurk" ? member['content'] : member['response']['content']
709
- @id = @type=="new_plurk" ? member['id'] : member['response']['id']
710
- @posted = @type=="new_plurk" ? member['posted'] : member['response']['posted']
711
-
712
- @owner_id = @type=="new_plurk" ? member['owner_id'] : member['plurk']['owner_id']
713
-
714
- @origin = member
715
- end
716
- end
717
-
718
- module_function
719
- # Convert user name into user_id, e.g. Given "tonytonyjan", return 5874158 in Integer
720
- # return:: user id in Integer
721
- def get_uid user_name
722
- open("http://www.plurk.com/#{user_name}").read =~ /"user_id": (\d*)/
723
- raise "User \"#{user_name}\" not found." unless $1
724
- return $1.to_i
725
- end
726
- end