Exemplo n.º 1
0
def oauth_url_dance(consumer_key, consumer_secret, callback_url,
                    oauth_verifier, pre_verify_token_filename,
                    verified_token_filename):
    # Verification happens in two stages...

    # 1) If we haven't done a pre-verification yet... Then we get credentials from Twitter
    # that will be used to sign our redirect to them, find the redirect, and instruct the Javascript
    # that called us to do the redirect.
    if not os.path.exists(CREDS_PRE_VERIFIY):
        twitter = Twitter(auth=OAuth('', '', consumer_key, consumer_secret),
                          format='',
                          api_version=None)
        oauth_token, oauth_token_secret = parse_oauth_tokens(
            twitter.oauth.request_token(oauth_callback=callback_url))
        write_token_file(pre_verify_token_filename, oauth_token,
                         oauth_token_secret)

        oauth_url = 'https://api.twitter.com/oauth/authorize?' + urllib.urlencode(
            {'oauth_token': oauth_token})
        return oauth_url

    # 2) We've done pre-verification, hopefully the user has authed us in Twitter
    # and we've been redirected to. Check we are and ask for the permanent tokens.
    oauth_token, oauth_token_secret = read_token_file(CREDS_PRE_VERIFIY)
    twitter = Twitter(auth=OAuth(oauth_token, oauth_token_secret, consumer_key,
                                 consumer_secret),
                      format='',
                      api_version=None)
    oauth_token, oauth_token_secret = parse_oauth_tokens(
        twitter.oauth.access_token(oauth_verifier=oauth_verifier))
    write_token_file(verified_token_filename, oauth_token, oauth_token_secret)
    return oauth_token, oauth_token_secret
Exemplo n.º 2
0
def get_stream_iterator(track_keywords):
    # TODO: from config
    args = {
        'token': "1010873339819196416-3yDJVTlPLOyptBjx8DAeB9feAXSpJp",
        'token_secret': "i2ddWam867Y1wWYxvC0b4KcYViZuQ36hDoHkYNm8CCUko",
        'consumer_key': "Y3eSkzd8OPea2lUoXypISoBT2",
        'consumer_secret': "nZOjyOuzBjgUgEzygK4zU3T6PiI4MnTLdHSm1Y2JuHiVW00Hfv",
        'timeout': 8000,
        'heartbeat_timeout': 3000,
        'track_keywords': track_keywords,
        'no_block': True
    }

    auth = OAuth(args['token'], args['token_secret'], args['consumer_key'], args['consumer_secret'])

    stream_args = dict(
        timeout = args['timeout'],
        block = not args['no_block'],
        heartbeat_timeout = args['heartbeat_timeout'])

    query_args = dict()
    if args.get('track_keywords'):
        query_args['track'] = args['track_keywords']

    stream = TwitterStream(auth=auth, **stream_args)
    if args.get('track_keywords'):
        tweet_iter = stream.statuses.filter(**query_args)
    else:
        tweet_iter = stream.statuses.sample()
    
    return tweet_iter
Exemplo n.º 3
0
    def __init__(self, connection):
        self.logger = logging.getLogger(self.__name)
        self.dbconnection = connection.dbconnection

        self.output_channel = "#%s" % connection.config.get(
            "lowflyingrocks",
            "channel")

        # OAUTH_FILENAME = os.environ.get(
        #    'HOME',
        #    '') + os.sep + '.lampstand_oauth'
        OAUTH_FILENAME = connection.config.get("twitter", "oauth_cache")
        CONSUMER_KEY = connection.config.get("twitter", "consumer_key")
        CONSUMER_SECRET = connection.config.get("twitter", "consumer_secret")

        try:
            if not os.path.exists(OAUTH_FILENAME):
                oauth_dance(
                    "Lampstand", CONSUMER_KEY, CONSUMER_SECRET,
                    OAUTH_FILENAME)

            self.oauth_token, self.oauth_token_secret = read_token_file(
                OAUTH_FILENAME)

            self.twitter = Twitter(
                auth=OAuth(
                    self.oauth_token,
                    self.oauth_token_secret,
                    CONSUMER_KEY,
                    CONSUMER_SECRET),
                secure=True,
                domain='api.twitter.com')
        except:
            self.twitter = False
Exemplo n.º 4
0
def get_twitter_tools(oauthfile):
    #--- register oauth tokens -------------------------------------------
    try:
        oauth_token, oauth_token_secret = read_token_file(oauthfile)
    except IOError:
        print 'OAuth file {} not found'.format(oauthfile)
        response = raw_input(
            'Do you want to initiate a new oauth dance (y or n)? ')
        if not (len(response) > 0 and response[0].upper() == 'Y'):
            oauth_token = oauth_token_secret = ''
        else:
            oauth_token, oauth_token_secret = oauth_dance(
                'Brilliant App',
                CONSUMER_KEY,
                CONSUMER_SECRET,
                token_filename=oauthfile)

    #--- t1 = Twitter Search API, t2 = Twitter REST API ------------------
    t1 = Twitter(domain='search.twitter.com')
    t2 = Twitter(auth=OAuth(oauth_token, oauth_token_secret, CONSUMER_KEY,
                            CONSUMER_SECRET),
                 secure=True,
                 api_version='1',
                 domain='api.twitter.com')
    return t1, t2
Exemplo n.º 5
0
    def from_oauth_file(cls, filepath=None):
        """Get an object bound to the Twitter API using your own credentials.

        The `twitter` library ships with a `twitter` command that uses PIN
        OAuth. Generate your own OAuth credentials by running `twitter` from
        the shell, which will open a browser window to authenticate you. Once
        successfully run, even just one time, you will have a credential file
        at ~/.twitter_oauth.

        This factory function reuses your credential file to get a `Twitter`
        object. (Really, this code is just lifted from the `twitter.cmdline`
        module to minimize OAuth dancing.)
        """
        if filepath is None:
            # Use default OAuth filepath from `twitter` command-line program.
            home = os.environ.get('HOME', os.environ.get('USERPROFILE', ''))
            filepath = os.path.join(home, '.twitter_oauth')

        oauth_token, oauth_token_secret = read_token_file(filepath)

        twitter = cls(
            auth=OAuth(
                oauth_token, oauth_token_secret, CONSUMER_KEY, CONSUMER_SECRET),
            api_version='1.1',
            domain='api.twitter.com')

        return twitter
Exemplo n.º 6
0
def main():
    args = parse_arguments()
    htmlparser = HTMLParser()

    credentials = load_yaml(args.yaml)

    # When using Twitter stream you must authorize
    auth = OAuth(credentials['access_token'],
                 credentials['access_token_secret'],
                 credentials['consumer_key'],
                 credentials['consumer_secret'])

    # These arguments are optional:
    stream_args = dict(
        timeout=args.timeout,
        block=not args.no_block,
        heartbeat_timeout=args.heartbeat_timeout)

    query_args = dict()
    query_args['track'] = args.track_keywords

    if args.no_states:
        do_state(None, auth, query_args, stream_args, htmlparser)
    else:
        for state in STATES:
            do_state(state, auth, query_args, stream_args, htmlparser)
Exemplo n.º 7
0
    def localinit(self):
        OAUTH_FILENAME = self.connection.config.get("twitter", "oauth_cache")
        CONSUMER_KEY = self.connection.config.get("twitter", "consumer_key")
        CONSUMER_SECRET = self.connection.config.get(
            "twitter", "consumer_secret")

        try:
            if not os.path.exists(OAUTH_FILENAME):
                oauth_dance(
                    "Lampstand", CONSUMER_KEY, CONSUMER_SECRET,
                    OAUTH_FILENAME)

            self.oauth_token, self.oauth_token_secret = read_token_file(
                OAUTH_FILENAME)

            self.twitter = Twitter(
                auth=OAuth(
                    self.oauth_token,
                    self.oauth_token_secret,
                    CONSUMER_KEY,
                    CONSUMER_SECRET),
                secure=True,
                domain='api.twitter.com')
        except:
            pass
Exemplo n.º 8
0
def main():
    args = parse_arguments()

    if not all((args.token, args.token_secret, args.consumer_key,
                args.consumer_secret)):
        print(__doc__)
        return 2

    # When using twitter stream you must authorize.
    auth = OAuth(args.token, args.token_secret, args.consumer_key,
                 args.consumer_secret)
    if args.user_stream:
        stream = TwitterStream(auth=auth, domain='userstream.twitter.com')
        tweet_iter = stream.user()
    elif args.site_stream:
        stream = TwitterStream(auth=auth, domain='sitestream.twitter.com')
        tweet_iter = stream.site()
    else:
        stream = TwitterStream(auth=auth, timeout=60.0)
        tweet_iter = stream.statuses.sample()

    # Iterate over the sample stream.
    for tweet in tweet_iter:
        # You must test that your tweet has text. It might be a delete
        # or data message.
        if tweet.get('text'):
            printNicely(tweet['text'])
