1

I've been trying to use Tweepy to stream tweets using the Twitter API and save them into a CouchDB database. However, the database remains empty, and no tweets are being saved. I've checked my Twitter API keys, access permissions, and the filter terms, but the problem persists. I've also added debug messages in my code to check if the tweets are being received correctly, but I'm not getting any error messages.
this is my code :

import tweepy
import json
import couchdb

server = "http://username:password@127.0.0.1:5984/"  
database_name = "test"


consumer_key = ''
consumer_secret = ''
access_token_key = ''
access_token_secret = ''


filterTerms = ["twitter"]

try:
    couchclient = couchdb.Server(server)
    print("Connected to CouchDB Server")
except:
    print("Cannot connect to CouchDB Server")
    raise


try:
    db = couchclient.create(database_name)
    print("Database created")
except couchdb.http.PreconditionFailed:
    db = couchclient[database_name]
    print("Using existing database")

# OAuth
auth1 = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth1.set_access_token(access_token_key, access_token_secret)


class StreamListener(tweepy.StreamListener):
    def on_status(self, tweet):
        print('Ran on_status')

    def on_error(self, status_code):
        return False

    def on_data(self, data):
        if data[0].isdigit():
            pass
        else:

            jdata = json.loads(data)
            print("Received tweet:", jdata)
            try:
                db.save(jdata)
                print("Tweet saved to the database:", jdata)
            except Exception as e:
                print("Error saving tweet:", e)


l = StreamListener()
streamer = tweepy.Stream(auth=auth1, listener=l)
print("Streaming started...")
streamer.filter(track=filterTerms)

Thank you in advance for your assistance!

A solution to the Issue with Twitter API and streaming tweets using Tweepy and CouchDB

rahes
  • 21
  • 1

0 Answers0