#!/usr/bin/python
# -*- encoding: utf-8 -*-

import json
import http

from flask import Flask, jsonify, Response
import requests

ROOT_URL = "https://medium.com/"
EXCEPE_STR = "])}while(1);</x>"
app = Flask(__name__)


@app.route("/")
def index():
    return "Hello!!"


@app.route("/<username>", methods=["GET"])
def get_user_posts(username, count=10):
    payload = {"count": count}
    accept_header = {"Accept": "application/json"}
    req = requests.get(ROOT_URL + "@{0}/latest".format(username),
                       headers=accept_header,
                       params=payload)
    if req.status_code == requests.codes.ok:
        req_text = req.text.replace(EXCEPE_STR, "").strip()
        response_dict = json.loads(req_text)
        post_dict = response_dict.get("payload").get("references").get("Post")
        return jsonify(post_dict)
    else:
        return Response(status=req.status_code)


@app.route("/posts/<post_id>", methods=["GET"])
def get_post(post_id):
    pass


@app.route("/top")
def get_top_posts():
    pass


@app.route("/tags/<tag_name>", methods=["GET"])
def get_posts_by_tag(tag_name):
    pass


if __name__ == "__main__":
    get_user_posts("enginebai")

@@ -53,20 +53,8 @@
 json
-%0Aimport http
 %0A%0Afr
@@ -145,25 +145,32 @@
 .com/%22%0AE
-XCEPE_STR
+SCAPE_CHARACTERS
  = %22%5D)%7Dw
@@ -183,16 +183,64 @@
 );%3C/x%3E%22%0A
+ACCEPT_HEADER = %7B%22Accept%22: %22application/json%22%7D%0A%0A
 app = Fl
@@ -398,108 +398,291 @@
     
-payload = %7B%22count%22: count%7D%0A    accept_header = %7B%22Accept%22: %22application/json%22%7D%0A    req = 
+return send_request(ROOT_URL + %22@%7B0%7D/latest%22.format(username), param=%7B%22count%22: count%7D)%0A%0A%0A@app.route(%22/top%22)%0Adef get_top_posts():%0A    return send_request(ROOT_URL + %22browse/top%22)%0A%0A%0A@app.route(%22/tags/%3Ctag_name%3E%22, methods=%5B%22GET%22%5D)%0Adef get_posts_by_tag(tag_name):%0A    return send_
 request
-s.get
 (ROO
@@ -682,35 +682,40 @@
 est(ROOT_URL + %22
-@%7B0
+tag/%7Btag
 %7D/latest%22.format
@@ -719,87 +719,209 @@
 mat(
-username),%0A                       headers=accept_header,%0A                      
+tag=tag_name))%0A%0A%0A@app.route(%22/posts/%3Cpost_id%3E%22, methods=%5B%22GET%22%5D)%0Adef get_post(post_id):%0A    pass%0A%0A%0Adef send_request(url, headers=ACCEPT_HEADER, param=None):%0A    req = requests.get(url, headers=headers,
  par
@@ -926,21 +926,19 @@
 arams=pa
-yload
+ram
 )%0A    if
@@ -976,24 +976,185 @@
 des.ok:%0A    
+    post_dict = parse_post(req.text)%0A        return jsonify(post_dict)%0A    else:%0A        return Response(status=req.status_code)%0A%0A%0Adef parse_post(request_text):%0A
     req_text
@@ -1159,17 +1159,21 @@
 xt = req
-.
+uest_
 text.rep
@@ -1182,17 +1182,24 @@
 ce(E
-XCEPE_STR
+SCAPE_CHARACTERS
 , %22%22
@@ -1208,20 +1208,16 @@
 strip()%0A
-    
     resp
@@ -1245,28 +1245,24 @@
 s(req_text)%0A
-    
     post_dic
@@ -1332,319 +1332,77 @@
     
-    return jsonify(post_dict)%0A    else:%0A        return Response(status=req.status_code)%0A%0A%0A@app.route(%22/posts/%3Cpost_id%3E%22, methods=%5B%22GET%22%5D)%0Adef get_post(post_id):%0A    pass%0A%0A%0A@app.route(%22/top%22)%0Adef get_top_posts():%0A    pass%0A%0A%0A@app.route(%22/tags/%3Ctag_name%3E%22, methods=%5B%22GET%22%5D)%0Adef get_posts_by_tag(tag_name):%0A    pass
+print(post_dict)%0A    app.logger.debug(post_dict)%0A    return post_dict
 %0A%0A%0Ai

Add top and tags post listing api