Tweepy:流侦听器未捕获任何内容

时间:2018-09-18 16:21:01

标签: python twitter tweepy

我有这段代码曾经可以像三个月前一样正常工作,但现在却无法正常工作。流侦听器未捕获任何内容。它只是沉默,既不捕获推文也不引发错误。它正在运行,但是我看不到任何活动。我通过更新状态检查了API连接。我正在Google Cloud Instance中运行它,但是我也在工作站上尝试了它。因此,我可以排除与Google Cloud Instance有关的问题。

# Standard library
import logging
import datetime, time
import sqlite3

# External library
import tweepy

# Logging configuration
logging.basicConfig(filename='../.log/tweets_capture.log',level=logging.INFO)

# Twitter OAuth authentication
# This is where your key and secrete for twitter login should go.
# More info at https://www.slickremix.com/docs/how-to-get-api-keys-and-tokens-for-twitter/
with open('../../cred/bioinfobotmain.txt', 'r') as f: # Reading the credentials from a text file.
    creds = f.readlines()
    consumer_key = creds[0].rstrip()
    consumer_secret = creds[1].rstrip()
    access_token = creds[2].rstrip()
    access_token_secret = creds[3].rstrip()
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)
#api.update_status('Start Streaming')

# Subclass for stream listener
class StreamListener(tweepy.StreamListener):
    def on_status(self, status):
        print(status.text)
        if status.lang == 'en' and 'RT'.upper() not in status.text :
            stat = status.text
            stat = stat.replace('\n','')
            stat = stat.replace('\t','')
            user_id = status.user.id_str
            stat_id = status.id_str
            create = str(status.created_at)
            name = status.user.screen_name
            data = (create, name, user_id, stat_id, stat)
            #Connecting to SQLite3 database
            try:
                db_file = '../../db/bioinfotweet.db'
                conn = sqlite3.connect(db_file, isolation_level=None)
                conn.execute('PRAGMA journal_mode=wal') # This will let concurrent read and write to the database. 
                c = conn.cursor()
                c.execute("INSERT INTO tweetscapture (Date, ScreenName, UserID, TweetID, Text) values (?, ?, ?, ?, ?)", data)
                conn.commit()
                cdate="Tweet inserted at: "+str(datetime.datetime.now())
                logging.info(cdate)
                conn.close()
            except Exception as ex:
                exname = str(ex)
                template = "An exception of type {0} occurred. Arguments:\n{1!r}"
                message = template.format(type(ex).__name__, ex.args)
                logging.info("Sqlite3 database exception occurred.")
                logging.info(message)

    def on_error(self, status_code):
        if status_code == 420:
            cdate = "Error code 420 at:"+str(datetime.datetime.now())
            logging.info(cdate)
            logging.info("Sleeping for 15 mins")
            time.sleep(900)
        return False

stream_listener = StreamListener()
stream = tweepy.Stream(auth=api.auth, listener=stream_listener)
cdate="Stream started at: "+str(datetime.datetime.now())
logging.info(cdate)

while True:
    try:
        stream.userstream(encoding='utf8')
    except Exception as ex:
        exname = str(ex)
        template = "An exception of type {0} occurred. Arguments:\n{1!r}"
        message = template.format(type(ex).__name__, ex.args)
        logging.info("Generic exception occurred.")
        logging.info(message)
        if "not defined" in exname:
            break
        else:
            logging.info("Sleeping for 60 sec")
            time.sleep(60)
            continue

1 个答案:

答案 0 :(得分:1)

用户流已于2018年8月23日停用。您的代码调用stream.userstream将不再起作用。有关更多信息,请参见this announcement