Tweepy streamer stopping after few seconds

python twitter tweepy

125 просмотра

1 ответ

591 Репутация автора

Hi !

I am experiencing some issue about tweepy library for Python. The first time I launched the below script, everything perfectly worked, and the second time... the script stop unexpectedly.

I did not found anything about this behavior, the Listener is stopping after few seconds, and I do not have any error code or something.

There is the simple code:

import tweepy
import sys
import json
from textwrap import TextWrapper
from datetime import datetime
from elasticsearch import Elasticsearch

consumer_key = "hidden"
consumer_secret = "hidden"
access_token = "hidden"
access_token_secret = "hidden"

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
ES_HOST = {"host" : "localhost", "port" : 9200}

es = Elasticsearch(hosts = [ES_HOST])

class StreamListener(tweepy.StreamListener):
        print('Starting StreamListener')
        status_wrapper = TextWrapper(width=60, initial_indent='    ', subsequent_indent='    ')

        def on_status(self, status):
                try:
                        print 'n%s %s' % (status.author.screen_name, status.created_at)
                        json_data = status._json
                        #print json_data['text']

                        es.create(index="idx_twp",
                                doc_type="twitter_twp_nintendo",
                                body=json_data
                                )

                except Exception, e:
                        print e
                        pass

print('Starting Receiving')

streamer = tweepy.Stream(auth=auth, listener=StreamListener(), timeout=3000000000)

#Fill with your own Keywords bellow
terms = ['nintendo']

streamer.filter(None,terms)
#streamer.userstream(None)

print ('Ending program')

And then there is the ouput (only 2 seconds);

[root@localhost ~]# python projects/m/twitter/twitter_logs.py
Starting StreamListener
Starting Receiving
Ending program

I am using Python 2.7.5

Any ideas about ?

Автор: toshiro92 Источник Размещён: 18.07.2016 08:14

Ответы (1)


0 плюса

591 Репутация автора

Решение

Hi !

I solved this weird issue by changing my Python version to 3.5 via virtualenv. For now, it works well.

This could was due to the python version, anyway if someone have this, I just recommend to use virtualenv to test another Python version, and see what happens.

FYI : I already opened issue #759 into the github project.

Автор: toshiro92 Размещён: 15.08.2016 05:25
Вопросы из категории :
32x32