Question

What`s wrong with the method? 'stream' is the method name I declared for it to start the streamer and it works. However when I wrote this method to disconnect the streamer, it have error saying that 'str' object has no attribute 'disconnect'. Can anyone give me example or tell me what did I missed out?

def twitter_stop(request):
    stream = request.session['stream']

    stream.disconnect()
     return HttpResponse(request)

updated: This is my start method which is working:

def twitter_start(request):
     stream = MyStreamer(settings.CONSUMER_KEY, settings.CONSUMER_SECRET,
                settings.ACCESS_TOKEN_KEY, settings.ACCESS_TOKEN_SECRET)
     # get the stream object from the SESSION
      request.session['stream'] = stream
     # get the keywords from the models
        keywords = Keys.objects.all()
        stream.statuses.filter(track=keywords)
         return HttpResponse(request)
Was it helpful?

Solution

The 2 request objects are different ; the stream you assign in twitter_start in request.session is lost after the execution of the function. Use a global variable to store the stream, eg:

global STREAM

def twitter_start(request):
    global STREAM
    STREAM = MyStreamer(settings.CONSUMER_KEY, settings.CONSUMER_SECRET,
                        settings.ACCESS_TOKEN_KEY, settings.ACCESS_TOKEN_SECRET)

    # get the keywords from the models
    keywords = Keys.objects.all()
    STREAM.statuses.filter(track=keywords)
    return HttpResponse(request)

def twitter_stop(request):
    STREAM.disconnect()
    return HttpResponse(request)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top