Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to stream twitter metadata to a client browser from server in Python/flask
    primarykey
    data
    text
    <p>I'm trying to return to the client a stream of twitter metadata which the client enters in a form filtered by tweepy's 'track'. </p> <p>The problem I'm having is that nothing is returned back to the browser, eventhough the server receives it and processes the request. This is my code so far:</p> <p>The main.py:</p> <pre><code>import flask from flask.views import MethodView from tweetStreamsRT import StreamerRt app = flask.Flask(__name__) app.secret_key = "asecret" class View(MethodView): def get(self): return flask.render_template('index.html') def post(self): results = StreamerRt().filter(track = (flask.request.form['event'])) flask.flash(results) return self.get() app.add_url_rule('/', view_func = View.as_view('index'), methods=['GET', 'POST']) app.debug = True app.run() </code></pre> <p>The class connecting to the Twitter Streaming API:</p> <pre><code>import sys import tweepy import json import time #from textwrap import TextWrapper import pymongo CONSUMER_KEY = '' CONSUMER_SECRET = '' ACCESS_TOKEN = '' ACCESS_TOKEN_SECRET = '' auth = tweepy.OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET) auth.set_access_token(ACCESS_TOKEN,ACCESS_TOKEN_SECRET) connection = pymongo.Connection() db = connection.twitterStream class Listener(tweepy.StreamListener): def on_status(self, status): message = {} if status.author.lang == 'en': try: message = { 'Author': status.author.screen_name, 'Author_id': status.id, 'Time_normal': str(status.created_at), 'geo_enabled': status.author.geo_enabled, 'Geo': status.geo, 'Place' : status.place, 'Coordinates': status.coordinates} db.tweetStream.insert(message) except Exception, e: print &gt;&gt; sys.stderr, 'Encountered Exception:', e pass def on_error(self, status_code): waitPeriod = 60 if status_code == 420: print &gt;&gt; sys.stderr, 'Encountered a %i Error, will retry in %i minutes' % \ (status_code, waitPeriod/60) time.sleep(waitPeriod) waitPeriod *= 2 return waitPeriod else: print &gt;&gt; sys.stderr, 'Encountered error with status code:', status_code return True def on_timeout(self): print &gt;&gt; sys.stderr, 'Timeout...' return True def StreamerRt(): return tweepy.streaming.Stream(auth, Listener(), timeout=60) </code></pre> <p>My html file which inherits from a base and forms html file:</p> <pre><code>{% extends "base.html" %} {% import "forms.html" as forms %} {% block page_header %} &lt;div class="page-header"&gt; &lt;h1&gt;Welcome&lt;/h1&gt; &lt;/div&gt; {% endblock %} {% block content %} &lt;h2&gt;Enter the Event you would like to follow&lt;/h2&gt; &lt;form action="/" method="post"&gt; &lt;input type="text" name="event" /&gt; &lt;input type="submit" value="Submit Query" /&gt; &lt;/form&gt; {% with messages = get_flashed_messages() %} {% if messages %} Results: &lt;pre&gt; {% for message in messages %} {{ message }} {% endfor %} &lt;/pre&gt; {% endif %} {% endwith %} {% endblock %} </code></pre> <p>It seems that I can't return any streaming data to the client.</p> <p>How could you return certain metadata from Twitter Streaming API to the browser, at the same time that it is written into the DB.</p> <p>Thanks</p> <p>Furthermore the function StreamerRt doesnt filter by track as input from the client, but seems to return the whole public timeline.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload