Skip to content

Commit a3c0882

Browse files
committed
fix some codacy issues
1 parent 6af32d6 commit a3c0882

File tree

9 files changed

+61
-69
lines changed

9 files changed

+61
-69
lines changed

instapy/browser.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def set_selenium_local_session(proxy_address,
7676

7777
# converts to custom browser
7878
# browser = convert_selenium_browser(browser)
79-
79+
8080
# authenticate with popup alert window
8181
if (proxy_username and proxy_password):
8282
proxy_authentication(browser,
@@ -255,6 +255,7 @@ def retry(max_retry_count = 3, start_page = None):
255255
def real_decorator(org_func):
256256
def wrapper(*args, **kwargs):
257257
browser = None
258+
_start_page = start_page
258259

259260
# try to find instance of a browser in the arguments
260261
# all webdriver classes (chrome, firefox, ...) inherit from Remote class
@@ -281,7 +282,7 @@ def wrapper(*args, **kwargs):
281282

282283
# get current page if none is given
283284
if not start_page:
284-
start_page = browser.current_url
285+
_start_page = browser.current_url
285286

286287
rv = None
287288
retry_count = 0
@@ -300,9 +301,9 @@ def wrapper(*args, **kwargs):
300301
rv = None
301302

302303
# refresh page
303-
browser.get(start_page)
304+
browser.get(_start_page)
304305

305-
return rv;
306+
return rv
306307
return wrapper
307308
return real_decorator
308309

@@ -347,7 +348,7 @@ def wait_for_valid_authorization(self, username, logger):
347348
break
348349

349350
# return to original page
350-
web_address_navigator(self, current_url)
351+
web_address_navigator(self, current_url)
351352

352353

353354

instapy/commenters_util.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ def extract_information(browser, username, daysold, max_pic):
173173
click_element(browser, one_pic_elem)
174174
except Exception:
175175
print("Error: cant click on the photo..")
176-
pass
177176

178177
sleep(1.5)
179178

@@ -408,9 +407,4 @@ def get_photo_urls_from_profile(browser, username, links_to_return_amount=1,
408407
min(links_to_return_amount, len(links)), " links: ",
409408
links[:links_to_return_amount])
410409
sleep(1)
411-
return links[:links_to_return_amount]
412-
# except:
413-
# Code below is unreachable - perhaps get rid of?
414-
print("Error: Couldnt get pictures links.")
415-
return []
416-
410+
return links[:links_to_return_amount]

instapy/like_util.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ def get_links_for_username(browser,
413413
return False
414414

415415
# if private user, we can get links only if we following
416-
following_status, follow_button = get_following_status(
416+
following_status, _ = get_following_status(
417417
browser, "profile", username, person, None, logger, logfolder)
418418

419419
#if following_status is None:
@@ -509,7 +509,7 @@ def check_link(browser, post_link, dont_like, mandatory_words,
509509
# navigate to it again
510510
web_address_navigator(browser, post_link)
511511

512-
"""Check if the Post is Valid/Exists"""
512+
# Check if the Post is Valid/Exists
513513
try:
514514
post_page = browser.execute_script(
515515
"return window._sharedData.entry_data.PostPage")
@@ -530,8 +530,7 @@ def check_link(browser, post_link, dont_like, mandatory_words,
530530
'Unavailable Page: {}'.format(post_link.encode('utf-8')))
531531
return True, None, None, 'Unavailable Page', "Failure"
532532

533-
"""Gets the description of the post's link and checks for the dont_like
534-
tags"""
533+
# Gets the description of the post's link and checks for the dont_like tags
535534
graphql = 'graphql' in post_page[0]
536535
if graphql:
537536
media = post_page[0]['graphql']['shortcode_media']
@@ -579,14 +578,14 @@ def check_link(browser, post_link, dont_like, mandatory_words,
579578
if owner_comments == '':
580579
owner_comments = None
581580

582-
"""Append owner comments to description as it might contain further tags"""
581+
# Append owner comments to description as it might contain further tags
583582
if image_text is None:
584583
image_text = owner_comments
585584

586585
elif owner_comments:
587586
image_text = image_text + '\n' + owner_comments
588587

589-
"""If the image still has no description gets the first comment"""
588+
# If the image still has no description gets the first comment
590589
if image_text is None:
591590
if graphql:
592591
media_edge_string = get_media_edge_comment_string(media)
@@ -604,16 +603,14 @@ def check_link(browser, post_link, dont_like, mandatory_words,
604603
logger.info('Link: {}'.format(post_link.encode('utf-8')))
605604
logger.info('Description: {}'.format(image_text.encode('utf-8')))
606605

607-
"""Check if mandatory character set, before adding the location to the
608-
text"""
606+
# Check if mandatory character set, before adding the location to the text
609607
if mandatory_language:
610608
if not check_character_set(image_text):
611609
return True, user_name, is_video, 'Mandatory language not ' \
612610
'fulfilled', "Not mandatory " \
613611
"language"
614612

615-
"""Append location to image_text so we can search through both in one
616-
go."""
613+
# Append location to image_text so we can search through both in one go
617614
if location_name:
618615
logger.info('Location: {}'.format(location_name.encode('utf-8')))
619616
image_text = image_text + '\n' + location_name

instapy/login_util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def login_user(browser,
234234
.perform())
235235

236236
# update server calls for both 'click' and 'send_keys' actions
237-
for i in range(2):
237+
for _ in range(2):
238238
update_activity()
239239

240240
sleep(1)

instapy/relationship_tools.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ def get_following(browser,
302302
web_address_navigator(browser, user_link)
303303

304304
# Get following count
305-
followers_count, following_count = get_relationship_counts(browser,
305+
_, following_count = get_relationship_counts(browser,
306306
username,
307307
logger)
308308

@@ -362,7 +362,7 @@ def get_following(browser,
362362
)
363363
web_address_navigator(browser, url)
364364

365-
""" Get stored graphql queries data to be used """
365+
# Get stored graphql queries data to be used
366366
try:
367367
filename = '{}graphql_queries.json'.format(logfolder)
368368
query_date = datetime.today().strftime('%d-%m-%Y')
@@ -372,7 +372,8 @@ def get_following(browser,
372372
json.dump({username: {query_date: {"sc_rolled": 0}}},
373373
graphql_queries_file)
374374
graphql_queries_file.close()
375-
""" Loads the existing graphql queries data """
375+
376+
# Loads the existing graphql queries data
376377
with open(filename) as graphql_queries_file:
377378
graphql_queries = json.load(graphql_queries_file)
378379
stored_usernames = list(
@@ -553,7 +554,7 @@ def get_unfollowers(browser,
553554
"~leaving out of an invalid value")
554555
return [], []
555556

556-
elif username is None or type(username) != str:
557+
elif username is None or not isinstance(username, str):
557558
logger.info(
558559
"Please enter a username to pick Unfollowers ~leaving out of an "
559560
"invalid value")
@@ -630,7 +631,7 @@ def get_nonfollowers(browser,
630631
logfolder):
631632
""" Finds Nonfollowers of a given user """
632633

633-
if username is None or type(username) != str:
634+
if username is None or not isinstance(username, str):
634635
logger.info(
635636
"Please enter a username to pick Nonfollowers ~leaving out of "
636637
"an invalid value")

instapy/text_analytics.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@ def text_analysis(text, text_type, logger):
5151
(YANDEX_CONFIG["match_language"] is not True and
5252
(not MEANINGCLOUD_CONFIG or MEANINGCLOUD_CONFIG[
5353
"enabled"] is not True))):
54-
""" No analysis will be held """
54+
55+
# No analysis will be held
5556
print('')
5657
logger.info(
5758
"{} text: \"{}\"".format(text_type_c, text.encode("utf-8")))
5859
return None
5960

6061
if YANDEX_CONFIG["match_language"] is True:
61-
""" Language detection & match will take place """
62+
# Language detection & match will take place
6263
if has_any_letters(emojiless_text):
6364
language_of_text = detect_language(emojiless_text)
6465
else:
@@ -84,7 +85,7 @@ def text_analysis(text, text_type, logger):
8485
return False
8586

8687
if MEANINGCLOUD_CONFIG["enabled"] is True:
87-
""" Text language normalization for accuracy & efficiency """
88+
# Text language normalization for accuracy & efficiency
8889
if not language_of_text:
8990
if has_any_letters(emojiless_text):
9091
language_of_text = detect_language(emojiless_text)
@@ -113,7 +114,7 @@ def text_analysis(text, text_type, logger):
113114
text = translate_text(translation_direction, text)
114115
language_of_text = "en"
115116

116-
""" Sentiment Analysis """
117+
# Sentiment Analysis
117118
sentiment = sentiment_analysis(text, language_of_text, logger)
118119

119120
if sentiment is None:
@@ -250,7 +251,7 @@ def sentiment_analysis(text, language_of_text, logger):
250251

251252
except (ValueError, ConnectionError) as exc:
252253
print('')
253-
logger.exception("{}\t~{}\n{}\n"
254+
logger.exception("{}\t~{}\n"
254255
.format(MEANINGCLOUD_FAILURE_MSG,
255256
str(exc).encode("utf-8")))
256257
return None

instapy/unfollow_util.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -210,22 +210,22 @@ def unfollow(browser,
210210
""" Unfollows the given amount of users"""
211211

212212
if (customList is not None and
213-
type(customList) in [tuple, list] and
213+
isinstance(customList, (tuple, list)) and
214214
len(customList) == 3 and
215215
customList[0] is True and
216-
type(customList[1]) in [list, tuple, set] and
216+
isinstance(customList[1], (list, tuple, set)) and
217217
len(customList[1]) > 0 and
218218
customList[2] in ["all", "nonfollowers"]):
219219
customList_data = customList[1]
220-
if type(customList_data) != list:
220+
if not isinstance(customList_data, list):
221221
customList_data = list(customList_data)
222222
unfollow_track = customList[2]
223223
customList = True
224224
else:
225225
customList = False
226226

227227
if (InstapyFollowed is not None and
228-
type(InstapyFollowed) in [tuple, list] and
228+
isinstance(InstapyFollowed, (tuple, list)) and
229229
len(InstapyFollowed) == 2 and
230230
InstapyFollowed[0] is True and
231231
InstapyFollowed[1] in ["all", "nonfollowers"]):
@@ -243,7 +243,7 @@ def unfollow(browser,
243243
web_address_navigator(browser, user_link)
244244

245245
# check how many poeple we are following
246-
allfollowers, allfollowing = get_relationship_counts(browser, username,
246+
_, allfollowing = get_relationship_counts(browser, username,
247247
logger)
248248

249249
if allfollowing is None:
@@ -276,7 +276,8 @@ def unfollow(browser,
276276

277277
elif nonFollowers is True:
278278
logger.info("Unfollowing the users who do not follow back\n")
279-
""" Unfollow only the users who do not follow you back """
279+
280+
# Unfollow only the users who do not follow you back
280281
unfollow_list = get_nonfollowers(browser,
281282
username,
282283
relationship_data,
@@ -416,7 +417,7 @@ def unfollow(browser,
416417
# now it is time to unfollow since
417418
# time filter pass, user is now eligble to unfollow
418419
if followedback_status is not True:
419-
420+
420421
user_link = "https://www.instagram.com/{}/".format(person)
421422
web_address_navigator(browser, user_link)
422423
valid_page = is_page_available(browser, logger)
@@ -1035,7 +1036,7 @@ def get_given_user_followers(browser,
10351036
return [], []
10361037

10371038
# check how many people are following this user.
1038-
allfollowers, allfollowing = get_relationship_counts(browser, user_name,
1039+
allfollowers, _ = get_relationship_counts(browser, user_name,
10391040
logger)
10401041

10411042
# skip early for no followers
@@ -1302,7 +1303,8 @@ def unfollow_user(browser, track, username, person, person_id, button,
13021303
return False, "jumped"
13031304

13041305
if track in ["profile", "post"]:
1305-
""" Method of unfollowing from a user's profile page or post page """
1306+
# Method of unfollowing from a user's profile page or post page
1307+
13061308
if track == "profile":
13071309
user_link = "https://www.instagram.com/{}/".format(person)
13081310
web_address_navigator(browser, user_link)
@@ -1363,7 +1365,8 @@ def unfollow_user(browser, track, username, person, person_id, button,
13631365
person))
13641366
return False, "unexpected failure"
13651367
elif track == "dialog":
1366-
""" Method of unfollowing from a dialog box """
1368+
# Method of unfollowing from a dialog box
1369+
13671370
click_element(browser, button)
13681371
sleep(4) # TODO: use explicit wait here
13691372
confirm_unfollow(browser)
@@ -1404,8 +1407,6 @@ def confirm_unfollow(browser):
14041407

14051408
elif isinstance(exc, NoSuchElementException):
14061409
sleep(1)
1407-
pass
1408-
14091410

14101411
def post_unfollow_cleanup(state, username, person, relationship_data,
14111412
person_id, logger, logfolder):
@@ -1626,7 +1627,7 @@ def set_followback_in_pool(username, person, person_id, logtime, logger, logfold
16261627
'{0}{1}_followedPool.csv'.format(logfolder, username), person, logger)
16271628

16281629
# return the username with new timestamp
1629-
log_followed_pool(username, person, logger, logfolder, logtime, person_id, "true")
1630+
log_followed_pool(username, person, logger, logfolder, logtime, person_id)
16301631

16311632

16321633
def refresh_follow_time_in_pool(username, person, person_id, extra_secs, logger, logfolder):
@@ -1638,5 +1639,5 @@ def refresh_follow_time_in_pool(username, person, person_id, extra_secs, logger,
16381639
'{0}{1}_followedPool.csv'.format(logfolder, username), person, logger)
16391640

16401641
# return the username with new timestamp
1641-
log_followed_pool(username, person, logger, logfolder, logtime, person_id, "unknown")
1642+
log_followed_pool(username, person, logger, logfolder, logtime, person_id)
16421643

0 commit comments

Comments
 (0)