def trends_to_string_util(trendsMap): webpage = '' for location, trends in trendsMap.items(): header = ''.join([location_from_woeid(location), ": "]) body = '\n'.join(map(str, trends)) webpage = header + body return webpage
def images(location): """ Renders image page with title of python chart that is non-interactable Example: http://0.0.0.0:5000/images/2282863 :param location: :return: """ place = location_from_woeid(location) return render_template("images.html", woeid=location, place=place)
def visualize_trends(trends: dict): trends_logger.info("Start: visualizing trends, trends length: {}".format(len(trends))) trends_logger.debug(trends) figures = [] for woeid, trend in trends.items(): graph = _visualize_trend(location_from_woeid(woeid), trend) figures.append(graph) trends_logger.info("End: visualizing trends, trends length: {}".format(len(trends))) return figures
def graph_labels_by_woeid(woeid: int, limit=TREND_HARD_LIMIT): """ :param woeid: location geo id :param limit: limit on top trending topics (50 max) :return: """ trend = twitter_trends._trend_for_one_location(woeid) topics = [x.name for idx, x in enumerate(trend[:limit])] # label volumes = [x.volume for idx, x in enumerate(trend[:limit])] title = location_from_woeid(woeid) return topics, volumes, title
def _trend_for_one_location(woeid): """ returns list of Trends for given location :param woeid: geo location such as 1 fot Worldwide :return: """ trends_logger.info("Twitter API call: trends_place api call for {}".format( location_from_woeid(woeid))) india_trends = trends_place(woeid) trends = json.loads(json.dumps(india_trends)) fields = _parse_trends(trends[0]) return fields
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')