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

Updated deprecated module imports and various fixes to make it work out-of-the-box #6784

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
10 changes: 4 additions & 6 deletions instapy/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options as Firefox_Options
from selenium.webdriver.firefox.service import Service
from selenium.webdriver import Remote
from webdriverdownloader import GeckoDriverDownloader

Expand Down Expand Up @@ -120,12 +121,9 @@ def set_selenium_local_session(

# prefer user path before downloaded one
driver_path = geckodriver_path or get_geckodriver()
browser = webdriver.Firefox(
firefox_profile=firefox_profile,
executable_path=driver_path,
log_path=geckodriver_log,
options=firefox_options,
)
service = Service(driver_path, log_file=geckodriver_log)

browser = webdriver.Firefox(options=firefox_options, service=service)

# add extension to hide selenium
browser.install_addon(create_firefox_extension(), temporary=True)
Expand Down
10 changes: 5 additions & 5 deletions instapy/clarifai_util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Module which handles the clarifai api and checks
the image for invalid content"""
from clarifai.rest import ClarifaiApp, Workflow
from clarifai.client import App, Workflow
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By

Expand Down Expand Up @@ -28,7 +28,7 @@ def check_image(
by checking link against models included in the workflow. If a workflow
hasn't been provided, InstaPy will check images against given model(
s)"""
clarifai_api = ClarifaiApp(api_key=clarifai_api_key)
clarifai_api = App(api_key=clarifai_api_key)
clarifai_tags = []

if proxy is not None:
Expand Down Expand Up @@ -84,7 +84,7 @@ def check_image(
)
return False, [], clarifai_tags

for (tags, should_comment, comments) in img_tags:
for tags, should_comment, comments in img_tags:
if should_comment and given_tags_in_result(tags, clarifai_tags, full_match):
return True, comments, clarifai_tags
elif given_tags_in_result(tags, clarifai_tags, full_match):
Expand Down Expand Up @@ -145,11 +145,11 @@ def get_clarifai_response(clarifai_api, clarifai_model, source_link, check_video
keys in the dictionary below, model value is used in
clarifai_api.models.get(). Useful for custom models."""
# List of models which support video inputs
video_models = ["apparel", "food", "general", "nsfw", "travel", "wedding"]
video_models = ["Apparel", "food", "general", "nsfw", "travel", "wedding"]
clarifai_models = {
"general": "general-v1.3",
"nsfw": "nsfw-v1.0",
"apparel": "apparel",
"Apparel": "Apparel",
"celebrity": "celeb-v1.3",
"color": "color",
"demographics": "demographics",
Expand Down
11 changes: 8 additions & 3 deletions instapy/comment_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,10 @@ def get_comments_on_post(
amount = amount * 3

# check if commenting on the post is enabled
(commenting_approved, disapproval_reason,) = verify_commenting(
(
commenting_approved,
disapproval_reason,
) = verify_commenting(
browser,
None,
None,
Expand Down Expand Up @@ -406,10 +409,12 @@ def process_comments(
logfolder,
publish=True,
):

# comments
if delimit_commenting:
(commenting_approved, disapproval_reason,) = verify_commenting(
(
commenting_approved,
disapproval_reason,
) = verify_commenting(
browser,
max_comments,
min_comments,
Expand Down
1 change: 0 additions & 1 deletion instapy/commenters_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,6 @@ def likers_from_photo(browser, amount=20, logger=None):
or (len(user_list) != previous_len)
and (len(user_list) < amount)
):

if previous_len + 10 >= amount:
logger.info("Scrolling finished...")
if amount < 10:
Expand Down
16 changes: 4 additions & 12 deletions instapy/instapy.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,12 +730,14 @@ def set_smart_hashtags(
return

for tag in tags:

# CI Travis alert for Python3.5 and apidisplaypurposes
from apidisplaypurposes import displaypurposes

myToken = displaypurposes.generate_api_token(tag, Settings.user_agent)
head = {"user-agent": displaypurposes.USER_AGENTS["chrome"], "api-token": myToken}
head = {
"user-agent": displaypurposes.USER_AGENTS["chrome"],
"api-token": myToken,
}
req = requests.get(
"https://apidisplaypurposes.com/tag/{}".format(tag), headers=head
)
Expand Down Expand Up @@ -1406,7 +1408,6 @@ def set_skip_users(
skip_bio_keyword: list = [],
mandatory_bio_keywords: list = [],
):

self.skip_business = skip_business
self.skip_private = skip_private
self.skip_no_profile_pic = skip_no_profile_pic
Expand Down Expand Up @@ -1437,7 +1438,6 @@ def set_skip_users(
def set_delimit_liking(
self, enabled: bool = False, max_likes: int = None, min_likes: int = None
):

self.delimit_liking = True if enabled is True else False
self.max_likes = max_likes
self.min_likes = min_likes
Expand All @@ -1449,7 +1449,6 @@ def set_delimit_commenting(
min_comments: int = None,
comments_mandatory_words: list = [],
):

self.delimit_commenting = True if enabled is True else False
self.max_comments = max_comments
self.min_comments = min_comments
Expand Down Expand Up @@ -1645,7 +1644,6 @@ def like_by_locations(
"read", user_name, self.follow_times, self.logger
)
):

follow_state, msg = follow_user(
self.browser,
"post",
Expand Down Expand Up @@ -2085,7 +2083,6 @@ def like_by_tags(
"read", user_name, self.follow_times, self.logger
)
):

follow_state, msg = follow_user(
self.browser,
"post",
Expand Down Expand Up @@ -2682,7 +2679,6 @@ def interact_by_users(

# follow
if following and not (self.dont_follow_inap_post and inap_img > 0):

follow_state, msg = follow_user(
self.browser,
track,
Expand Down Expand Up @@ -2999,7 +2995,6 @@ def interact_by_users_tagged_posts(

# follow
if following and not (self.dont_follow_inap_post and inap_img > 0):

follow_state, msg = follow_user(
self.browser,
"profile",
Expand Down Expand Up @@ -3455,7 +3450,6 @@ def interact_user_likers(
def interact_user_following(
self, usernames: list, amount: int = 10, randomize: bool = False
):

if self.aborting:
return self

Expand Down Expand Up @@ -4853,7 +4847,6 @@ def follow_by_tags(
media: str = None,
interact: bool = False,
):

if self.aborting:
return self

Expand Down Expand Up @@ -5873,7 +5866,6 @@ def accept_follow_requests(self, amount: int = 100, sleep_delay: int = 1):

accepted = 0
while accepted < amount:

feed_link = "https://www.instagram.com/accounts/activity/?followRequests=1"
web_address_navigator(self.browser, feed_link)

Expand Down
19 changes: 14 additions & 5 deletions instapy/like_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,12 +616,12 @@ def check_link(
owner_comments = owner_comments + "\n" + comment["node"]["text"]

else:
media = post_page ['items'] [0]
is_video = media ["is_unified_video"]
user_name = media ["user"] ["username"]
media = post_page["items"][0]
is_video = media["is_unified_video"]
user_name = media["user"]["username"]
image_text = None
if media["caption"]:
image_text = media ["caption"] ["text"]
image_text = media["caption"]["text"]
# RC: Disabling owner's comments temporarily
owner_comments = ""

Expand Down Expand Up @@ -749,7 +749,16 @@ def like_image(browser, username, blacklist, logger, logfolder, total_liked_img)
browser.execute_script("arguments[0].scrollIntoView(true);", element)

# find first for like element
like_elem = browser.find_elements(By.XPATH, like_xpath)
like_elem = browser.execute_script("""
var svgLikeButton = document.querySelector("svg[aria-label='Like']");
if (svgLikeButton) {
var clickableParent = svgLikeButton.closest('button, a'); // Find the nearest parent that is a button or link
if (clickableParent) {
clickableParent.scrollIntoView({behavior: 'smooth', block: 'center'}); // Scroll the button into view
clickableParent.click(); // Click the clickable parent element
}
}
""")

if len(like_elem) == 1:
# sleep real quick right before clicking the element
Expand Down
7 changes: 3 additions & 4 deletions instapy/login_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ def login_user(

# Hotfix - this check crashes more often than not -- plus in not necessary,
# I can verify my own connection
if want_check_browser:
if not check_browser(browser, logfolder, logger, proxy_address):
return False
# if want_check_browser:
# if not check_browser(browser, logfolder, logger, proxy_address):
# return False

ig_homepage = "https://www.instagram.com"
web_address_navigator(browser, ig_homepage)
Expand Down Expand Up @@ -629,7 +629,6 @@ def two_factor_authentication(browser, logger, security_codes):
sleep(random.randint(3, 5))

if "two_factor" in browser.current_url:

logger.info("- Two Factor Authentication is enabled...")

# Chose one code from the security_codes list
Expand Down
2 changes: 0 additions & 2 deletions instapy/text_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def text_analysis(text, text_type, logger):
and (not MEANINGCLOUD_CONFIG or MEANINGCLOUD_CONFIG["enabled"] is not True)
)
):

# No analysis will be held
print("")
logger.info('{} text: "{}"'.format(text_type_c, text.encode("utf-8")))
Expand Down Expand Up @@ -531,7 +530,6 @@ def lift_meaningcloud_request(request):
"214",
"215",
]:

if status_code == "103":
error_msg = (
"request too large: exceeded the limit on the number of words"
Expand Down
7 changes: 0 additions & 7 deletions instapy/unfollow_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ def set_automated_followed_pool(
reader = csv.reader(followedPoolFile)

for row in reader:

# init
followedback = None
user_id = "undefined" # 'undefined' rather than None is *intentional
Expand Down Expand Up @@ -238,7 +237,6 @@ def unfollow(
or nonFollowers is True
or allFollowing is True
):

if nonFollowers is True:
InstapyFollowed = False

Expand Down Expand Up @@ -419,15 +417,13 @@ def unfollow(

# delay unfollowing of follow-backers
if delay_followbackers and unfollow_track != "nonfollowers":

followedback_status = automatedFollowedPool["all"][person][
"followedback"
]
# if once before we set that flag to true
# now it is time to unfollow since
# time filter pass, user is now eligble to unfollow
if followedback_status is not True:

user_link = "https://www.instagram.com/{}/".format(person)
web_address_navigator(browser, user_link)
valid_page = is_page_available(browser, logger)
Expand Down Expand Up @@ -652,7 +648,6 @@ def get_users_through_dialog_with_graphql(
logfolder,
edge_followed_by,
):

# TODO: simulation implementation

real_amount = amount
Expand Down Expand Up @@ -1509,7 +1504,6 @@ def verify_action(
post_action_text_correct = None

if action in ["follow", "unfollow"]:

# assuming button_change testing is relevant to those actions only
button_change = False

Expand All @@ -1522,7 +1516,6 @@ def verify_action(
post_action_text_fail = ["Following", "Requested", "Message"]

while True:

# count retries at beginning
retry_count += 1

Expand Down
10 changes: 3 additions & 7 deletions instapy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import emoji
import regex
from bs4 import BeautifulSoup
from emoji.unicode_codes import EMOJI_DATA
from emoji.unicode_codes import _EMOJI_UNICODE

# import exceptions
from selenium.common.exceptions import (
Expand Down Expand Up @@ -230,7 +230,6 @@ def validate_username(
or delimit_by_numbers
and (max_followers or max_following or min_followers or min_following)
):

relationship_ratio = None
reverse_relationship = False

Expand Down Expand Up @@ -863,11 +862,9 @@ def get_active_users(browser, username, posts, boundary, logger):
and likers_count
and likers_count - 1 > len(user_list)
):

if (
boundary is not None and likers_count - 1 > boundary
) or boundary is None:

if try_again <= 1: # can increase the amount of tries
logger.info(
"Failed to get the desired amount of "
Expand Down Expand Up @@ -1620,7 +1617,6 @@ def check_authorization(browser, username, method, logger, notify=True):

# different methods can be added in future
if method == "activity counts":

# navigate to owner's profile page only if it is on an unusual page
current_url = get_current_url(browser)
if (
Expand Down Expand Up @@ -2634,7 +2630,7 @@ def get_additional_data(browser):
# break
original_url = browser.current_url
if not additional_data:
browser.get('view-source:'+ browser.current_url +'?__a=1&__d=dis')
browser.get("view-source:" + browser.current_url + "?__a=1&__d=dis")
text = browser.find_element(By.TAG_NAME, "pre").text
additional_data = json.loads(re.search("{.*}", text).group())
browser.get(original_url)
Expand All @@ -2651,7 +2647,7 @@ def get_shared_data(browser):
:return shared_data: Json data from window._sharedData extracted from page source
"""
shared_data = None
browser.get('view-source:'+ browser.current_url +'?__a=1&__d=dis')
browser.get("view-source:" + browser.current_url + "?__a=1&__d=dis")
text = browser.find_element(By.TAG_NAME, "pre").text
shared_data = json.loads(re.search("{.*}", text).group())

Expand Down
6 changes: 3 additions & 3 deletions instapy/xpath_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
}

xpath["like_image"] = {
"like": "//*[contains(@class, '_aamw')]/button/div/*[*[local-name()='svg']/@aria-label='Like']/*",
"like": "//svg[descendant::title[contains(text(), 'Like')]]",
"unlike": "//*[contains(@class, '_aamw')]/button/div/*[*[local-name()='svg']/@aria-label='Unlike']/*",
"play": "//*/span[contains(@aria-label, 'Play')]",
}
Expand All @@ -172,9 +172,9 @@
xpath["login_user"] = {
"input_password": "//input[@name='password']",
"input_username_XP": "//input[@name='username']",
"login_elem": "//button[text()='Log In']",
"login_elem": "//button[text()='Log in']",
"login_elem_no_such_exception": "//a[text()='Log in']",
"login_elem_no_such_exception_2": "//div[text()='Log In']",
"login_elem_no_such_exception_2": "//div[text()='Log in']",
"nav": "//nav",
"website_status": "//span[@id='status']",
"response_time": "//span[@id='response']",
Expand Down