Skip to content

Commit

Permalink
Merge pull request #378 from GramAddict/update-fixes
Browse files Browse the repository at this point in the history
Fix account selecting
  • Loading branch information
mastrolube authored Mar 9, 2024
2 parents 53b7c54 + 886f314 commit 9e483bb
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 60 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
/dist
/gramaddict.egg-info
/.idea
/test
/.vscode
*/*.yml
*/*.json
Expand All @@ -17,7 +16,6 @@ accounts
Pipfile
Pipfile.lock
*.pdf
*.txt
*.mp4
*.log*
*.ini
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# Changelog
## 3.2.10 (2024-03-09)
### Fix
- account selecting
- function specialization for load and clean txt file
- better logging for the user
### Others
- test for load and clean txt file

## 3.2.9 (2024-02-10)
### Fix
- remove pandas as dependency for telegram reports
Expand Down
4 changes: 2 additions & 2 deletions GramAddict/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Human-like Instagram bot powered by UIAutomator2"""

__version__ = "3.2.9"
__tested_ig_version__ = "263.2.0.19.104"
__version__ = "3.2.10"
__tested_ig_version__ = "300.0.0.29.110"

from GramAddict.core.bot_flow import start_bot

Expand Down
3 changes: 3 additions & 0 deletions GramAddict/core/bot_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ def start_bot(**kwargs):
logger.warning(
"If you press ENTER, you are aware of this and will not ask for support in case of a crash."
)
logger.warning(
"If you want to avoid pressing ENTER next run, add allow-untested-ig-version: True in your config.yml file. (read the docs for more info)"
)
input()

except Exception as e:
Expand Down
102 changes: 48 additions & 54 deletions GramAddict/core/interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from os import path
from random import choice, randint, shuffle, uniform
from time import sleep, time
from typing import Optional, Tuple
from typing import Optional, Tuple, List

import emoji
import spintax
Expand Down Expand Up @@ -734,7 +734,7 @@ def _send_PM(
message = load_random_message(my_username)
if message is None:
logger.warning(
"You forgot to populate your PM list! If you don't want to comment set 'pm-percentage: 0'"
"If you don't want to comment set 'pm-percentage: 0' in your config.yml."
)
device.back()
return False
Expand Down Expand Up @@ -780,76 +780,70 @@ def _send_PM(
return False


def load_random_message(my_username: str) -> Optional[str]:
def _load_and_clean_txt_file(
my_username: str, txt_filename: str
) -> Optional[List[str]]:
def nonblank_lines(f):
for ln in f:
line = ln.rstrip()
if line:
yield line

lines = []
file_name = os.path.join(storage.ACCOUNTS, my_username, storage.FILENAME_MESSAGES)
file_name = os.path.join(storage.ACCOUNTS, my_username, txt_filename)
if path.isfile(file_name):
try:
with open(file_name, "r", encoding="utf-8") as f:
for line in nonblank_lines(f):
lines.append(line)
random_message = choice(lines)
if random_message != "":
return emoji.emojize(
spintax.spin(random_message.replace("\\n", "\n")),
use_aliases=True,
)
else:
return None
if lines:
return lines
logger.warning(f"{file_name} is empty! Check your account folder.")
return None
except Exception as e:
logger.error(f"Error: {e}.")
return None
logger.warning(f"{file_name} not found! Check your account folder.")
return None


def load_random_comment(my_username: str, media_type: MediaType) -> Optional[str]:
def nonblank_lines(f):
for ln in f:
line = ln.rstrip()
if line:
yield line
def load_random_message(my_username: str) -> Optional[str]:
lines = _load_and_clean_txt_file(my_username, storage.FILENAME_MESSAGES)
if lines is not None:
random_message = choice(lines)
return emoji.emojize(
spintax.spin(random_message.replace("\\n", "\n")),
use_aliases=True,
)
return None

lines = []
file_name = os.path.join(storage.ACCOUNTS, my_username, storage.FILENAME_COMMENTS)
if path.isfile(file_name):
with open(file_name, "r", encoding="utf-8") as f:
for line in nonblank_lines(f):
lines.append(line)
try:
photo_header = lines.index("%PHOTO")
video_header = lines.index("%VIDEO")
carousel_header = lines.index("%CAROUSEL")
except ValueError:
logger.warning(
f"You didn't follow the rules of sections for {file_name}! Look at config example."
)
return None
photo_comments = lines[photo_header + 1 : video_header]
video_comments = lines[video_header + 1 : carousel_header]
carousel_comments = lines[carousel_header + 1 :]
random_comment = ""
if media_type == MediaType.PHOTO:
random_comment = (
choice(photo_comments) if len(photo_comments) > 0 else ""
)
elif media_type in (MediaType.VIDEO, MediaType.IGTV, MediaType.REEL):
random_comment = (
choice(video_comments) if len(video_comments) > 0 else ""
)
elif media_type == MediaType.CAROUSEL:
random_comment = (
choice(carousel_comments) if len(carousel_comments) > 0 else ""
)
if random_comment != "":
return emoji.emojize(spintax.spin(random_comment), use_aliases=True)
else:
return None

def load_random_comment(my_username: str, media_type: MediaType) -> Optional[str]:
lines = _load_and_clean_txt_file(my_username, storage.FILENAME_COMMENTS)
if lines is None:
return None
try:
photo_header = lines.index("%PHOTO")
video_header = lines.index("%VIDEO")
carousel_header = lines.index("%CAROUSEL")
except ValueError:
logger.warning(
f"You didn't follow the rules for sections in your {storage.FILENAME_COMMENTS} txt file! Look at config example."
)
return None
photo_comments = lines[photo_header + 1 : video_header]
video_comments = lines[video_header + 1 : carousel_header]
carousel_comments = lines[carousel_header + 1 :]
random_comment = ""
if media_type == MediaType.PHOTO:
random_comment = choice(photo_comments) if len(photo_comments) > 0 else ""
elif media_type in (MediaType.VIDEO, MediaType.IGTV, MediaType.REEL):
random_comment = choice(video_comments) if len(video_comments) > 0 else ""
elif media_type == MediaType.CAROUSEL:
random_comment = choice(carousel_comments) if len(carousel_comments) > 0 else ""
if random_comment != "":
return emoji.emojize(spintax.spin(random_comment), use_aliases=True)
else:
logger.warning(f"{file_name} not found!")
return None


Expand Down
1 change: 1 addition & 0 deletions GramAddict/core/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ def __init__(self, APP_ID):
self.UNIFIED_FOLLOW_LIST_TAB_LAYOUT = (
f"{APP_ID}:id/unified_follow_list_tab_layout"
)
self.USERNAME_TEXTVIEW = f"{APP_ID}:id/username_textview"
self.VIDEO_CONTAINER = f"{APP_ID}:id/video_container"
self.VIEW_PLAY_BUTTON = f"{APP_ID}:id/view_play_button"
self.ZOOMABLE_VIEW_CONTAINER = f"{APP_ID}:id/zoomable_view_container"
Expand Down
2 changes: 1 addition & 1 deletion GramAddict/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ def changeToUsername(self, username: str):
def _find_username(self, username, has_scrolled=False):
list_view = self.device.find(resourceId=ResourceID.LIST)
username_obj = self.device.find(
resourceId=ResourceID.ROW_USER_TEXTVIEW,
resourceIdMatches=f"{ResourceID.ROW_USER_TEXTVIEW}|{ResourceID.USERNAME_TEXTVIEW}",
textMatches=case_insensitive_re(username),
)
if username_obj.exists(Timeout.SHORT):
Expand Down
2 changes: 1 addition & 1 deletion GramAddict/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# that file is deprecated, current version is now stored in GramAddict/__init__.py
__version__ = "3.2.9"
__version__ = "3.2.10"
24 changes: 24 additions & 0 deletions test/test_load_txt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from GramAddict.core.interaction import _load_and_clean_txt_file


def test_load_txt_ok(mocker):
mocker.patch("os.path.join", return_value="txt/txt_ok.txt")
message = _load_and_clean_txt_file("test_user", "txt_filename")
assert message is not None
assert message == [
"Hello, test_user! How are you today?",
"Hello everyone!",
"Goodbye, test_user! Have a great day!",
]


def test_load_txt_empty(mocker):
mocker.patch("os.path.join", return_value="txt/txt_empty.txt")
message = _load_and_clean_txt_file("test_user", "txt_filename")
assert message is None


def test_load_txt_not_exists(mocker):
mocker.patch("os.path.join", return_value="txt/txt_not_exists.txt")
message = _load_and_clean_txt_file("test_user", "txt_filename")
assert message is None
5 changes: 5 additions & 0 deletions test/txt/txt_empty.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@





6 changes: 6 additions & 0 deletions test/txt/txt_ok.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

Hello, test_user! How are you today?
Hello everyone!

Goodbye, test_user! Have a great day!

0 comments on commit 9e483bb

Please sign in to comment.