Extract tweet and save with mysql

by: abbey, 9 years ago

Last edited: 9 years ago


from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import MySQLdb
import time
import json

# replace mysql.server with "localhost" if you are running via your own server!
# server       MySQL username MySQL pass  Database name.
#                        MySQL server , username, password and databasename.
conn = MySQLdb.connect("localhost", "root", "adminremoved", "adminremoved")

c = conn.cursor()

# consumer key, consumer secret, access token, access secret.
ckey = "adminremoved"
csecret = "adminremoved"
atoken = "adminremoved"
asecret = "adminremoved"


class listener(StreamListener):

    def on_data(self, data):
        username, tweetmsg, tweet_date = json.loads(data)

        # tweet = all_data["text"]

        # username = all_data["user"]["screen_name"]

        # username = all_data['abbeydupsy']
        # tweetmsg = all_data['Thank you Lord']
        # tweet_date =all_data['2015-11-11']

        c.execute("INSERT INTO mytbf_tweet (tweetid, username, tweetmsg, tweet_date) VALUES (%s,%s,%s,%s)",
                  (time.time(), username, tweetmsg, tweet_date))

        conn.commit()

        print(username, username, tweetmsg, tweet_date)

        return True

    def on_error(self, status):
        print (status)


# Authenticating
auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)

twitterStream = Stream(auth, listener())
twitterStream.filter(track=['zombie', 'threat', 'exploit', 'fawkessecurity', 'worms', 'virus'], languages= ["en"])

please help, i error 401.



You must be logged in to post. Please login or register an account.



I went ahead and removed your keys and database password. Try to not post that information. Error 401 is an authentication error with Twitter. Maybe your account isn't yet verified? I seem to recall you need to confirm your email for apps now. Maybe also typoed some of your auth info? For more information on twitter error codes: https://dev.twitter.com/overview/api/response-codes

-Harrison 9 years ago

You must be logged in to post. Please login or register an account.