Exemplo n.º 9
0
    def postTweet(self):

        """Simple method to post a tweet"""

        oauth_token, oauth_secret = read_token_file(self.MY_TWITTER_CREDS)

        try:
            if self.check_graphical.checkState() == 2:
                t_up = Twitter(domain='upload.twitter.com',
                               auth=OAuth(oauth_token, oauth_secret,
                                          self.CONSUMER_KEY,
                                          self.CONSUMER_SECRET))

                with open(self.DATA_PATH + "/graphical_abstracts/{}".format(self.graphical), "rb") as image:
                    imagedata = image.read()

                id_img = t_up.media.upload(media=imagedata)["media_id_string"]
            else:
                self.l.debug("No image, check box not checked")
                id_img = None

        except AttributeError:
            self.l.debug("No image, no check box at all")
            id_img = None

        twitter = Twitter(auth=OAuth(oauth_token, oauth_secret,
                                     self.CONSUMER_KEY, self.CONSUMER_SECRET))

        text = self.text_tweet.toPlainText() + " #ChemBrows"

        if id_img is None:
            try:
                twitter.statuses.update(status=text)
            except Exception as e:
                QtWidgets.QMessageBox.critical(self, "Twitter error", "ChemBrows could not tweet that.\nYour tweet is probably too long: {} chara.".format(len(text)),
                                           QtWidgets.QMessageBox.Ok, defaultButton=QtWidgets.QMessageBox.Ok)
                self.l.error('postTweet: {}'.format(e), exc_info=True)
        else:
            try:
                twitter.statuses.update(status=text, media_ids=id_img)
            except Exception as e:
                QtWidgets.QMessageBox.critical(self, "Twitter error", "ChemBrows could not tweet that.\nYour tweet is probably too long: {} chara.".format(len(text)),
                                           QtWidgets.QMessageBox.Ok, defaultButton=QtWidgets.QMessageBox.Ok)
                self.l.error("postTweet: {}".format(e), exc_info=True)

        self.close()
Exemplo n.º 10
0
def twitter_global_status(content, live=False):
    """
    Twitter content via Civicbooms global feed
    
    In the future should maybe be linked to the Civicboom user, and all users could have twitter keys stored
    """
    #if isinstance(content, Content):
    #    content = content.to_dict('full')
    #content_dict = aggregation_dict(content, safe_strings=True)

    if live:
        link = tiny_url(content.__link__())
    else:
        link = 'http://tinyurl.com/xxxxxxx'

    title = strip_html_tags(content.title)
    content_preview = strip_html_tags(content.content)

    # Create Twitter message with tiny URL
    if len(title) > 70:
        title = truncate(title, length=70)
        content_preview = truncate(content_preview, length=30)
    else:
        content_preview = truncate(content_preview, length=100 - len(title))

    twitter_post = {}
    twitter_post['status'] = "%s: %s (%s)" % (title, content_preview, link)

    # Add location if avalable
    if content.location:
        twitter_post['lat'] = content.location.coords(Session)[1]
        twitter_post['long'] = content.location.coords(Session)[0]
        twitter_post['display_coordinates'] = True

    # Optional ideas
    # t['in_reply_to_status_id '] # If this is a reply to another tweet (could be good in the future if we can store master tweets)
    # t['trim_user'] = False? default?
    # t['place_id']  = "" #need reverse Geocode using the twitter api call geo/reverse_geocode
    # t['include_entities'] = True
    if live:

        twitter_post['status'] = twitter_post['status'].encode(
            'utf8', 'replace')
        t = Twitter(
            auth=OAuth(
                twitter_auth['oauth_token'],
                twitter_auth['oauth_token_secret'],
                twitter_auth['consumer_key'],
                twitter_auth['consumer_secret'],
            ),
            secure=True,
            api_version='1',
            domain='api.twitter.com',
        )
        t.statuses.update(**twitter_post)

    else:
        log.info('twitter_global aggregation disabled: \n%s' % twitter_post)
Exemplo n.º 11
0
def oauth_login(token_file=os.path.expanduser('~') + "/credentials.txt"):

    credentials = Credentials()
    credentials.read_from_file(token_file)

    return Twitter(auth=OAuth(credentials.OAUTH_TOKEN,
                              credentials.OAUTH_TOKEN_SECRET,
                              credentials.APP_KEY,
                              credentials.APP_SECRET))  #, retry=True)
