Skip to content

Commit

Permalink
add support for retrieving follow requests and approving follow reque…
Browse files Browse the repository at this point in the history
…sts.
  • Loading branch information
Stan van Rooy committed Jul 26, 2020
1 parent 1e0840e commit 63f9acc
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 3 deletions.
21 changes: 21 additions & 0 deletions examples/friendships/approve_all_follow_requests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from src.api.client import ApiClient
import os

from src.api.actions.structs.search import SearchUsername
from src.api.actions.structs.friendships import ApproveFollowRequest, PendingFollowRequests


if __name__ == '__main__':
if os.path.isfile('./.instauto.save'):
client = ApiClient.initiate_from_file('./.instauto.save')
else:
client = ApiClient(user_name="your_username", password="your_password")
client.login()
client.save_to_disk('./.instauto.save')

p = PendingFollowRequests()
users = client.get_follow_requests(p)

for user in users: # approves all requests
a = ApproveFollowRequest.create(str(user['pk']))
resp = client.approve_follow_request(a)
21 changes: 21 additions & 0 deletions examples/friendships/approve_follow_request_of_specific_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from src.api.client import ApiClient
import os

from src.api.actions.structs.search import SearchUsername
from src.api.actions.structs.friendships import ApproveFollowRequest


if __name__ == '__main__':
if os.path.isfile('./.instauto.save'):
client = ApiClient.initiate_from_file('./.instauto.save')
else:
client = ApiClient(user_name="your_username", password="your_password")
client.login()
client.save_to_disk('./.instauto.save')

s = SearchUsername.create("stan058_", 1)
resp = client.search_username(s).json()
user_id = resp['users'][0]['pk']

a = ApproveFollowRequest.create(user_id)
resp = client.approve_follow_request(a)
30 changes: 29 additions & 1 deletion original_messages/friendships.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,33 @@
]
}
}
}
},
{
"endpoint": "friendships/pending/",
"response": {
"big_list": false,
"global_blacklist_sample": null,
"next_max_id": null,
"page_size": 200,
"sections": null,
"status": "ok",
"suggested_users": {
"suggestions": []
},
"users": [
{
"account_badges": [],
"full_name": "",
"has_anonymous_profile_picture": false,
"is_private": true,
"is_verified": false,
"latest_reel_media": 0,
"pk": 2097017052,
"profile_pic_id": "",
"profile_pic_url": "",
"username": ""
}
]

},
]
34 changes: 32 additions & 2 deletions src/api/actions/friendships.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from requests import Session, Response
from typing import Union, Callable, Tuple
from typing import Union, Callable, Tuple, List
from .structs.friendships import CreateFriendship, DestroyFriendship, RemoveFriendship, ShowFriendship, \
ShowFriendshipFollowers, ShowFriendshipFollowing
ShowFriendshipFollowers, ShowFriendshipFollowing, PendingFollowRequests, ApproveFollowRequest
from ..structs import State, Method


Expand Down Expand Up @@ -142,3 +142,33 @@ def get_following(self, obj: ShowFriendshipFollowing) -> Tuple[ShowFriendshipFol
obj.max_id = as_json['next_max_id']
obj.page += 1
return obj, resp

def get_follow_requests(self, obj: PendingFollowRequests) -> List[dict]:
"""
Returns
-------
List[dict]
A list which contains all pending follow requests. If successfull, it will return an list of the
following structures:
{
"account_badges": [],
"full_name": "",
"has_anonymous_profile_picture": false,
"is_private": true,
"is_verified": false,
"latest_reel_media": 0,
"pk": 2097017052,
"profile_pic_id": "",
"profile_pic_url": "",
"username": ""
}
"""
resp = self._request('friendships/pending/', Method.GET)
parsed = resp.json()
return parsed['users']

def approve_follow_request(self, obj: ApproveFollowRequest) -> Response:
obj._csrftoken = self._session.cookies.get('csrftoken', domain='instagram.com')
obj._uid = self.state.user_id
obj._uuid = self.state.uuid
return self._request(f'friendships/approve/{obj.user_id}/', Method.POST, data=obj.__dict__)
26 changes: 26 additions & 0 deletions src/api/actions/structs/friendships.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,29 @@ def create(cls, user_id: str) -> "ShowFriendshipFollowing":
i.user_id = user_id
i.rank_token = uuid.uuid4()
return i


class PendingFollowRequests:
@classmethod
def create(cls):
return cls()


class ApproveFollowRequest:
surface: str = 'follow_requests'
radio_type: str = 'wifi-none'
user_id: str = None
_csrftoken: str = None
_uid: str = None
_uuid: str = None

@classmethod
def create(cls, user_id, **kwargs):
i = cls()
for k, v in kwargs.items():
if hasattr(i, k):
setattr(i, k, v)
else:
logger.warning("{} was sent as a keyword argument, but isn't supported.")
i.user_id = user_id
return i

0 comments on commit 63f9acc

Please sign in to comment.