Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from tweepy.streaming import StreamListener
  4. from tweepy import OAuthHandler
  5. from tweepy import Stream
  6. from datetime import datetime
  7.  
  8. #access_token = '105809219-LoQ2ti1dcO0tZpSECWCyteeIpaerWoqISKKvrbXV'
  9. #access_token_secret = 'bwNY0BcU3VKPygM5ZpiDTmtYnbPqzywdvQHLNNr9CMa1e'
  10. #consumer_key = 'PN9JvV4BpOpvZgHoSqdMi2Oy9'
  11. #consumer_secret = '4zWp5hzFgLMsVYsLCKhrLNRQumj62qmlPCM8le94cbEruyq6VP'
  12. #estas contrasennas son de nachoo, devolverselas cuando el quiera sacar sus propios tweets
  13. access_token = '307706130-yf5CsDgNqW183WE3saE39fz6gXgCRRvIwEMAKyVj'
  14. access_token_secret = 'SyyESQ93PKAFYF2e9OKqxdytHyMdA4wtZ4p3Qlb7Q93c0'
  15. consumer_key = 'irvY2ST0cYvpyFjLkIESGYaWs'
  16. consumer_secret = 'cYHJU0epL8MVR70CfYK9BmWBHnNMjvRelk7CUVy64TTjqCcMO9'
  17.  
  18. EPOCH_SIZE = 10000
  19.  
  20. class StdOutListener(StreamListener):
  21. def __init__(self):
  22. self.epoch = 1
  23. self.tweets = 0
  24. self.out_file = datetime.now().strftime("%Y_%m_%d_%H_%M")+'_Facebook_Data_Stream_'
  25. self.output = open(self.out_file+str(self.epoch)+'.txt', 'a')
  26.  
  27. def shutdown(self):
  28. print("{0} Epochs, {1} Tweets guardados".format(self.epoch, (self.epoch+1)*EPOCH_SIZE))
  29. self.output.close()
  30.  
  31. def on_data(self, data):
  32. self.tweets += 1
  33. self.output.write(data)
  34. if self.tweets >= EPOCH_SIZE:
  35. print("[Epoch {0}]: {1} Tweets guardados".format(self.epoch, (self.epoch+1)*EPOCH_SIZE))
  36. self.output.close()
  37. self.tweets = 0
  38. self.epoch += 1
  39. self.output = open(self.out_file+str(self.epoch)+'.txt', 'a')
  40. return True
  41.  
  42. def on_error(self, status):
  43. print('error')
  44. print(status)
  45. self.shutdown()
  46.  
  47.  
  48. if __name__ == '__main__':
  49. while True:
  50. try:
  51. l = StdOutListener()
  52. auth = OAuthHandler(consumer_key, consumer_secret)
  53. auth.set_access_token(access_token, access_token_secret)
  54. stream = Stream(auth, l)
  55. stream.filter(languages=["es"], track=['#toquedequeda','#ChileDesperto','#YoApoyoAlPresidente','#EstadoEmergencia','#AquiFaltaPinochet','#RenunciaPineraCuliao','#ChileEnResistencia','#DespiertaChile'])
  56. except Exception as err:
  57. print(err.message)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement