Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Feb 9, 2023. It is now read-only.

Commit a2c717d

Browse files
committed
added friendship methods
1 parent 16e82d0 commit a2c717d

File tree

1 file changed

+65
-1
lines changed

1 file changed

+65
-1
lines changed

twitapi/__init__.py

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,73 @@ def users_show(self, user_id=None, screen_name=None):
389389
else:
390390
params['screen_name'] = screen_name
391391

392-
return self.request(self.base_api_url+'/users/show.json?%s' % urlencode(params), "GET")
392+
return self.request(self.base_api_url+'/users/show.json?%s' %
393+
urlencode(params), "GET")
394+
395+
def friendships_create(self, user_id=None, screen_name=None, follow=False):
396+
"""
397+
Allows the authenticating users to follow the user specified in
398+
the ID parameter. Returns the befriended user in the requested
399+
format when successful. Returns a string describing the failure
400+
condition when unsuccessful. If you are already friends with the
401+
user an HTTP 403 will be returned.
402+
403+
Setting follow to True enables notifications for the target user
404+
in addition to becoming friends.
405+
"""
406+
if not user_id and not screen_name:
407+
raise Exception("A user_id or screen_name must be provided.")
408+
409+
if user_id and screen_name:
410+
raise Exception("A user_id OR screen_name must be provided.")
411+
412+
params = {}
413+
if user_id:
414+
params['user_id'] = user_id
415+
else:
416+
params['screen_name'] = screen_name
417+
if follow:
418+
params['follow'] = 'true'
419+
420+
return self.request(self.base_api_url+'/friendships/create.json',
421+
"POST", urlencode(params))
393422

423+
def friendships_destroy(self, user_id=None, screen_name=None):
424+
"""
425+
Allows the authenticating users to unfollow the user specified in
426+
the ID parameter. Returns the unfollowed user in the requested
427+
format when successful. Returns a string describing the failure
428+
condition when unsuccessful.
429+
"""
430+
if not user_id and not screen_name:
431+
raise Exception("A user_id or screen_name must be provided.")
432+
433+
if user_id and screen_name:
434+
raise Exception("A user_id OR screen_name must be provided.")
435+
436+
params = {}
437+
if user_id:
438+
params['user_id'] = user_id
439+
else:
440+
params['screen_name'] = screen_name
441+
442+
return self.request(self.base_api_url+'/friendships/destroy.json',
443+
"POST", urlencode(params))
394444

445+
def friendships_exists(self, user_a, user_b):
446+
"""
447+
Tests for the existance of friendship between two users.
448+
Will return true if user_a follows user_b, otherwise will return false.
449+
450+
user_a and user_b can be the user_id or screen_name of the users.
451+
"""
452+
params = {
453+
'user_a': user_a,
454+
'user_b': user_b
455+
}
456+
457+
return self.request(self.base_api_url+'/friendships/exists.json?%s' %
458+
urlencode(params), "GET")
395459

396460
__all__ = ["OAuth", "BasicAuthentication", "Client"]
397461

0 commit comments

Comments
 (0)