From a20b6c3e0b3d6d03469cc7691225f254dc0f5d19 Mon Sep 17 00:00:00 2001 From: Ajda Pretnar Date: Fri, 1 Oct 2021 14:14:28 +0200 Subject: [PATCH] Twitter: Fixes for tweepy 4.0.0 --- orangecontrib/text/tests/test_twitter.py | 12 ++++++------ orangecontrib/text/twitter.py | 10 +++++++--- orangecontrib/text/widgets/owtwitter.py | 4 ++-- orangecontrib/text/widgets/tests/test_owtwitter.py | 6 +++--- requirements.txt | 2 +- 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/orangecontrib/text/tests/test_twitter.py b/orangecontrib/text/tests/test_twitter.py index 5c2f257da..3caa77103 100644 --- a/orangecontrib/text/tests/test_twitter.py +++ b/orangecontrib/text/tests/test_twitter.py @@ -9,7 +9,7 @@ from orangecontrib.text import twitter from orangecontrib.text.corpus import Corpus -from tweepy import TweepError +from tweepy import TweepyException def get_credentials(): @@ -54,7 +54,7 @@ def test_pickle(self): class MyCursor: def __init__(self, *args, **kwargs): time.sleep(0.05) - self.statuses = tweepy.Status.parse_list( + self.statuses = tweepy.models.Status.parse_list( None, json.load( open(os.path.join(os.path.dirname(__file__), "tweets.json")) @@ -158,16 +158,16 @@ def setUp(self): def test_error_reporting(self): with unittest.mock.patch("tweepy.Cursor.items") as mock: - mock.side_effect = tweepy.TweepError("", Response(500)) + mock.side_effect = tweepy.TweepyException("", Response(500)) api = twitter.TwitterAPI(self.credentials) - with self.assertRaises(TweepError): + with self.assertRaises(TweepyException): api.search_authors("hello", max_tweets=5) def test_rate_limit_reporting(self): with unittest.mock.patch("tweepy.Cursor.items") as mock: - mock.side_effect = tweepy.TweepError("", Response(429)) + mock.side_effect = tweepy.TweepyException("", Response(429)) api = twitter.TwitterAPI(self.credentials) - with self.assertRaises(TweepError): + with self.assertRaises(TweepyException): api.search_authors("hello", max_tweets=5) diff --git a/orangecontrib/text/twitter.py b/orangecontrib/text/twitter.py index fc6e3ad5d..c8859ea64 100644 --- a/orangecontrib/text/twitter.py +++ b/orangecontrib/text/twitter.py @@ -39,7 +39,7 @@ def check(self): try: self.auth.get_authorization_url() self._valid = True - except tweepy.TweepError: + except tweepy.TweepyException: self._valid = False return self._valid @@ -83,7 +83,10 @@ class TwitterAPI: StringVariable("Content"), lambda doc: doc.full_text if not doc.retweeted else doc.text, ), - (tv, lambda doc: TwitterAPI.tv.parse(doc.created_at.isoformat())), + # temporary fix until Orange>3.30.1 then change back to + # (tv, lambda doc: TwitterAPI.tv.parse(doc.created_at.isoformat())), + (tv, lambda doc: TwitterAPI.tv.parse( + TwitterAPI.tv._tzre_sub(doc.created_at.isoformat()))), (DiscreteVariable("Language"), lambda doc: doc.lang), ( DiscreteVariable("Location"), @@ -200,8 +203,9 @@ def build_query(): query = build_query() cursor = tweepy.Cursor( - self.api.search, q=query, lang=lang, tweet_mode="extended" + self.api.search_tweets, q=query, lang=lang, tweet_mode="extended" ) + corpus, count = self.fetch( cursor, max_tweets, search_author=False, callback=callback ) diff --git a/orangecontrib/text/widgets/owtwitter.py b/orangecontrib/text/widgets/owtwitter.py index 3373b6f9c..c851ccb07 100644 --- a/orangecontrib/text/widgets/owtwitter.py +++ b/orangecontrib/text/widgets/owtwitter.py @@ -9,7 +9,7 @@ from Orange.widgets.settings import Setting from Orange.widgets.widget import OWWidget, Msg, Output from Orange.widgets.utils.concurrent import TaskState, ConcurrentWidgetMixin -from tweepy import TweepError +from tweepy import TweepyException from orangecontrib.text import twitter from orangecontrib.text.corpus import Corpus @@ -336,7 +336,7 @@ def on_done(self, result): def on_exception(self, ex): self.search_button.setText("Search") - if isinstance(ex, TweepError) and ex.response.status_code == 429: + if isinstance(ex, TweepyException) and ex.response.status_code == 429: self.Error.rate_limit() else: self.Error.api_error(str(ex)) diff --git a/orangecontrib/text/widgets/tests/test_owtwitter.py b/orangecontrib/text/widgets/tests/test_owtwitter.py index 497cd705c..cb17c660d 100644 --- a/orangecontrib/text/widgets/tests/test_owtwitter.py +++ b/orangecontrib/text/widgets/tests/test_owtwitter.py @@ -7,7 +7,7 @@ from orangecontrib.text import twitter, Corpus from orangecontrib.text.tests.test_twitter import Response from orangecontrib.text.widgets.owtwitter import OWTwitter -from tweepy import TweepError +from tweepy import TweepyException # it is not possible to test real API because API key cannot be shared for @@ -61,7 +61,7 @@ def test_author(self): @patch("tweepy.Cursor.items") def test_rate_limit(self, mock_items): - mock_items.side_effect = TweepError("Rate limit error", Response(429)) + mock_items.side_effect = TweepyException("Rate limit error", Response(429)) self.widget.word_list = ["orange"] self.widget.search_button.click() self.wait_until_finished() @@ -70,7 +70,7 @@ def test_rate_limit(self, mock_items): @patch("tweepy.Cursor.items") def test_error(self, mock_items): - mock_items.side_effect = TweepError("Other errors", Response(400)) + mock_items.side_effect = TweepyException("Other errors", Response(400)) self.widget.word_list = ["orange"] self.widget.search_button.click() self.wait_until_finished() diff --git a/requirements.txt b/requirements.txt index 188658e1c..07eecd955 100644 --- a/requirements.txt +++ b/requirements.txt @@ -21,7 +21,7 @@ scipy serverfiles simhash >=1.11 six -tweepy +tweepy >=4.0.0 ufal.udpipe >=1.2.0.3 wikipedia yake