Exemplo n.º 12
0
 def __init__(self):
     self.stream = TwitterStream(
         auth=OAuth(ACCESS_KEY,
                    ACCESS_SECRET,
                    CONSUMER_KEY,
                    CONSUMER_SECRET),
         api_version='1.1')
     self.twitter = Twitter(
         auth=OAuth(ACCESS_KEY,
                    ACCESS_SECRET,
                    CONSUMER_KEY,
                    CONSUMER_SECRET),
         api_version='1.1')
     self.tmblr = tumblpy.Tumblpy(app_key=TUMBLR_KEY,
                                  app_secret=TUMBLR_SECRET,
                                  oauth_token=TOKEN_KEY,
                                  oauth_token_secret=TOKEN_SECRET
                                  )
Exemplo n.º 13
0
def main():
    args = parse_arguments()

    # When using twitter stream you must authorize.
    auth = OAuth(args.token, args.token_secret, args.consumer_key,
                 args.consumer_secret)

    # These arguments are optional:
    stream_args = dict(timeout=args.timeout,
                       block=not args.no_block,
                       heartbeat_timeout=args.heartbeat_timeout)

    query_args = dict()
    if args.track_keywords:
        query_args['track'] = args.track_keywords

    if args.user_stream:
        stream = TwitterStream(auth=auth,
                               domain='userstream.twitter.com',
                               **stream_args)
        tweet_iter = stream.user(**query_args)
    elif args.site_stream:
        stream = TwitterStream(auth=auth,
                               domain='sitestream.twitter.com',
                               **stream_args)
        tweet_iter = stream.site(**query_args)
    else:
        stream = TwitterStream(auth=auth, **stream_args)
        if args.track_keywords:
            tweet_iter = stream.statuses.filter(**query_args)
        else:
            tweet_iter = stream.statuses.sample()

    # Connect to RethinkDB
    try:
        db.connect()
    except RqlDriverError:
        log.error('Couldn\'t connect to database.')

    # Iterate over the sample stream.
    for tweet in tweet_iter:
        # You must test that your tweet has text. It might be a delete or data message.
        if tweet is None:
            log.error('None')
        elif tweet is Timeout:
            log.error('Timeout')
        elif tweet is HeartbeatTimeout:
            log.error('Heartbeat Timeout')
        elif tweet is Hangup:
            log.error('Hangup')
        elif tweet.get('text'):
            tweet[
                'stream_id'] = '1234567890'  # TODO: add that stream_id gets passed to open_stream.py
            db.insert(DATABASE, TWEETS_TABLE, tweet)
            printNicely(tweet['text'])
        else:
            log.error('Some data ' + str(tweet))
Exemplo n.º 14
0
def get_auth():
    """
    Twitter has some bizarre requirements about how to authorize an "app" to
    use its API.

    The user of the app has to log in to get a secret token. That's fine. But
    the app itself has its own "consumer secret" token. The app has to know it,
    and the user of the app has to not know it.

    This is, of course, impossible. It's equivalent to DRM. Your computer can't
    *really* make use of secret information while hiding the same information
    from you.

    The threat appears to be that, if you have this super-sekrit token, you can
    impersonate the app while doing something different. Well, of course you
    can do that, because you *have the source code* and you can change it to do
    what you want. You still have to log in as a particular user who has a
    token that's actually secret, you know.

    Even developers of closed-source applications that use the Twitter API are
    unsure what to do, for good reason. These "secrets" are not secret in any
    cryptographic sense. A bit of Googling shows that the secret tokens for
    every popular Twitter app are already posted on the Web.

    Twitter wants us to pretend this string can be kept secret, and hide this
    secret behind a fig leaf like everybody else does. So that's what we've
    done.
    """

    from twitter.oauth import OAuth
    from twitter import oauth_dance, read_token_file

    def unhide(secret):
        """
        Do something mysterious and exactly as secure as every other Twitter
        app.
        """
        return ''.join([chr(ord(c) - 0x2800) for c in secret])

    fig_leaf = '⠴⡹⠹⡩⠶⠴⡶⡅⡂⡩⡅⠳⡏⡉⡈⠰⠰⡹⡥⡶⡈⡐⡍⡂⡫⡍⡗⡬⡒⡧⡶⡣⡰⡄⡧⡸⡑⡣⠵⡓⠶⠴⡁'
    consumer_key = 'OFhyNd2Zt4Ba6gJGJXfbsw'

    if os.path.exists(AUTH_TOKEN_PATH):
        token, token_secret = read_token_file(AUTH_TOKEN_PATH)
    else:
        authdir = os.path.dirname(AUTH_TOKEN_PATH)
        if not os.path.exists(authdir):
            os.makedirs(authdir)
        token, token_secret = oauth_dance(app_name='ftfy-tester',
                                          consumer_key=consumer_key,
                                          consumer_secret=unhide(fig_leaf),
                                          token_filename=AUTH_TOKEN_PATH)

    return OAuth(token=token,
                 token_secret=token_secret,
                 consumer_key=consumer_key,
                 consumer_secret=unhide(fig_leaf))
Exemplo n.º 15
0
def load_auth(path, raw=False):
    if not os.path.exists(path):
        raise OSError("path not found")
    with open(path) as f:
        args = f.read().splitlines()
        if raw:
            return args
        else:
            return OAuth(*args)
Exemplo n.º 16
0
def main():
    oauth_filename = os.environ.get('HOME', '') + os.sep + '.twitter_oauth'
    oauth_filename = os.path.expanduser(oauth_filename)

    oauth_token, oauth_token_secret = read_token_file(oauth_filename)
    auth = OAuth(oauth_token, oauth_token_secret, CONSUMER_KEY,
                 CONSUMER_SECRET)
    twitter = Twitter(auth=auth,
                      secure=True,
                      api_version='1',
                      domain='api.twitter.com')

    try:
        tweets = pickle.load(open('tweets.pickle'))
    except:
        tweets = []
    print "Horay! I've got %s tweets from the file!" % len(tweets)

    # используем nltk
    featuresets = [(get_features(tweet), tweet['good']) for tweet in tweets]
    total = len(featuresets)
    train_set, test_set = featuresets[total / 2:], featuresets[:total / 2]

    classifier = nltk.NaiveBayesClassifier.train(train_set)
    #tree_classifier = nltk.DecisionTreeClassifier.train(train_set)
    print nltk.classify.accuracy(classifier, test_set)
    classifier.show_most_informative_features(10)
    #print nltk.classify.accuracy(tree_classifier, test_set)

    if MILK:
        # используем milk
        learner = milk.defaultclassifier()
        get_milk_keys(get_features(tweet) for tweet in tweets)
        features = [get_milk_features(tweet) for tweet in tweets]
        labels = [tweet['good'] for tweet in tweets]
        model = learner.train(features, labels)

    ids = set(tweet['id'] for tweet in tweets)

    tweet_iter = twitter.statuses.friends_timeline(count=COUNT)
    for tweet in tweet_iter:
        if tweet.get('text') and tweet['id'] not in ids:
            print '%s: %s' % (tweet['user']['name'], tweet['text'])
            print '[nltk] I think, this tweet is interesting with probability', classifier.prob_classify(
                get_features(tweet)).prob(True)
            if MILK:
                print '[milk] I think, this tweet is interesting with probability', model.apply(
                    get_milk_features(tweet))
            good = raw_input('Interesting or not?\n(y/n): ') in ('y', 'Y', 'G',
                                                                 'g')
            tweet['good'] = good
            tweets.append(tweet)

    pickle.dump(tweets, open('tweets.pickle', 'w'))
    def fetchTwitterData(self):

        ACCESS_TOKEN    = '4493656032-zhJ6hagHhaLy3TjQKb6tSnuO1imDlcOOYlcletE'
        ACCESS_SECRET   = 'cwjjJnqsSQkll4qdPvNMYnQgU3lkOBeo4MJRbuGujAV3u'
        CONSUMER_KEY    = 'O1MENwW8o14QlD4vFxeJSgAld'
        CONSUMER_SECRET = 'lUkCrhP2ct6j7pATtW7DNbAMJqvxoAulSC6zEQ3d2IM1dDDanR'


        oauth = OAuth(ACCESS_TOKEN, ACCESS_SECRET, CONSUMER_KEY, CONSUMER_SECRET)


        twitter_stream = TwitterStream(auth=oauth)

        read_tags = ReadTwitterTags()
        #tags = read_tags.read_twitter_tags("../twitter_reckoner/twitter_tag_filters_stars.py")
        tags = ['rajini']
        #data_writer = DataWriter("/home/dharshekthvel/.tweets")
        data_writer = DataWriter("/root/data/rajini.tweets")

        for each_tag in tags:

            iterator = twitter_stream.statuses.filter(track=each_tag, language="en")

            tweet_count = 999999
            for tweet in iterator:

                tweet_count -= 1

                complete_tweet = json.dumps(tweet)
                processed_tweet = json.loads(complete_tweet)


                import datetime
                now = datetime.datetime.now()

                if 'text' in processed_tweet.keys():
                    twitter_data = TwitterData(now.strftime("%Y-%m-%d-%H-%M-%S"), processed_tweet['text'].lower(),  "NA", "NA", "NA", "NA", "NOT_ANALYZED")




                clean_tweet = CleanTweets()
                rt_removed_tweet = clean_tweet.remove_RT_from_tweet(twitter_data.tweeted_text)
                cleaned_tweet = clean_tweet.remove_unnecessary_info(rt_removed_tweet)

                #redis_data = MarchDataToRedis()
                #redis_data.saveRedisData(twitter_data)

                print ("T - " + cleaned_tweet)
                data_writer.appendDataToFile(cleaned_tweet)

                if tweet_count <= 0:
                        break
Exemplo n.º 18
0
def authen():
    """
    Authenticate with Twitter OAuth
    """
    # When using rainbow stream you must authorize.
    twitter_credential = os.environ.get(
        'HOME', os.environ.get('USERPROFILE', '')) + os.sep + '.rainbow_oauth'
    if not os.path.exists(twitter_credential):
        oauth_dance("Rainbow Stream", CONSUMER_KEY, CONSUMER_SECRET,
                    twitter_credential)
    oauth_token, oauth_token_secret = read_token_file(twitter_credential)
    return OAuth(oauth_token, oauth_token_secret, CONSUMER_KEY,
                 CONSUMER_SECRET)
Exemplo n.º 19
0
    def __init__(self, redis_url=None, quotation_url=None):
        OAUTH_TOKEN = os.environ.get('TWITTER_OAUTH_TOKEN')
        OAUTH_SECRET = os.environ.get('TWITTER_OAUTH_SECRET')
        CONSUMER_KEY = os.environ.get('TWITTER_CONSUMER_KEY')
        CONSUMER_SECRET = os.environ.get('TWITTER_CONSUMER_SECRET')
        if not quotation_url:
            quotation_url = os.environ.get('QUOTATION_URL')
        self.BASE_URL = quotation_url
        self.DUPLICATE_CODE = 187

        self.twitter = Twitter(auth=OAuth(OAUTH_TOKEN, OAUTH_SECRET,
                                          CONSUMER_KEY, CONSUMER_SECRET))
        self.redis = get_redis(redis_url)
Exemplo n.º 20
0
def twitter_authorize():
    twitter = Twitter(
        auth=OAuth(session['oauth_token'], session['oauth_token_secret'],
                   app.config['TWITTER_CONSUMER_KEY'],
                   app.config['TWITTER_CONSUMER_SECRET']
                   ),
        format='', api_version=None)
    oauth_token, oauth_token_secret = parse_oauth_tokens(
        twitter.oauth.access_token(
            oauth_verifier=request.args.get('oauth_verifier'))
    )
    if oauth_token and oauth_token_secret:
        t = Twitter(
            auth=OAuth(oauth_token,
                       oauth_token_secret,
                       app.config['TWITTER_CONSUMER_KEY'],
                       app.config['TWITTER_CONSUMER_SECRET'])
        )
        user_info = t.account.settings()
        user = TwitterAccount.query\
            .filter_by(username=user_info['screen_name'])\
            .first()
        if user:
            user.oauth_token = oauth_token
            user.oauth_secret = oauth_token_secret
            db.session.commit()
            return '@' + user_info['screen_name'] + ' was updated.'
        else:
            new_user = TwitterAccount(
                username=user_info['screen_name'],
                oauth_token=oauth_token,
                oauth_secret=oauth_token_secret
            )
            db.session.add(new_user)
            db.session.commit()
            return '@' + user_info['screen_name'] + ' was added.'
    else:
        return 'Twitter Account was not added.'
Exemplo n.º 21
0
def post(honeypycfg, service, clientip):

    ck = honeypycfg.get('twitter', 'consumerkey')
    cs = honeypycfg.get('twitter', 'consumersecret')
    ot = honeypycfg.get('twitter', 'oauthtoken')
    os = honeypycfg.get('twitter', 'oauthsecret')

    t = Twitter(auth=OAuth(ot, os, ck, cs))
    nodename = honeypycfg.get('honeypy', 'nodename')

    try:
        t.statuses.update(status=nodename + ': #' + service + ' Possible '  + service + ' attack from ' + clientip + ' https://riskdiscovery.com/honeydb/host/' + clientip)
    except Exception, err:
        log.msg('Error posting to Twitter: %s' % err)
Exemplo n.º 22
0
def get_twitter(need_auth=False):
    if need_auth:
        oauth_token, oauth_token_secret = read_token_file(OAUTH_FILENAME)
        auth = OAuth(oauth_token, oauth_token_secret, CONSUMER_KEY,
                     CONSUMER_SECRET)
    else:
        auth = None

    twitter = Twitter(auth=auth,
                      secure=True,
                      api_version='1',
                      domain='api.twitter.com')

    return twitter
Exemplo n.º 23
0
def twitter_login():
    twitter = Twitter(
        auth=OAuth('', '',
                   app.config['TWITTER_CONSUMER_KEY'],
                   app.config['TWITTER_CONSUMER_SECRET']
                   ),
        format='', api_version=None)
    session['oauth_token'], session['oauth_token_secret'] = parse_oauth_tokens(
        twitter.oauth.request_token(
            oauth_callback=app.config['TWITTER_CALLBACK'])
    )
    oauth_url = ('https://api.twitter.com/oauth/authorize?oauth_token=' +
                 session['oauth_token'])
    return '<a href="' + oauth_url + '">Login with Twitter</a>'
Exemplo n.º 24
0
def main():
    args = parse_arguments()

    # When using twitter stream you must authorize.
    auth = OAuth(args.token, args.token_secret, args.consumer_key,
                 args.consumer_secret)

    # These arguments are optional:
    stream_args = dict(timeout=args.timeout,
                       block=not args.no_block,
                       heartbeat_timeout=args.heartbeat_timeout)

    query_args = dict()
    if args.track_keywords:
        query_args['track'] = args.track_keywords

    if args.user_stream:
        stream = TwitterStream(auth=auth,
                               domain='userstream.twitter.com',
                               **stream_args)
        tweet_iter = stream.user(**query_args)
    elif args.site_stream:
        stream = TwitterStream(auth=auth,
                               domain='sitestream.twitter.com',
                               **stream_args)
        tweet_iter = stream.site(**query_args)
    else:
        stream = TwitterStream(auth=auth, **stream_args)
        if args.track_keywords:
            tweet_iter = stream.statuses.filter(**query_args)
        else:
            tweet_iter = stream.statuses.sample()

    # Iterate over the sample stream.
    for tweet in tweet_iter:
        # You must test that your tweet has text. It might be a delete
        # or data message.
        if tweet is None:
            printNicely("-- None --")
        elif tweet is Timeout:
            printNicely("-- Timeout --")
        elif tweet is HeartbeatTimeout:
            printNicely("-- Heartbeat Timeout --")
        elif tweet is Hangup:
            printNicely("-- Hangup --")
        elif tweet.get('text'):
            printNicely(tweet['text'])
        else:
            printNicely("-- Some data: " + str(tweet))
Exemplo n.º 25
0
def get_auth():
    config_file_name = EXPORT_PATH + 'credentials - f1.ini'
    Config = ConfigParser.ConfigParser()
    Config.read(config_file_name)

    print Config.get("credentials", "consumer_key")

    auth = OAuth(
        consumer_key = Config.get("credentials", "consumer_key"),
        consumer_secret = Config.get("credentials", "consumer_secret"),
        token = Config.get("credentials", "token"),
        token_secret = Config.get("credentials", "token_secret")
    )

    return auth
Exemplo n.º 26
0
 def api(self):
     auth = OAuth(token=self.oauth_token,
                  token_secret=self.oauth_token_secret,
                  consumer_key=self.config['consumer_key'],
                  consumer_secret=self.config['consumer_secret'])
     kwargs = {'auth': auth}
     try:
         kwargs['domain'] = self.config['host']
     except KeyError:
         pass
     try:
         kwargs['secure'] = asbool(self.config['secure'])
     except KeyError:
         pass
     return Twitter(**kwargs)
Exemplo n.º 27
0
def main():
    # return test()
    try:
        import setproctitle
        setproctitle.setproctitle('zmq-publisher')
    except ImportError:
        print("missing module: setproctitle")

    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('auth', type=str, help="twitter auth token file")
    parser.add_argument('-n',
                        '--hostname',
                        type=str,
                        help="publisher hostname")
    parser.add_argument('-p', '--port', type=str, help="publisher port")
    parser.add_argument('-a',
                        '--all-replies',
                        help='include non-follower replies',
                        action="store_true")
    parser.add_argument('-u',
                        '--user-only',
                        help="don't include items from followed accounts",
                        action="store_true")
    args = parser.parse_args()

    funcargs = dict()
    iter_kwargs = dict()
    if args.hostname:
        funcargs['hostname'] = args.hostname
    if args.port:
        funcargs['port'] = args.port
    if args.all_replies:
        iter_kwargs['replies'] = 'all'
    if args.user_only:
        iter_kwargs['with'] = 'user'

    creds = load_auth(args.auth, raw=True)
    # the OAuth object in the twitter module has different positional arguments
    # then the requests OAuth module used in my own streaming implementation
    auth = OAuth(creds[2], creds[3], creds[0], creds[1])
    iterator = functools.partial(user_stream_iter, auth)
    publisher = StreamPublisher(iterator=iterator,
                                iter_kwargs=iter_kwargs,
                                error_handler=twitter_error_handler,
                                **funcargs)

    return publisher.run()
