Repository: InstaPy/InstaPy

Issue:
Hi this my error
Traceback (most recent call last):
  File "start.py", line 47, in <module>
    session.like_by_tags(['musculation','teamshape','getfit','fit','fitnessmodel','fitspo','crossfit','muscu','shooting','fitfrenchies'], amount=10)
  File "/Users/Retina/Desktop/InstaPy/instapy/instapy.py", line 850, in like_by_tags
    self.logger)
  File "/Users/Retina/Desktop/InstaPy/instapy/like_util.py", line 538, in check_link
    'Inappropriate! ~ contains \'{}\' in \'{}\''.format(iffy, quashed))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 13: ordinal not in range(128)




Discussion:
Yeah that should be encoded.
Traceback (most recent call last):
  File "start.py", line 43, in <module>
    session.like_by_tags(['musculation','teamshape','getfit','strong','ifbb','fitspo','crossfit','instagood','lille','training'], amount=10)
  File "/Users/Retina/Desktop/InstaPy/instapy/instapy.py", line 850, in like_by_tags
    self.logger)
  File "/Users/Retina/Desktop/InstaPy/instapy/like_util.py", line 421, in check_link
    "return window._sharedData.entry_data.PostPage")
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 465, in execute_script
    'args': converted_args})['value']
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Cannot read property 'entry_data' of undefined
  (Session info: chrome=65.0.3325.162)
  (Driver info: chromedriver=2.36.540469 (1881fd7f8641508feb5166b7cae561d87723cfa8),platform=Mac OS X 10.13.3 x86_64)

MacBook-Pro-de-Mac:instapy Retina$ 


Resolving patch:
diff --git a/instapy/like_util.py b/instapy/like_util.py
--- a/instapy/like_util.py
+++ b/instapy/like_util.py
@@ -401,16 +401,26 @@ def get_links_for_username(browser,
     return links[:amount]
 
 
-def check_link(browser,
-               link,
-               dont_like,
-               ignore_if_contains,
-               ignore_users,
-               username,
-               like_by_followers_upper_limit,
-               like_by_followers_lower_limit,
-               logger):
-
+def check_link(browser, link, dont_like, ignore_if_contains, ignore_users, username,
+               like_by_followers_upper_limit, like_by_followers_lower_limit, logger):
+    """
+    Check the given link if it is appropriate
+
+    :param browser: The selenium webdriver instance
+    :param link:
+    :param dont_like: hashtags of inappropriate phrases
+    :param ignore_if_contains:
+    :param ignore_users:
+    :param username:
+    :param like_by_followers_upper_limit:
+    :param like_by_followers_lower_limit:
+    :param logger: the logger instance
+    :return: tuple of
+        boolean: True if inappropriate,
+        string: the username,
+        boolean: True if it is video media,
+        string: the message if inappropriate else 'None'
+    """
     browser.get(link)
     # update server calls
     update_activity()
@@ -534,8 +544,9 @@ def check_link(browser,
                      (re.split(r'\W+', dont_likes_regex))[1] if dont_likes_regex.endswith('+([^\\d\\w]|$)') else   # '[word'
                       (re.split(r'\W+', dont_likes_regex))[3] if dont_likes_regex.startswith('#[\\d\\w]+') else     # ']word'
                        (re.split(r'\W+', dont_likes_regex))[1])                                                    # '#word'
-            inapp_unit = ('Inappropriate! ~ contains \'{}\''.format(quashed) if quashed == iffy else
-                              'Inappropriate! ~ contains \'{}\' in \'{}\''.format(iffy, quashed))
+            inapp_unit = 'Inappropriate! ~ contains "{}"'.format(
+                quashed.encode('utf-8') if iffy == quashed else
+                '" in "'.join([iffy, quashed]).encode('utf-8'))
             return True, user_name, is_video, inapp_unit
 
     return False, user_name, is_video, 'None'


Test patch:
diff --git a/tests/test_like_util.py b/tests/test_like_util.py
new file mode 100644
--- /dev/null
+++ b/tests/test_like_util.py
@@ -0,0 +1,18 @@
+from unittest.mock import MagicMock, patch
+
+from instapy.like_util import check_link
+
+
+class TestCheckLink:
+
+    @patch('instapy.like_util.sleep')
+    def test_inappropriate_text_on_dont_like(self, sleep):
+        """when inappropriate the message is returned"""
+        browser = MagicMock()
+        browser.execute_script.side_effect = [
+            [{'media': {'is_video': False, 'owner': {'username': 'john'}, 'caption': '#f\xf6o'}}],
+            '']
+        inappropriate, user_name, is_video, reason = check_link(
+            browser, MagicMock(), ['#f'], MagicMock(), MagicMock(), MagicMock(),
+            None, None, MagicMock())
+        assert inappropriate is True