Skip to content

Commit

Permalink
Refactor code to add config feature.
Browse files Browse the repository at this point in the history
Move InstaGPy class constructor arguments to a config.py file. Configurations can now be changed by importing the config file to the project.\n\n from instagpy import config
  • Loading branch information
iSarabjitDhiman committed Jun 22, 2023
1 parent 4d53b44 commit 91d144a
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 41 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ OR
```python
from instagpy import InstaGPy

InstaGPy(proxies=proxies, max_retries=3, use_mutiple_account=False, session_ids=None, min_requests=None, max_requests=None)
InstaGPy(use_mutiple_account=False, session_ids=None, min_requests=None, max_requests=None)
```

> ### Example - Get Basic User Details of a User
Expand All @@ -52,6 +52,22 @@ Check out step by step guide.

[Documentation](instagpy/docs/docs.md)

## Configuration

> ### Example - Config Usage
```python
from instagpy import config

config.PROXY = {"http":"127.0.0.1","https":"127.0.0.1"}
config.TIMEOUT = 10

```

Check out configuration docs for the available settings.

[Configurations](instagpy/docs/config.md)

## Features

- Extracts User's Followers
Expand Down
30 changes: 24 additions & 6 deletions instagpy/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
USER_AGENTS = ["Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Mobile/14G60 Instagram 12.0.0.16.90 (iPhone9,4; iOS 10_3_3; en_US; en-US; scale=2.61; gamut=wide; 1080x1920",
"Instagram 121.0.0.29.119 Android (26/8.0.0; 480dpi; 1080x2032; HUAWEI; FIG-LX1; HWFIG-H; hi6250; en_US; 185203708)"]
MIN_REQUESTS = 3 # Minimum requests to make before shuffling a session
MAX_REQUESTS = 6 # Minimum requests to make before shuffling a session
MAX_RETRIES = 3 # Maximun number of retries for each request
TIMEOUT = 5 # request timeout
# Configuration File

# It is used to reuse generated session. DON'T CHANGE IT.
_DEFAULT_SESSION = None

_USER_AGENTS = ["Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Mobile/14G60 Instagram 12.0.0.16.90 (iPhone9,4; iOS 10_3_3; en_US; en-US; scale=2.61; gamut=wide; 1080x1920",
"Instagram 121.0.0.29.119 Android (26/8.0.0; 480dpi; 1080x2032; HUAWEI; FIG-LX1; HWFIG-H; hi6250; en_US; 185203708)"]

# Minimum requests to make before shuffling a session
MIN_REQUESTS = 3

# Maximum requests to make before shuffling a session
MAX_REQUESTS = 6

# Maximun number of retries for each request
MAX_RETRIES = 3

# request timeout - in seconds
TIMEOUT = 5

# Example {"http":"proxy_here","https":"proxy_here"} Accepts python dictionary.
PROXY = None

# Directory to save and load logged in sessions/cookies
SESSION_DIRECTORY = "Insta Saved Sessions"


Expand Down
62 changes: 62 additions & 0 deletions instagpy/docs/config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<h1 align="center">Configuration</h1>

# Importing

```python
from instagpy import config
```

> ### Example - Config Usage
```python
from instagpy import InstaGPy
from instagpy import config

config.PROXY = {"http":"127.0.0.1","https":"127.0.0.1"}
config.TIMEOUT = 10

insta = InstaGPy()

insta.get_user_basic_details('champagnepapi',print_formatted=True)

```

## Requests Limit for Sessions

```python
# Minimum requests to make before shuffling a session
config.MIN_REQUESTS = 3
```

```python
# Maximum requests to make before shuffling a session
config.MAX_REQUESTS = 6
```

## Retries Limit

```python
# Maximun number of retries for each request
config.MAX_RETRIES = 3
```

## Request Timeout

```python
# request timeout - in seconds
config.TIMEOUT = 5
```

## Using Proxies

```python
# Example {"http":"proxy_here","https":"proxy_here"} Accepts python dictionary.
config.PROXY = None
```

## Saved Sessions Directory

```python
# Directory to save and load logged in sessions/cookies
config.SESSION_DIRECTORY = "Insta Saved Sessions"
```
11 changes: 7 additions & 4 deletions instagpy/docs/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

