Trending Machine Learning topics for Alexa I've created a small Flash Briefing Alexa skill that reports the top 5 trending topics in Machine Learning from the My Bridge service.

The feed for the Flash Briefing skill can be pointed to an external endpoint, but the response has to be in a certain format.

Sample:

[    
    {
        mainText: "Posted by Hui Fang, Software Engineer, Machine Perception Machine learning (ML) excels in many areas with well defined goals. Tasks where ...",
        redirectionUrl: "https://research.googleblog.com/2017/07/using-deep-learning-to-create.html",
        titleText: "Using Deep Learning to Create Professional-Level Photographs",
        uid: "200561",
        updateDate: "2017-07-13T20:11:59.000000Z"
    },
    ...
]


To expose an endpoint I've created a small Flask app (deployed to Heroku, runs on gunicorn), that interacts with a My Bridge API via the requests module:

from datetime import datetime
import logging

from flask import Flask, jsonify
import requests


ML_ID_URL = "https://mybridge-backend.herokuapp.com/api/v1/skills/mapping/machine-learning"
ML_TOP_URL = "https://mybridge-backend.herokuapp.com/api/v1/knowledge/skills/{id}?offset=0&amp;limit=5&amp;sort="

logger = logging.getLogger()
logger.setLevel(logging.INFO)

app = Flask(__name__)


@app.route("/")
def main():
    with requests.Session() as session:
        session.headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"}

        # get topic ID
        response = session.get(ML_ID_URL)
        data = response.json()

        ml_topic_id = data["body"]["id"]

        # get top 5 trending topics
        response = session.get(ML_TOP_URL.format(id=ml_topic_id))
        data = response.json()

        return jsonify([
            {
                "uid": str(topic["id"]),
                "updateDate": datetime.strptime(topic["reg_date"], "%Y-%m-%d %H:%M:%S.%f").strftime("%Y-%m-%dT%H:%M:%S.%fZ"),  # "2016-04-10T00:00:00.0Z"
                "titleText": topic["title"],
                "mainText": topic["excerpt"],
                "redirectionUrl": topic["externalURL"]
            }
            for topic in data['body']
        ])


if __name__ == '__main__':
    app.run(debug=True, use_reloader=True)


What would you improve code style and code organization wise?
Structure

Nothing! The code seems fairly straightforward and clear, and I was able to understand what you’re doing even if I didn't study flask too much. So I don’t think there’s anything wrong with the structure.

Code style

One small nitpick: some of your lines are too long (>79 characters). From PEP8:


  Limit all lines to a maximum of 79 characters.


Anyway, as a personal preference, I like to stick to PyCharm's recommendation and keep it &lt; 120. But even with that in mind, you still have one line which doesn't fit.

Do this instead (for &lt;120):

session.headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/59.0.3071.115 Safari/537.36"
}


Or:

session.headers = {
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5)" 
                      "AppleWebKit/537.36 (KHTML, like Gecko) "
                      "Chrome/59.0.3071.115 Safari/537.36"
    }


Anyway, that's really subjective and it didn't bother me that you kept the entire User-Agent on the same line.

The same observation is also applicable for updateDate.



Flask-RESTful API

Apparently, you're trying to create some sort of RESTful API. Luckily, flask let's you do this by installing their Flask-RESTful API which you can do by running pip install flask-restful.

Flask-RESTful is a powerful extension for Flask that was designed to help you build complex RESTful APIs. It follows similar design principles to Flask so it is really easy (IMO) to pick up. Now, you might wonder why you should use this: well, your code was quite straightforward to implement but as the API grows in complexity it is easy to see this code becoming very complicated, very quickly!