Exemplo n.º 28
0
def main():
    try:
        filename = sys.argv[1]
    except:
        filename = 'tweets.json'
    out = open(filename, 'a')

    try:
        con = import_module(sys.argv[2])
    except:
        con = import_module('config')
    auth = OAuth(con.TOKEN, con.TOKEN_SECRET, con.CONSUMER_KEY,
                 con.CONSUMER_SECRET)
    '''stream_args = dict(
        timeout=args.timeout,
        block=not args.no_block,
        heartbeat_timeout=args.heartbeat_timeout)'''

    stream_args = {}
    query_args = {"locations": con.COORDS}
    stream = TwitterStream(auth=auth)
    tweet_iter = stream.statuses.filter(**query_args)

    for tweet in tweet_iter:
        if tweet is None:
            printNicely("-- None --")
        elif tweet is Timeout:
            printNicely("-- Timeout --")
        elif tweet is HeartbeatTimeout:
            printNicely("-- Heartbeat Timeout --")
        elif tweet is Hangup:
            printNicely("-- Hangup --")
        elif tweet.get('text'):
            tweetJson = {
                "id": tweet['id_str'],
                "time": tweet['created_at'],
                "place": tweet['place'],
                "coordinates": tweet['coordinates'],
                "retweets": tweet['retweet_count'],
                "text": tweet['text'],
                "hashtags": tweet['entities']['hashtags']
            }
            out.write(json.dumps(tweetJson) + "\n")
        else:
            printNicely("-- Some data: " + str(tweet))
Exemplo n.º 29
0
def twitterAuth(config, OAUTH_TWITTER):
    HOME = os.environ.get('HOME', '')
    twitter_user = config.get("twitter", "username")
    OAUTH_FILENAME = HOME + os.sep + '.lifesteam_oauth_' + twitter_user
    CONSUMER_KEY = config.get("twitter", "consumer_key")
    CONSUMER_SECRET = config.get("twitter", "consumer_secret")

    if not os.path.exists(OAUTH_FILENAME):
        oauth_dance("Lifestream", CONSUMER_KEY, CONSUMER_SECRET,
                    OAUTH_FILENAME)

    oauth_token, oauth_token_secret = read_token_file(OAUTH_FILENAME)

    return Twitter(auth=OAuth(oauth_token, oauth_token_secret, CONSUMER_KEY,
                              CONSUMER_SECRET),
                   secure=True,
                   api_version='1.1',
                   domain='api.twitter.com')
Exemplo n.º 30
0
def main():
    args = parse_arguments()

    # When using twitter stream you must authorize.
    auth = OAuth(args.token, args.token_secret, args.consumer_key,
                 args.consumer_secret)

    # These arguments are optional:
    stream_args = dict(timeout=args.timeout,
                       block=not args.no_block,
                       heartbeat_timeout=args.heartbeat_timeout)

    query_args = dict()
    if args.track_keywords:
        query_args['track'] = args.track_keywords

    if args.user_stream:
        stream = TwitterStream(auth=auth,
                               domain='userstream.twitter.com',
                               **stream_args)
        tweet_iter = stream.user(**query_args)
    elif args.site_stream:
        stream = TwitterStream(auth=auth,
                               domain='sitestream.twitter.com',
                               **stream_args)
        tweet_iter = stream.site(**query_args)
    else:
        stream = TwitterStream(auth=auth, **stream_args)
        if args.track_keywords:
            tweet_iter = stream.statuses.filter(**query_args)
        else:
            tweet_iter = stream.statuses.sample()

    i = 0
    # Iterate over the sample stream.
    for tweet in tweet_iter:
        # You must test that your tweet has text. It might be a delete
        # or data message.
        if i > 10:
            exit(0)
        if tweet.get('text') and tweet.get('lang') == 'en':
            #printNicely("@" + tweet['user']['screen_name'] + ' [' + str(tweet['retweet_count']) + ']')
            printNicely(tweet['text'])
            i += 1