Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. from tweepy.streaming import StreamListener
  2. from tweepy import OAuthHandler
  3. from tweepy import Stream
  4. import tweepy
  5.  
  6. consumer_key="KqiUO3abFVhv0iIMcBYV0BxUf"
  7. consumer_secret="7shjEbPXs4l5mejXxZgGhaqm8XAFiWhsv1EnYplmT60hAll9yc"
  8. access_token="1098485478909771777-2qWb39Gju80n080aDpnQunAm22MliI"
  9. access_token_secret="m83ujY8fqxDW9hKa0MKwjJwLPCTgBxu3goc9U0JRekv5y"
  10.  
  11.  
  12.  
  13. MAX_TWEETS = 5
  14.  
  15. auth = OAuthHandler(consumer_key, consumer_secret)
  16. api = tweepy.API(auth)
  17.  
  18. #for tweet in tweepy.Cursor(api.search, q='#python', rpp=100).items(MAX_TWEETS):
  19. # a = tweet
  20.  
  21. #gh = a['created_at']
  22. #if gh and isinstance(gh, list):
  23. # Assuming you want to iterate over the list
  24. #for ghs in gh:
  25. #c = ghs.get('created_at')
  26. #print(c)
  27.  
  28.  
  29.  
  30. # def get_tweets(username):
  31.  
  32. # # Authorization to consumer key and consumer secret
  33. # auth = OAuthHandler(consumer_key, consumer_secret)
  34.  
  35. # # Access to user's access key and access secret
  36. # auth.set_access_token(access_token, access_token_secret)
  37.  
  38. # # Calling api
  39. # api = tweepy.API(auth)
  40.  
  41. # # 200 tweets to be extracted
  42. # number_of_tweets=20
  43. # tweets = api.user_timeline(screen_name=username)
  44.  
  45. # # Empty Array
  46. # tmp=[]
  47.  
  48. # # create array of tweet information: username,
  49. # # tweet id, date/time, text
  50. # tweets_for_csv = [tweet.text for tweet in tweets] # CSV file created
  51. # for j in tweets_for_csv:
  52.  
  53. # # Appending tweets to the empty array tmp
  54. # tmp.append(j)
  55.  
  56. # # Printing the tweets
  57. # print(tmp)
  58.  
  59.  
  60. # Driver code
  61. # if __name__ == '__main__':
  62.  
  63. # # Here goes the twitter handle for the user
  64. # # whose tweets are to be extracted.
  65. # get_tweets("twitter-handle")
  66.  
  67.  
  68.  
  69.  
  70. class MyStreamListener(StreamListener):
  71.  
  72. def on_data(self, data):
  73. count = 0
  74. count +=1
  75. while count >=11:
  76. print(data)
  77. break
  78. #savefile=open('tweetsdata.csv','a')
  79. #savefile.write(data)
  80. #savefile.write('/n')
  81. #savefile.close()
  82. #return True
  83.  
  84. def on_error(self, status):
  85. print(status)
  86.  
  87. if __name__ == '__main__':
  88. l = MyStreamListener()
  89.  
  90. auth = OAuthHandler(consumer_key, consumer_secret)
  91. auth.set_access_token(access_token, access_token_secret)
  92. a=input("enter hashtag")
  93. stream = Stream(auth, l)
  94. stream.filter(track=[a])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement