Guest User

TwitterStreamListener.py

a guest
Mar 4th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. import tweepy
  2. import socket
  3. import requests
  4. import time
  5. from Authentication import authentication
  6. from pyredictit import *
  7. import json
  8. import datetime
  9.  
  10.  
  11. class TwitterStreamListener(tweepy.StreamListener):
  12. """ A listener handles tweets are the received from the stream.
  13. This is a basic listener that just prints received tweets to stdout.
  14. """
  15.  
  16. def on_status(self, status):
  17. get_user_informations(status)
  18.  
  19.  
  20.  
  21. def get_user_informations(tweet):
  22.  
  23. if str(tweet.user.id) == 'twitter_id':
  24. tweet_count = tweet.user.statuses_count
  25. print("Apoc Tweet Count is ", tweet_count, " ", datetime.datetime.now())
  26. buytest()
  27.  
  28.  
  29.  
  30. def connectToApi():
  31. auth = authentication()
  32.  
  33. consumer_key = auth.getconsumer_key()
  34. consumer_secret = auth.getconsumer_secret()
  35.  
  36. access_token = auth.getaccess_token()
  37. access_token_secret = auth.getaccess_token_secret()
  38.  
  39. # Authentication
  40. auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  41. auth.secure = True
  42. auth.set_access_token(access_token, access_token_secret)
  43.  
  44. api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True, retry_count=10, retry_delay=5,
  45. retry_errors=5)
  46.  
  47. streamListener = TwitterStreamListener()
  48. myStream = tweepy.Stream(auth=api.auth, listener=streamListener)
  49.  
  50. myStream.filter(follow=['Twitter_Id'], async=True)
  51. user = api.get_user('TwitterUserName')
  52. print("Initial Tweet Count is ", str(user.statuses_count), " at ", datetime.datetime.now())
  53.  
  54.  
  55. def buytest():
  56. pyt1 = pyredictit()
  57. pyt1.create_authed_session(username=' ',
  58. password=' ')
  59. short_sell_contracts = pyt1.search_for_contracts(market='politics', buy_sell='buy', type_='short')
  60. first_contract = short_sell_contracts[151]
  61. print(first_contract.market)
  62. print(first_contract.type_)
  63. first_contract.buy_shares(number_of_shares=100, api=pyt1, buy_price=first_contract.buy)
  64.  
  65.  
  66. def buytest_all():
  67. pyt1 = pyredictit()
  68. pyt1.create_authed_session(username=' ',
  69. password=' ')
  70. short_sell_contracts = pyt1.search_for_contracts(market='politics', buy_sell='buy', type_='short')
  71. first_contract = short_sell_contracts[150]
  72. print(first_contract.market)
  73. print(first_contract.type_)
  74. while True:
  75. pyt1.monitor_price_of_contract(contract=first_contract, monitor_type='buy_at',
  76. number_of_shares=1, trigger_price=0.70)
  77. sleep(30)
  78.  
  79.  
  80.  
  81. if __name__ == '__main__':
  82. connectToApi()
  83. # Get access and key from another class
Add Comment
Please, Sign In to add comment