Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. from tweepy.streaming import StreamListener
  4. import json
  5. import time
  6. import sys
  7.  
  8. class SListener(StreamListener):
  9. def __init__(self, api = None, fprefix = 'streamer'):
  10. self.api = api or API()
  11. self.counter = 0
  12. self.fprefix = fprefix
  13. self.output = open('%s_%s.json' % (self.fprefix, time.strftime('%Y%m%d-%H%M%S')), 'w')
  14.  
  15.  
  16. def on_data(self, data):
  17. if 'in_reply_to_status' in data:
  18. self.on_status(data)
  19. elif 'delete' in data:
  20. delete = json.loads(data)['delete']['status']
  21. if self.on_delete(delete['id'], delete['user_id']) is False:
  22. return False
  23. elif 'limit' in data:
  24. if self.on_limit(json.loads(data)['limit']['track']) is False:
  25. return False
  26. elif 'warning' in data:
  27. warning = json.loads(data)['warnings']
  28. print("WARNING: %s" % warning['message'])
  29. return
  30.  
  31.  
  32. def on_status(self, status):
  33. self.output.write(status)
  34. self.counter += 1
  35. if self.counter >= 20000:
  36. self.output.close()
  37. self.output = open('%s_%s.json' % (self.fprefix, time.strftime('%Y%m%d-%H%M%S')), 'w')
  38. self.counter = 0
  39. return
  40.  
  41.  
  42. def on_delete(self, status_id, user_id):
  43. print("Delete notice")
  44. return
  45.  
  46.  
  47. def on_limit(self, track):
  48. print("WARNING: Limitation notice received, tweets missed: %d" % track)
  49. return
  50.  
  51.  
  52. def on_error(self, status_code):
  53. print('Encountered error with status code:', status_code)
  54. return
  55.  
  56.  
  57. def on_timeout(self):
  58. print("Timeout, sleeping for 60 seconds...")
  59. time.sleep(60)
  60. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement