Skip to content

Commit

Permalink
Twitter: Fixes for tweepy 4.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ajdapretnar committed Oct 6, 2021
1 parent 3b5e676 commit a20b6c3
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
12 changes: 6 additions & 6 deletions orangecontrib/text/tests/test_twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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)


Expand Down
10 changes: 7 additions & 3 deletions orangecontrib/text/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions orangecontrib/text/widgets/owtwitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions orangecontrib/text/widgets/tests/test_owtwitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ scipy
serverfiles
simhash >=1.11
six
tweepy
tweepy >=4.0.0
ufal.udpipe >=1.2.0.3
wikipedia
yake

0 comments on commit a20b6c3

Please sign in to comment.