Using Python, Tweepy, Insert Cursor and Arcpy

488 views Asked by At

I am super new to Python so forgive me for my lack of knowledge haha but for some reason I cannot get Python to insert rows in my database. Here is what I have:

import sys, arcpy, datetime, tweepy


consumer_key = " "
consumer_secret = " "
access_token = " "
access_token_secret = " "

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

table = r"C:\....dbf"

rows = arcpy.InsertCursor(table)

class CustomStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        try:


            user = status.user.screen_name
            tweet = status.text
            coord_x = status.coordinates['coordinates'][0]
            coord_y = status.coordinates['coordinates'][1]
            date_utc = status.created_at
            h_m_s_utc = (str(status.created_at.hour))+':'+(str(status.created_at.minute))+':'+(str(status.created_at.second))
            date_est = datetime.datetime.now()
            h_m_s_est = (str(date_est.hour))+':'+(str(date_est.minute))+':'+(str(date_est.second))


            row.user_name=user
            row.tweet=tweet
            row.coord_x=coord_x
            row.coord_y=coord_y
            row.date_utc=date_utc
            row.h_m_s_utc=h_m_s_utc
            row.date_est=date_est
            rows.insertRow(row)
            del row, rows
            insert_table= r"C:\....dbf"
            insert_row(insert_table)

        print user
        print tweet

    except:
        # If there are no coordinates for a tweet, then pass
        pass

def on_error(self, status_code):
    print >> sys.stderr, 'Encountered error with status code:', status_code
    return True # Don't kill the stream

def on_timeout(self):
    print >> sys.stderr, 'Timeout...'
    return True # Don't kill the stream

# ----------------Script execution----------------
listener = tweepy.streaming.Stream(auth, CustomStreamListener())
listener.filter(track=[' love ', '#love'])

I am pretty sure it has something to do with the row.rowID thing.

Sorry if it is a disaster! Any help is much appreciated!

1

There are 1 answers

0
tpdance On

I looks like you're forgetting to call the data access (.da) method for the insert cursor.

with arcpy.da.InsertCursor(in_table, field_names) as inCursor:
    for row in rows:
        inCursor.insertRow(row) # example

-or-

inCursor = arcpy.da.InsertCursor(in_table, field_names)
for row in rows:
    cursor.insertRow(row) # example
del inCursor # make sure to delete cursor if you do it this way as to avoid data lock.

Also, if you just want the Insert Cursor method, you can

from arcpy import da

For more info, check out: http://resources.arcgis.com/en/help/main/10.2/index.html#//018w0000000t000000