[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added limitations and config from file support #18

Merged
merged 1 commit into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- This is to be used for saving ids, etc.
- Added basic limitations support.
- `session.configuration.limitations`.
- Added new function to load a configuration from a file.

### Fixed
- Fixed all type checking issues displayed in VS Code.
Expand Down
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ def read(file: str) -> str:
keywords=[
'automation',
'bot',
'ig',
'insta',
'instagram',
'instagrapi',
'instapy',
'instapy2'
'instapy2',
'python',
'python3'
],
license_file='LICENSE.md',
long_description=read(file='README.md'),
Expand Down
18 changes: 18 additions & 0 deletions src/instapy2/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
from instagrapi import Client
from instagrapi.types import Media

from json import load
from os import getcwd, sep
from typing import List

import random

class Configuration:
def __init__(self, session: Client):
self.session = session

self.comments = CommentsUtility(session=session)
self.follows = FollowsUtility(session=session)
self.interactions = InteractionsUtility()
Expand All @@ -28,6 +32,20 @@ def __init__(self, session: Client):
self.location = LocationHelper(session=session)
self.people = PeopleHelper(session=session)

def from_file(self):
config_json = load(fp=open(file=f'{getcwd()}{sep}config.json'))
configuration = config_json['accounts'][self.session.username]['configuration']
print(config_json)

if 'comments' in configuration: self.comments.from_json(data=configuration['comments'])
if 'follows' in configuration: self.follows.from_json(data=configuration['follows'])
if 'interactions' in configuration: self.interactions.from_json(data=configuration['interactions'])
if 'likes' in configuration: self.likes.from_json(data=configuration['likes'])
if 'limitations' in configuration: self.limitations.from_json(data=configuration['limitations'])
if 'messages' in configuration: self.messages.from_json(data=configuration['messages'])
if 'people' in configuration: self.people.from_json(data=configuration['people'])


def set_pexels_api_key(self, key: str):
self.pexels_api_key = key

Expand Down
6 changes: 5 additions & 1 deletion src/instapy2/helpers/people.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from instagrapi import Client
from instagrapi.types import Media

from typing import List
from typing import Dict, List

class PeopleHelper:
def __init__(self, session: Client):
Expand All @@ -10,6 +10,10 @@ def __init__(self, session: Client):
self.friends_to_skip = []
self.users_to_skip = []

def from_json(self, data: Dict):
self.friends_to_skip = data['friends_to_skip'] or []
self.users_to_skip = data['users_to_skip'] or []

def skip_friends(self, usernames: List[str]):
"""
Sets which users to skip when unfollowing. Commenting and liking will still occur.
Expand Down
8 changes: 7 additions & 1 deletion src/instapy2/utilities/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from instagrapi import Client
from instagrapi.types import Media

from typing import List, Tuple, Union
from typing import Dict, List, Tuple, Union

class CommentsUtility:
def __init__(self, session: Client):
Expand All @@ -13,6 +13,12 @@ def __init__(self, session: Client):
self.enabled_for_liked_media = False
self.percentage = 0

def from_json(self, data: Dict):
self.comments = data['comments'] or []
self.enabled = data['enabled'] or False
self.enabled_for_liked_media = data['enabled_for_liked_media'] or False
self.percentage = data['percentage'] or 0

def set_comments(self, comments: List[str]):
"""
Sets the comments to be used when commenting on media.
Expand Down
7 changes: 6 additions & 1 deletion src/instapy2/utilities/follows.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from instagrapi import Client
from instagrapi.types import UserShort

from typing import Tuple, Union
from typing import Dict, Tuple, Union

class FollowsUtility:
def __init__(self, session: Client):
Expand All @@ -11,6 +11,11 @@ def __init__(self, session: Client):
self.percentage = 0
self.times = 1

def from_json(self, data: Dict):
self.enabled = data['enabled'] or False
self.percentage = data['percentage'] or 0
self.times = data['times'] or 1

def set_enabled(self, enabled: bool):
self.enabled = enabled

Expand Down
8 changes: 8 additions & 0 deletions src/instapy2/utilities/interactions.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
from instagrapi import Client

from typing import Dict

class InteractionsUtility:
def __init__(self):
self.amount = 0
self.enabled = False
self.percentage = 0
self.randomize = False

def from_json(self, data: Dict):
self.amount = data['amount'] or 0
self.enabled = data['enabled'] or False
self.percentage = data['percentage'] or 0
self.randomize = data['randomize'] or False

def set_amount(self, amount: int):
self.amount = amount

Expand Down
6 changes: 6 additions & 0 deletions src/instapy2/utilities/likes.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
from instagrapi import Client
from instagrapi.types import Media

from typing import Dict

class LikesUtility:
def __init__(self, session: Client):
self.session = session

self.enabled = False
self.percentage = 0

def from_json(self, data: Dict):
self.enabled = data['enabled'] or False
self.percentage = data['percentage'] or 0

def set_enabled(self, enabled: bool):
self.enabled = enabled

Expand Down
7 changes: 6 additions & 1 deletion src/instapy2/utilities/limitations.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from instagrapi import Client
from instagrapi.types import Media, UserShort

from typing import Tuple
from typing import Dict, Tuple

class LimitationsUtility:
def __init__(self, session: Client):
Expand All @@ -11,6 +11,11 @@ def __init__(self, session: Client):
self.enabled = False
self.followers_range = (1, 1000)

def from_json(self, data: Dict):
self.commenters_range = (data['commenters_range'][0] or 1, data['commenters_range'][1] or 100)
self.enabled = data['enabled'] or False
self.followers_range = (data['followers_range'][0] or 1, data['followers_range'][1] or 1)

def set_commenters_range(self, range: Tuple[int, int]):
self.commenters_range = range

Expand Down
17 changes: 6 additions & 11 deletions src/instapy2/utilities/messages.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
from instagrapi import Client
from instagrapi.types import UserShort

from typing import List, Tuple, Union
from typing import Dict, List, Tuple, Union

class MessageUtility:
def __init__(self, session: Client):
self.session = session

self.enabled = False
self.follower_range = 1, 10000
self.messages = []
self.percentage = 0

def from_json(self, data: Dict):
self.enabled = data['enabled'] or False
self.messages = data['messages'] or []
self.percentage = data['percentage'] or 0

def set_enabled(self, enabled: bool):
"""
Enables the ability to direct message a user.
Expand All @@ -20,15 +24,6 @@ def set_enabled(self, enabled: bool):
"""
self.enabled = enabled

def set_follower_range(self, follower_range: Tuple[int, int]):
"""
Sets the limit to how many followers a user can have to be direct messaged.

:param follower_range: follower_range=(1, 100) means a user with between 1 and 100
followers will be direct messaged.
"""
self.follower_range = follower_range

def set_messages(self, messages: List[str]):
"""
Sets the messages to be used when direct messaging a user.
Expand Down