В январе 2018 года я использовал скрипт на Python 2.7, который в основном читал твиты, как они появились на моей временной шкале. Если были ссылки или изображения, они открывались в браузере по умолчанию.
Теперь я устанавливаю все необходимые пакеты и Python 2.7, и это ошибка, которую он мне дает:
pygame 1.9.5
Hello from the pygame community. https://www.pygame.org/contribute.html
Waiting for a Tweet...
Traceback (most recent call last):
File "twitter.py", line 65, in <module>
api.userstream(_with='@username')
File "C:\Python27\lib\site-packages\tweepy\streaming.py", line 397, in userstream
self._start(async)
File "C:\Python27\lib\site-packages\tweepy\streaming.py", line 364, in _start
self._run()
File "C:\Python27\lib\site-packages\tweepy\streaming.py", line 297, in _run
six.reraise(*exc_info)
File "C:\Python27\lib\site-packages\tweepy\streaming.py", line 252, in _run
if self.listener.on_error(resp.status_code) is False:
File "twitter.py", line 52, in on_error
print >> sys.stderr, 'Encountered error with status code:', status_code
TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and 'file'
Press any key to continue . . .
Может ли кто-нибудь мне помочь? Это мой код:
from __future__ import absolute_import, print_function
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import time
import simplejson
import json
import tweepy
from PIL import Image
from io import StringIO
import webbrowser
import sys
from pygame import mixer
consumer_key="xxx"
consumer_secret="xxx"
print("Waiting for a Tweet...")
access_token="xxx"
access_token_secret="xx"
class CustomStreamListener(tweepy.StreamListener):
def on_status(self, status):
mixer.init()
try:
if 'media' in status.extended_entities:
for media in status.extended_entities['media']:
if media.get("type", None) == 'photo':
webbrowser.open_new_tab(media['media_url'])
except:
pass
try:
if 'media' in status.extended_entities:
for media in status.extended_entities.get("media", [{}]):
if media.get("type", None) == "video" or "animated_gif":
webbrowser.open_new_tab(media["video_info"]["variants"][0]["url"])
except:
pass
try:
if 'urls' in status.entities:
for media in status.entities.get("urls", [{}]):
webbrowser.open_new_tab(media["expanded_url"])
except:
pass
if status.text:
print(status.text)
def on_error(self, status_code):
print >> sys.stderr, 'Encountered error with status code:', status_code
return True
def on_timeout(self):
print >> sys.stderr, 'Timeout...'
return True
if __name__ == '__main__':
l = CustomStreamListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = Stream(auth, l)
api.userstream(_with='@username')