11import  tweepy 
22import  time 
3+ from  datetime  import  datetime 
34
45"""Un comment the below modules to get the real time tweets""" 
56
1112
1213
1314# Consumer keys and access tokens, used for OAuth 
14- consumer_key  =  'oM6OieC09OJJLeVJ8UbZkJPJO ' 
15- consumer_secret  =  'z0YUK4NNpOoSSRhY9dJqjVLzPhhU0V38OvN7ELvurdhnfLZi8D ' 
16- access_token  =  '2989212755-8KfCUDwcs8tdO27wme3qXCTnUnbBGooi85vdZoE ' 
17- access_token_secret  =  'AOAboTWRsOqAyfyIVIqQEWZicT7MkCMxhueSy600meu2M ' 
15+ consumer_key  =  '' 
16+ consumer_secret  =  '' 
17+ access_token  =  '' 
18+ access_token_secret  =  '' 
1819
1920
2021# OAuth process, using the keys and tokens 
2425""" Database Creation""" 
2526# con=MySQLdb.connect('localhost','root','root','mysql') 
2627# cur=con.cursor() 
27- 
28- 
29- 
3028# Creation of the actual interface, using authentication 
3129api  =  tweepy .API (auth )
32- 
33- 
3430"""To get all tweets from user timeline""" 
31+ """Pass twitter profile name as keword to tweet function""" 
3532
3633
37- 
38- """Pass twitter profile name as keword to tweet function""" 
39- def  tweet (twitter_profile ,business_id ):
40-     fn = '%s' % twitter_profile + '_tweets.csv' 
41-     f = open (fn ,'w' )
34+ def  tweet (twitter_profile , business_id , x ):
35+     fn  =  '%s'  %  twitter_profile + '_tweets.csv' 
36+     f  =  open (fn , 'w' )
4237    f .write ('Business_ID\t Name\t Time\t Tweets\t Retweet_Count\t Fav_count\n ' )
4338
44-     tw = tweepy .Cursor (api .user_timeline , id = twitter_profile ).items ()
39+     tw   =   tweepy .Cursor (api .user_timeline , id = twitter_profile ).items ()
4540    while  True :
4641        try :
47-             c =  tw .next ()
48- 
42+             c  =  tw .next ()
4943            try :
50-                 a =  c .retweeted_status .user .name .encode ('utf-8' )
44+                 a   =  c .retweeted_status .user .name .encode ('utf-8' )
5145            except :
52-                 a =  c .user .name .encode ('utf-8' )
53-             b =  c .created_at 
54-             d =  c .text .encode ('utf-8' ).replace ("'" ,"" ).replace ('\n ' ,'' ).replace ('"' ,'' )
55-             fc =  c .favorite_count 
56-             rt  =  c .retweet_count 
57- 
58-             print  business_id ,a ,b ,d ,fc ,rt 
59-             """Insert the tweet details into the csv file""" 
60-             f .write ('%s\t %s\t %s\t %s\t %s\t %s\n ' % (business_id ,a ,b ,d ,fc ,rt ))
61- 
62-             """Create a tabel called tweet with 3 columns and insert the data by uncomment""" 
63-             #sql=("insert into tweet(title,time,tweet) values('%s','%s','%s')"%(a,b,d)) 
64-             #cur.execute(sql) 
65-             #con.commit() 
66- 
46+                 a  =  c .user .name .encode ('utf-8' )
47+             b  =  c .created_at 
6748
49+             d  =  c .text .encode ('utf-8' ).replace ("'" , "" ).replace ('\n ' , '' ).replace ('"' , '' )
50+             fc  =  c .favorite_count 
51+             rt  =  c .retweet_count 
52+             try :
53+                 old_tweet_time  =  datetime .strptime (x , '%Y-%m-%d %H:%M:%S' )
54+             except :
55+                 old_tweet_time  =  datetime .strptime ('1' , '%d' )
56+             if  b  >  old_tweet_time :
57+                 print  business_id , a , b , d , fc , rt 
58+                 """Insert the tweet details into the csv file""" 
59+                 f .write ('%s\t %s\t %s\t %s\t %s\t %s\n '  %  (business_id , a , b , d , fc , rt ))
60+                 """Create a tabel called tweet with 3 columns and insert the data by uncomment""" 
61+             else :
62+                 break 
63+             # sql=("insert into tweet(title,time,tweet) values('%s','%s','%s')"%(a,b,d)) 
64+             # cur.execute(sql) 
65+             # con.commit() 
6866        except  tweepy .TweepError :
6967            print  "Got Exception Please wait for 15 Min to ReConnect" 
7068            time .sleep (60  *  15 )
@@ -97,11 +95,8 @@ def tweet(twitter_profile,business_id):
9795#    stream = Stream(auth, listener) 
9896"""Use the twitter Id to follow the realtime tweets follow=[' Id here']""" 
9997#   stream.filter(follow=['2874668814'],track=[]) 
98+ """Pass the twitter profile name,Business_ID and last tweet time to get the whole tweets""" 
10099
100+ """For Example ***tweet('firebrewbar',10004,'2015-01-20 01:25:51')  to get all tweets give empty string to tweet time(last arg)***""" 
101101
102- 
103- 
104- """Pass the twitter profile name and Business_ID to get the whole tweets""" 
105- 
106- 
107- tweet ('firebrewbar' ,10006 )
102+ tweet ('firebrewbar' , 10006 , '2015-01-20 01:25:51' )
0 commit comments