Search code examples
pythonpython-twitter

python-twitter stream raises JSONDecodeError


When calling for the next tweet of the stream with next(), the following error occurs. When I try calling other functions with the same twitter API object, such as GetFriends(), it works fine. Any help is much appreciated!

Here is the error:

Traceback (most recent call last):
 File "/usr/local/lib/python3.8/site-packages/twitter/api.py", line 4897, in _ParseAndCheckTwitter
    data = json.loads(json_data)
  File "/usr/local/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

And here is the code:

import twitter
class Topic:
    def __init__(self, topic):
        self.api = self.get_twitter_api()
        self.stream = self.api.GetStreamFilter(track=[topic])
        self.count_tweets()

    def get_twitter_api(self):
        with open('/twitter_credentials.json') as f:
            return twitter.Api(**eval(f.read()))

    def count_tweets(self):
        while next(self.stream):
            print("tweet")

Solution

  • The oldest trick in the book worked: restarting the PC.