```python
from instagpy import InstaGPy
from instagpy import config # if want to change configurations.Check out config docs.

insta = InstaGPy(max_retries=None, proxies=None, use_mutiple_account=False, session_ids=None, min_requests=None, max_requests=None,timeout=None)
insta = InstaGPy(use_mutiple_account=False, session_ids=None, min_requests=None, max_requests=None,timeout=None)


"""
Args:
max_retries (int, optional): Number of retires for each request. Defaults to None.
proxies (dict, optional): Proxies as a dictionary {'http': proxy_here,'https':proxy_here}. Residential Proxies are recommended. Defaults to None.
use_mutiple_account (bool, optional): Set to True if want to scrape data with mutiple account sessions So that you don't get blocked. Defaults to False.
session_ids (list, optional): List of Session IDs from Cookies. Applicable only if use_mutiple_accounts is True. Defaults to False.
min_requests (int, optional): Minimum requests to make before shuffling a session ID. Defaults to None.
Expand All @@ -24,10 +23,14 @@ insta = InstaGPy(max_retries=None, proxies=None, use_mutiple_account=False, sess
```python
from instagpy import InstaGPy
from instagpy import config

config.PROXY = {"http":"127.0.0.1","https":"127.0.0.1"}
config.TIMEOUT = 10

insta = InstaGPy()

print(insta.get_user_basic_details('champagnepapi'))
insta.get_user_basic_details('champagnepapi',print_formatted=True)

