Last active
April 11, 2021 04:16
-
-
Save shadabshaukat/8bd377b46681ab23d830b07b9762146c to your computer and use it in GitHub Desktop.
Stream Tweets Oracle Cloud Infrastructure Py2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Python2.7 ## | |
| import sys | |
| import json | |
| import tweepy | |
| import cx_Oracle | |
| import datetime | |
| from tweepy import Stream | |
| from tweepy import OAuthHandler | |
| from tweepy.streaming import StreamListener | |
| from datetime import datetime | |
| now = datetime.now() # current date and time | |
| date_time = now.strftime("%m/%d/%Y, %H:%M:%S") | |
| print("date and time:",date_time) | |
| #connection string for database | |
| ## paygdev_high is TNS entry on the instance ## | |
| conn_str='ADMIN/******@paygdev_high' | |
| #Get a connection to the DB | |
| conn =cx_Oracle.connect(conn_str) | |
| #turn on autocommit | |
| conn.autocommit=1 | |
| #object for executing sql | |
| c=conn.cursor() | |
| #clob variable | |
| bvar=c.var(cx_Oracle.CLOB) | |
| #Create twitter api application keys | |
| #consumer key, consumer secret, access token, access secret. | |
| ckey='Do' | |
| csecret='Not Disclose' | |
| atoken='These' | |
| asecret='Secret Keys' | |
| # Create API object | |
| auth = OAuthHandler(ckey, csecret) | |
| auth.set_access_token(atoken, asecret) | |
| api = tweepy.API(auth) | |
| # Verify Credentials | |
| try: | |
| api.verify_credentials() | |
| print("Authentication OK!") | |
| except: | |
| print("Error during Authentication") | |
| # Create a tweet | |
| api.update_status(date_time + ' UTC - (OCI Demo) Twitter Stream Started. Ingesting Data into Oracle Autonomous DB....') | |
| #listen to the stream | |
| class listener(StreamListener): | |
| def on_status(self, status): | |
| if status.retweeted: | |
| return | |
| def on_data(self, data): | |
| try: | |
| #Parse response into json object | |
| all_data = json.loads(data) | |
| #parse out tweet text, screenname and tweet id | |
| tweet = all_data["text"] | |
| if (all_data["user"]["screen_name"]) is not None: | |
| username = all_data["user"]["screen_name"] | |
| else: | |
| username = 'No User' | |
| tid = all_data["id"] | |
| #set clob variable to json doc | |
| bvar.setvalue(0,data) | |
| try: | |
| #create sql string with bind for clob var | |
| sql_str="INSERT INTO TWEET_LIVE_STREAM (ID,USERNAME,TWEET_JSON) Values("+str(tid)+",q'["+username.encode('utf-8').strip()+"]',:EATIT)" | |
| #insert into database | |
| c.execute(sql_str,[bvar]) | |
| except Exception: | |
| sys.exc_clear() | |
| #watch tweets go by in console | |
| print((username,tweet)) | |
| #in case you want to print response | |
| #print data | |
| return(True) | |
| except Exception: | |
| sys.exc_clear() | |
| def on_error(self, status): | |
| print status | |
| # print data | |
| # print sql_str | |
| #log in to twitter api | |
| auth = OAuthHandler(ckey, csecret) | |
| auth.set_access_token(atoken, asecret) | |
| #fire it up | |
| twitterStream = Stream(auth, listener()) | |
| #what to search for (not case sensitive) | |
| #comma separated for words use # for hashtag | |
| # languages set to English tweets only | |
| #phrases are words separated by spaces (like this comment) | |
| # OR Operator | |
| #twitterStream.filter(track=["Oracle", "OCI", "TikTok"],languages=["en"]) | |
| # AND Operator | |
| twitterStream.filter(track=["Oracle TikTok"],languages=["en"]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment