Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Twitter: Fixes for tweepy 4.0.0 #725

Merged
merged 2 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 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 @@ -150,6 +150,10 @@ def test_geo_util(self):
class Response:
def __init__(self, code):
self.status_code = code
self.reason = "dummy reason"

def json(self):
return {}


class TestTwitterAPIErrorRaising(unittest.TestCase):
Expand All @@ -158,16 +162,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.TooManyRequests(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 TooManyRequests

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, TooManyRequests):
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, TooManyRequests


# 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 = TooManyRequests(Response(492))
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