```

Expand Down
40 changes: 14 additions & 26 deletions instagpy/instagpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@

class InstaGPy:

def __init__(self, max_retries=None, proxies=None, use_mutiple_account=False, session_ids=None, min_requests=None, max_requests=None, timeout=None):
def __init__(self, use_mutiple_account=False, session_ids=None, min_requests=None, max_requests=None):
"""
Args:
max_retries (int, optional): Number of retires for each request. Defaults to None.
proxies (dict, optional): Proxies as a dictionary {'http': proxy_here,'https':proxy_here}. Residential Proxies are recommended. Defaults to None.
use_mutiple_account (bool, optional): Set to True if want to scrape data with mutiple account sessions So that you don't get blocked. Defaults to False.
session_ids (list, optional): List of Session IDs from Cookies. Applicable only if use_mutiple_accounts is True. Defaults to False.
min_requests (int, optional): Minimum requests to make before shuffling a session ID. Defaults to None.
Expand All @@ -39,9 +37,6 @@ def __init__(self, max_retries=None, proxies=None, use_mutiple_account=False, se
self.session_ids_container = None
self.session_ids = session_ids
self.use_mutiple_account = use_mutiple_account
self.max_retries = max_retries or config.MAX_RETRIES
self.timeout = timeout or config.TIMEOUT
self.proxies = proxies
self.generate_session()

@property
Expand All @@ -60,8 +55,7 @@ def _get_meta_data(self):
Returns:
dict: Meta Data.
"""
response = make_request(
path.META_DATA_URL, session=self.session, max_retries=self.max_retries)
response = make_request(path.META_DATA_URL)
return response

def generate_session(self, session_id=None):
Expand All @@ -71,13 +65,12 @@ def generate_session(self, session_id=None):
session_id (str, optional): Session Id from Instagram Session Cookies. Defaults to None.
"""
self.session = requests.Session()
if self.proxies is not None:
self.session.proxies = self.proxies
if config.PROXY is not None:
self.session.proxies = config.PROXY
self.session.verify = False
self.session.headers.update(
{"User-Agent": random.choice(config.USER_AGENTS)})
make_request(path.BASE_URL, session=self.session,
max_retries=self.max_retries)
{"User-Agent": random.choice(config._USER_AGENTS)})
make_request(path.BASE_URL, session=self.session)
response = requests.get(path.LOGIN_URL)
if not response.cookies:
for _ in range(self.max_retries):
Expand All @@ -96,6 +89,7 @@ def generate_session(self, session_id=None):
self.me
except:
pass
config._DEFAULT_SESSION = self.session
return self.session

def shuffle_session(self, ignore_requests_limit=False):
Expand Down Expand Up @@ -259,8 +253,7 @@ def get_user_info(self, username):
Returns:
dict: user info like username,id,bio,follower/following count etc.
"""
response = make_request(path.USER_PROFILE_ENDPOINT.format(
username), session=self.session, max_retries=self.max_retries)
response = make_request(path.USER_PROFILE_ENDPOINT.format(username))
self.shuffle_session()
return response

Expand All @@ -277,8 +270,7 @@ def get_user_data(self, user_id):
if not self.logged_in():
self.login()
user_id = self.get_user_id(user_id)
response = make_request(path.USER_DATA_ENDPOINT.format(
user_id), session=self.session, max_retries=self.max_retries)
response = make_request(path.USER_DATA_ENDPOINT.format(user_id))
self.shuffle_session()
return response

Expand Down Expand Up @@ -354,8 +346,7 @@ def get_user_friends(self, username, followers_list=False, followings_list=False
count=max_data, end_cursor=end_cursor)

try:
response = make_request(
url, params=query_params, session=self.session, max_retries=self.max_retries)
response = make_request(url, params=query_params)
if followers_list and user['is_verified']:
data = response['data']['user']['edge_followed_by']
has_next_page = data['page_info']['has_next_page']
Expand Down Expand Up @@ -428,7 +419,7 @@ def filter_by_date(user_posts):
query=path.USER_FEED_QUERY, user_id=user_id, count=50, end_cursor=end_cursor, is_graphql=True)
try:
response = make_request(
path.GRAPHQL_URL, params=query_params, session=self.session, max_retries=self.max_retries)
path.GRAPHQL_URL, params=query_params)
data = response['data']['user']['edge_owner_to_timeline_media']
posts_count = data['count']
has_next_page = data['page_info']['has_next_page']
Expand Down Expand Up @@ -472,8 +463,7 @@ def get_post_details(self, post_url):
url = path.GRAPHQL_URL
query_params = self.generate_query(
query=path.POST_DETAILS_QUERY, shortcode=post_id, is_graphql=True)
response = make_request(
url, params=query_params, session=self.session, max_retries=self.max_retries)
response = make_request(url, params=query_params)
self.shuffle_session()
return response

Expand Down Expand Up @@ -511,8 +501,7 @@ def get_about_user(self, username, print_formatted=True):
user_id = self.get_user_id(username)
data = {'referer_type': 'ProfileUsername', 'target_user_id': user_id, 'bk_client_context': {
'bloks_version': path.ABOUT_USER_QUERY, 'style_id': 'instagram'}, 'bloks_versioning_id': path.ABOUT_USER_QUERY}
response = make_request(path.ABOUT_USER_URL, method='POST', data=data,
session=self.session, max_retries=self.max_retries)
response = make_request(path.ABOUT_USER_URL, method='POST', data=data)
if print_formatted:
return utils.format_about_data(response)
self.shuffle_session()
Expand Down Expand Up @@ -544,8 +533,7 @@ def get_hashtag_posts(self, hashtag=None, end_cursor=None, max=None):
query=path.HASHTAG_QUERY, hashtag=hashtag, count=max_data, end_cursor=end_cursor, is_graphql=True)

try:
response = make_request(
url, params=query_params, session=self.session, max_retries=self.max_retries)
response = make_request(url, params=query_params)
data = response['data']['hashtag']['edge_hashtag_to_media']
has_next_page = data['page_info']['has_next_page']
end_cursor = data['page_info']['end_cursor']
Expand Down
7 changes: 4 additions & 3 deletions instagpy/request_util.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import requests
import bs4
from . import utils
from . import config


def make_request(url, session=None, method=None, max_retries=None, timeout=None, **kwargs):
if method is None:
method = "GET"
if max_retries is None:
max_retries = 3
max_retries = config.MAX_RETRIES or 3
if session is None:
session = requests.Session()
session = config._DEFAULT_SESSION or requests.Session()
if timeout is None:
timeout = 30
timeout = config.TIMEOUT or 30
for retry_count, _ in enumerate(range(max_retries), start=1):
try:
response_text = ""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup

VERSION = "0.1.7"
VERSION = "0.1.8"
SHORT_DESCRIPTION = "InstaGPy is an Instagram Unofficial API to extract data from Instargam Profiles. Scrape data from user's profile like username, userid, bio, email, phone, followers/followings list, profile media, account_type, etc."

with open("requirements.txt") as file:
Expand Down

0 comments on commit 91d144a

Please sign in to comment.