Skip to content

Instantly share code, notes, and snippets.

@uluQulu
Created June 10, 2018 15:33
Show Gist options
  • Save uluQulu/e1065fe51a92c81454481d42ec8017ce to your computer and use it in GitHub Desktop.
Save uluQulu/e1065fe51a92c81454481d42ec8017ce to your computer and use it in GitHub Desktop.
A new feature to do all kind of interactions on given URLs
def interact_by_URL(self,
urls=[],
media=None,
interact=False):
""" Interact on images at given URLs """
if self.aborting:
return self
self.logger.info("Starting to interact by given URLs\n")
if not isinstance(urls, list):
urls = [urls]
liked_img = 0
already_liked = 0
inap_img = 0
commented = 0
followed = 0
not_valid_users = 0
for index, url in enumerate(urls):
if "https://www.instagram.com/p/" not in url:
url = "https://www.instagram.com/p/"+url
self.logger.info('URL [{}/{}]'.format(index + 1, len(urls)))
self.logger.info('--> {}'.format(url.encode('utf-8')))
try:
inappropriate, user_name, is_video, reason, scope = (
check_link(self.browser,
url,
self.dont_like,
self.ignore_if_contains,
self.logger)
)
if not inappropriate and self.delimit_liking:
self.liking_approved = verify_liking(self.browser, self.max_likes, self.min_likes, self.logger)
if not inappropriate and self.liking_approved:
#validate user
validation, details = validate_username(self.browser,
user_name,
self.username,
self.ignore_users,
self.blacklist,
self.potency_ratio,
self.delimit_by_numbers,
self.max_followers,
self.max_following,
self.min_followers,
self.min_following,
self.logger)
if validation != True:
self.logger.info(details)
not_valid_users += 1
continue
else:
web_adress_navigator(self.browser, url)
#try to like
liked = like_image(self.browser,
user_name,
self.blacklist,
self.logger,
self.logfolder)
if liked:
liked_img += 1
checked_img = True
temp_comments = []
commenting = (random.randint(0, 100) <=
self.comment_percentage)
following = (random.randint(0, 100) <=
self.follow_percentage)
if self.use_clarifai and (following or commenting):
try:
checked_img, temp_comments = (
check_image(self.browser,
self.clarifai_api_key,
self.clarifai_img_tags,
self.logger,
self.clarifai_full_match)
)
except Exception as err:
self.logger.error(
'Image check error: {}'.format(err))
if (self.do_comment and
user_name not in self.dont_include and
checked_img and
commenting):
if self.delimit_commenting:
self.commenting_approved, disapproval_reason = verify_commenting(self.browser, self.max_comments, self.min_comments, self.logger)
if self.commenting_approved:
if temp_comments:
# Use clarifai related comments only!
comments = temp_comments
elif is_video:
comments = (self.comments +
self.video_comments)
else:
comments = (self.comments +
self.photo_comments)
commented += comment_image(self.browser,
user_name,
comments,
self.blacklist,
self.logger,
self.logfolder)
else:
self.logger.info(disapproval_reason)
else:
self.logger.info('--> Not commented')
sleep(1)
if (self.do_follow and
user_name not in self.dont_include and
checked_img and
following and
self.follow_restrict.get(user_name, 0) <
self.follow_times):
followed += follow_user(self.browser,
self.follow_restrict,
self.username,
user_name,
self.blacklist,
self.logger,
self.logfolder)
else:
self.logger.info('--> Not following')
sleep(1)
# Check if interaction is expected
if interact == True:
do_interact = random.randint(0, 100) <= self.user_interact_percentage
# Do interactions if any
if do_interact and self.user_interact_amount>0:
self.logger.info(
'--> Starting to interact {}..'
.format(user_name))
self.interact_by_users(user_name,
self.user_interact_amount,
self.user_interact_random,
self.user_interact_media)
else:
already_liked += 1
else:
self.logger.info(
'--> Image not liked: {}'.format(reason.encode('utf-8')))
inap_img += 1
except NoSuchElementException as err:
self.logger.error('Invalid Page: {}'.format(err))
self.logger.info('Liked: {}'.format(liked_img))
self.logger.info('Already Liked: {}'.format(already_liked))
self.logger.info('Commented: {}'.format(commented))
self.logger.info('Followed: {}'.format(followed))
self.logger.info('Inappropriate: {}'.format(inap_img))
self.logger.info('Not valid users: {}\n'.format(not_valid_users))
self.liked_img += liked_img
self.already_liked += already_liked
self.commented += commented
self.followed += followed
self.inap_img += inap_img
self.not_valid_users += not_valid_users
return self
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment