Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add data to mongoDB in python

Tags:

python

mongodb

I am a python noob (working with it for less than a few hours). I'm trying to read in twitter data and store it in a mongo database, but I am getting the following error:

Traceback (most recent call last):
  File "twit_test.py", line 8, in on_receive
    db.posts.insert(data)
  File "/Library/Python/2.6/site-packages/pymongo-2.0.1-py2.6-macosx-10.6-universal.egg/pymongo/collection.py", line 274, in insert
  File "/Library/Python/2.6/site-packages/pymongo-2.0.1-py2.6-macosx-10.6-universal.egg/pymongo/database.py", line 249, in _fix_incoming
  File "/Library/Python/2.6/site-packages/pymongo-2.0.1-py2.6-macosx-10.6-universal.egg/pymongo/son_manipulator.py", line 73, in transform_incoming
TypeError: 'str' object does not support item assignment
Traceback (most recent call last):
  File "twit_test.py", line 17, in <module>
    conn.perform() 

My code is very simple:

import pycurl, json
import pymongo

STREAM_URL = "https://stream.twitter.com/1/statuses/sample.json"
USER = "XXXXXXXX"
PASS = "XXXXXXXX"
def on_tweet(data):
  tweet = json.loads(data)
  db.posts.insert(tweet)

from pymongo import Connection
connection = Connection()
db = connection.test
conn = pycurl.Curl()
conn.setopt(pycurl.USERPWD, "%s:%s" % (USER, PASS))
conn.setopt(pycurl.URL, STREAM_URL)
conn.setopt(pycurl.WRITEFUNCTION, on_tweet)
conn.perform() 

I'm sure this is a VERY simple fix, hope you guys can help. Thanks!

like image 388
WildBill Avatar asked Feb 15 '26 23:02

WildBill


1 Answers

On receive you have to buffer the content. When a "\r\n" comes, then you get a tweet and it can be stored in mongodb

def on_tweet(data):
    tweet = json.loads(data)
    db.posts.insert(tweet)


 buffer = ""

 def on_receive(data):
     buffer += data.strip()         
     if (data.endswith("\r\n")):    
         if buffer: 
             on_tweet(buffer)
         buffer = ""

EDIT : I though you were using old streaming api. "on_tweet" function should be enough

like image 109
moliware Avatar answered Feb 18 '26 12:02

moliware



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!