示例#1
0
class TwitterPlayer(player.Player):
    def __init__(self, model, code, access_token, access_token_secret, opponent):
        player.Player.__init__(self, model, code)
        self._opponent = opponent
        self._last_id = None

        self._auth = OAuthHandler(auth.consumer_key, auth.consumer_secret)
        self._auth.set_access_token(access_token, access_token_secret)
        self._api = API(self._auth)
        self._listener = TwitterListener(self, self._api)
        self._stream = Stream(self._auth, self._listener)

    @property
    def username(self):
        return self._auth.get_username()

    def allow(self):
        print 'This is the opponent\'s turn...'
        self._stream.userstream()

    def update(self, event):
        if event.player == self.code:
            return
        message = '@%s %s' % (self._opponent, self._model.events[-1][1])
        self.tweet(message)

    def tweet(self, message):
        if self._last_id is None:
            self._api.update_status(message)
        else:
            self._api.update_status(message, self._last_id)
示例#2
0
 def validate_twitter(self):
     try:
         auth = OAuthHandler(self.consumer_key, self.consumer_secret)
         auth.set_access_token(self.access_token, self.access_token_secret)
         username = auth.get_username()
         self.twitter_auth = auth if username else None
     except Exception:
         if self.consumer_key and self.consumer_secret and self.access_token and self.access_token_secret:
             LOG.warning(
                 "Your Twitter token is invalid or you've used the wrong credentials.")
             LOG.warning(
                 "Please check consumer_key, consumer_secret, access_token, access_token_secret at https://apps.twitter.com")
         self.twitter_auth = None
示例#3
0
def main(creds=default_creds, debug=False):
    auth = OAuthHandler(creds['consumer_key'], creds['consumer_secret'])
    auth.set_access_token(creds['access_token'], creds['access_secret'])

    if debug:
        print("credentials:\t\t%s\n" % creds)
        print("filters:\t\t%s\n" % f)
        try:
            print(auth.get_username())
        except:
            print('auth failed')

    stream = tweepy.Stream(auth=auth, listener=TweepyListener())
    return stream
def auth():
    """ once you have registered for API, you can
        find 'em keys at https://apps.twitter.com while you are logged in with your account
        then click the API
        go-to "Keys and Access Tokens" tab for your keys and stuff."""
    consumer_key = ''
    consumer_secret = ''
    access_token = ''
    access_token_secret = ''
    try:
        # initialize the Oauth object for OAuthHandler class in tweepy module
        # parameters are consumer key, consumer_secret and callback(optional)
        oauth = OAuthHandler(consumer_key, consumer_secret)
        oauth.set_access_token(access_token, access_token_secret)
        username = OAuthHandler.get_username(oauth)
        api = API(oauth)
        yield api
        yield username
    except TweepError:
        return 0
示例#5
0
def main(creds=default_creds, debug=False):
    listener = TweepyListener()

    auth = OAuthHandler(creds['consumer_key'], creds['consumer_secret'])
    auth.set_access_token(creds['access_token'], creds['access_secret'])
    stream = tweepy.Stream(auth=auth, listener=listener)

    if debug:
        print("credentials")
        print(creds)
        print('-------')
        print("filters")
        print(f)
        print('-------')
        try:
            print(auth.get_username())
        except:
            print('fail with auth')

    return stream
示例#6
0
def auth():
    """ once you have registered for API, you can
        find 'em keys at https://apps.twitter.com while you are logged in with your account
        then click the API
        go-to "Keys and Access Tokens" tab for your keys and stuff."""
    consumer_key = 'taiVHaeP9PvO6iyYZMfhg9s2S'
    consumer_secret = 'JFDKEcTsURteiNA8qTelBsD8SmcgMnkvO5xOTb63hRVaQBBvsl'
    access_token = '939786756982849536-7qUj33koKvgAO4HXnFeqhsNXfgL7yoz'
    access_token_secret = 'vYvg2NtoqFTbXMvXS1qYF6clKa6UJ83w1efZ7IJXiNw39'
    try:
        # initialize the Oauth object for OAuthHandler class in tweepy module
        # parameters are consumer key, consumer_secret and callback(optional)
        oauth = OAuthHandler(consumer_key, consumer_secret)
        oauth.set_access_token(access_token, access_token_secret)
        username = OAuthHandler.get_username(oauth)
        api = API(oauth)
        yield api
        yield username
    except TweepError:
        return 0
consumer_key=Config.get(twcred,"consumer_key")
consumer_secret=Config.get(twcred,"consumer_secret")

access_token=Config.get(twcred,"access_token")
access_token_secret=Config.get(twcred,"access_token_secret")

class StdOutListener(StreamListener):
    """ A listener handles tweets are the received from the stream.
    This is a basic listener that just prints received tweets to stdout.

    """
    def on_data(self, data):
        print data
        return True

    def on_error(self, status):
        print status

if __name__ == '__main__':
    l = StdOutListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    pprint (vars(auth))
    auth.set_access_token(access_token, access_token_secret)

    pprint (vars(auth))
    print auth.get_username()
    

    stream = Stream(auth, l)
    stream.filter(track=['basketball'])
示例#8
0
          "keywords: " + keywords + " \n" +
          "welcome_msg: " + welcome_msg + " \n" +
          "tease_on: " + tease_on_regex + " \n" +
          "teaser_text: " + teaser_text + " \n" +
          "ffwd_msg: " + ffwd_msg + " \n" +
          "log_dir: " + log_dir + "\n" + 
          "log_level: " + log_level
          )
 

  if os.uname()[4].startswith("arm"):
    logging.info("Running on a Raspberry Pi.")
    is_rpi = True
    import RPi.GPIO as GPIO
  else:
    logging.info("Not a Raspberry Pi. Any of the GPIO commands will not be executed.")

  if is_rpi:
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(relay_pin, GPIO.OUT, initial = GPIO.LOW)

  l = OpenSesame()
  auth = OAuthHandler(consumer_key, consumer_secret)
  auth.set_access_token(access_token, access_token_secret)
  username = auth.get_username()
  api = tweepy.API(auth)

  stream = Stream(auth, l)
  stream.userstream()