'how do i transfer live twitter steam to mongo db in python
I need to make an app that will live stream tweets from twitter api to one of two things. I can either have them save to a file then from the file save them to my mongo db database, or I can have it go from the stream directly to the database.
Right now the program will save it to a file and print it in the terminal but I can not figure out where I would do the transfer. if i put the insert one in the on_status it just jumps out as soon as it hits that line and if i put it in the main area, it never does it.
from tweepy import Stream
from tweepy import OAuthHandler
import tweepy
import json
import pymongo
#tokens and access
access_token = ''
access_token_secret = ''
API_KEY = ''
API_KEY_secret = ''
#make connection to database
connection = pymongo.MongoClient('localhost', 27017)
#create the database and collection
database = connection.twitter_db
collection = database.tweets.create_index([("id", pymongo.ASCENDING)], unique=True,)
class MyListener (tweepy.Stream):
def on_status(self, status):
json_str=json.dumps(status._json)
print(json_str)
try:
with open("UkraineTweets.json","a") as file:
file.write(json_str + "\n")
print("written to the file")
#collection.insert_one(data)
#print("entered into mongo_db")
return True
except:
pass
return False
if __name__ == "__main__":
authentication = tweepy.OAuthHandler(API_KEY,API_KEY_secret)
authentication.set_access_token(access_token, access_token_secret)
api = tweepy.API(authentication)
stream = MyListener(API_KEY, API_KEY_secret, access_token, access_token_secret)
stream.filter(track=['Russia', 'Ukraine', 'war', 'Putin', '#StandWithUkraine' ,'#StopPutinNOW'], languages=['en'])''''
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
