def visualize(location): """ This reders image/png for a given woeid Example: http://0.0.0.0:5000/visualize/2282863 :param location: :return: """ trends_logger.info("visualizing location: " + location_from_woeid(location)) trends = twitter_trends.trends_by_location(woeids=[location]) fig = visualize_trends(trends) fig = fig[0] img = io.BytesIO() fig.savefig(img) img.seek(0) return send_file(img, mimetype='image/png')
def connect(): """ set connection and cursor :return: """ trends_logger.info("Connecting to maria db") conn = mysql.connector.connect( host=DB_CONFIG['Host'], user=DB_CONFIG['Username'], password=DB_CONFIG['Password'] ) cur = conn.cursor() if conn is not None: cur.execute("Use " + DB_CONFIG['Database']) trends_logger.info("connection successful, using database {}".format(DB_CONFIG['Database'])) return conn, cur
def setup(): logging.basicConfig(level=logging.INFO) trends_logger.info("Initializing application...") populate_location_map()
def close_connection(): if db_connection.is_connected(): cursor.close() db_connection.commit() db_connection.close() trends_logger.info("MySQL connection is closed")
def create_table_places(): with app.open_resource('data/database/createTableLocation.sql') as f: result = cursor.execute(f) trends_logger.info("Places_List created successfully. {} ".format(result))
def show_tables(): trends_logger.info("Show tables ") cursor.execute("SHOW TABLES") for table in cursor: print(table)
def close(cls): trends_logger.info("Closing database connection ...") if cls.connection is not None: cls.connection.close()
def update_cache(key, value): trends_logger.info("MISS: updating cache for key: {}, size: {}".format( key, size())) trends_cache[key] = value
def invalidate(): trends_logger.info("DANGER: invalidated cache manually, size: {}".format( size())) trends_cache.clear()
def get_cache(key): if key in trends_cache.keys(): trends_logger.info("HIT: found in cache for key: {}, size: {}".format( key, size())) return trends_cache[key] return None
def _visualize_trend(location, trend): trends_logger.info("Visualizing {}".format(location)) topic = [x.name for idx, x in enumerate(trend[:20])] volume = [x.volume for idx, x in enumerate(trend[:20])] figure = _get_graph_figure(labels=['volume', 'trends'], values=[topic, volume], title=location) return figure