Skip to content

Commit

Permalink
Update shuffle_session method.
Browse files Browse the repository at this point in the history
Now Supports shuffling sessions manually regardless of specified min/max number of requests.
  • Loading branch information
iSarabjitDhiman committed Jun 7, 2023
1 parent b88b46c commit 55a05e6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
16 changes: 16 additions & 0 deletions instagpy/docs/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ login(username=None, password=None, show_saved_sessions=True, save_session=True)
"""
```

## Shuffle Session Manually.

```python
shuffle_session(self, ignore_requests_limit=False):

"""
Shuffle session/cookies. Takes a new session ID from self.session_ids only if using with mutiple accounts.
Args:
ignore_requests_limit (bool, optional): Set to True to shuffle session manually regardless of min/max number of requests. Defaults to False.
Returns:
Session object: Session Object i.e. self.session
"""
```

## Get User ID of a User.

```python
Expand Down
28 changes: 21 additions & 7 deletions instagpy/instagpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ def __init__(self, max_retries=None, proxies=None, use_mutiple_account=False, se
raise Exception(
'Either Pass a list of session_ids or set use_multiple_account to False.')
if use_mutiple_account and session_ids:
self.session_ids = session_ids
self.current_request_number = 1
# shuffle session randomly after every nth request.
self.min_requests = min_requests or 3
self.max_requests = max_requests or 6
self.shuffle_session_after = random.randint(
self.min_requests, self.max_requests)
self.session_ids_container = None
self.session_ids = session_ids
self.use_mutiple_account = use_mutiple_account
self.max_retries = max_retries or 3
self.session = requests.Session()
Expand Down Expand Up @@ -70,17 +70,31 @@ def generate_session(self, session_id=None):
self.session.headers.update(
{'x-csrftoken': csrf_token, 'X-Requested-With': "XMLHttpRequest", 'Referer': login_page_url})

def shuffle_session(self):
def shuffle_session(self, ignore_requests_limit=False):
"""Shuffle session/cookies. Takes a new session ID from self.session_ids if using with mutiple accounts.
Args:
ignore_requests_limit (bool, optional): Set to True to shuffle session manually regardless of min/max number of requests. Defaults to False.
Returns:
Session object: Session Object i.e. self.session
"""
if not self.use_mutiple_account:
return
self.current_request_number += 1
if self.current_request_number % self.shuffle_session_after == 0:
self.shuffle_session_after = random.randint(
self.min_requests, self.max_requests)
change_session = False
if ignore_requests_limit:
change_session = True
if not ignore_requests_limit:
self.current_request_number += 1
if self.current_request_number % self.shuffle_session_after == 0:
self.shuffle_session_after = random.randint(
self.min_requests, self.max_requests)
change_session = True
if change_session:
if not self.session_ids_container:
self.session_ids_container = self.session_ids.copy()
session_id = self.session_ids_container.pop()
self.generate_session(session_id=session_id)
return self.generate_session(session_id=session_id)

def generate_query(self, query=None, count=None, user_id=None, end_cursor=None, search_surface=None, shortcode=None, is_graphql=False):
"""Generates query paramters for instagram api requests.
Expand Down

0 comments on commit 55a05e6

Please sign in to comment.