#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import

import os
import requests
from flask import Blueprint, jsonify, request, redirect

from me_api.configs import Config
from me_api.cache import cache


config = Config.modules['modules']['douban']
path = config['path']
me, client_secret, access_token, client_id = (
    config['data']['me'],
    config['data']['client_secret'],
    config['data']['access_token'],
    config['data']['client_id']
)
douban_api = Blueprint('douban', __name__, url_prefix=path)


@douban_api.route('/')
@cache.cached(timeout=3600)
def douban():
    if not access_token:
        return 'Need access token, please authenticate your app first.'
    response = requests.get(
        "https://api.douban.com/shuo/v2/statuses/user_timeline/{0}".format(me),
        headers={'Authorization': 'Bearer ' + access_token})
    return jsonify(douban=response.json())


@douban_api.route('/login')
def authorization():
    if access_token:
        return "You've already had an access token in the config file."
    authorization_url = 'https://www.douban.com/service/auth2/auth'
    return redirect(
        '{0}?client_id={1}&redirect_uri={2}&response_type={3}'.format(
            authorization_url, client_id,
            os.path.join(request.url, 'redirect'), 'code')
    )


@douban_api.route('/login/redirect')
def get_access_toekn():
    authorization_code = request.args.get('code', '')
    token_url = 'https://www.douban.com/service/auth2/token'
    post_data = {
        'client_id': client_id,
        'client_secret': client_secret,
        'redirect_uri': request.base_url,
        'grant_type': 'authorization_code',
        'code': authorization_code
    }
    response = requests.post(token_url, data=post_data)
    return response.text

@@ -180,22 +180,20 @@
 pi.c
-onfigs
+ache
  import 
 Conf
@@ -188,22 +188,21 @@
  import 
-Config
+cache
 %0Afrom me
@@ -210,274 +210,85 @@
 api.
-cache import cache%0A%0A%0Aconfig = Config.modules%5B'modules'%5D%5B'douban'%5D%0Apath = config%5B'path'%5D%0Ame, client_secret, access_token, client_id = (%0A    config%5B'data'%5D%5B'me'%5D,%0A    config%5B'data'%5D%5B'client_secret'%5D,%0A    c
+middleware.utils import MiddlewareConfig%0A%0A%0Aconfig = MiddlewareC
 onfig
-%5B'data'%5D%5B'access_token'%5D,%0A    config%5B'data'%5D%5B'client_id'%5D%0A
+('douban'
 )%0Ado
@@ -339,16 +339,23 @@
 _prefix=
+config.
 path)%0A%0A%0A
@@ -430,16 +430,23 @@
  if not 
+config.
 access_t
@@ -560,16 +560,17 @@
         
+(
 %22https:/
@@ -602,16 +602,28 @@
 tatuses/
+%22%0A         %22
 user_tim
@@ -632,24 +632,25 @@
 ine/%7B0%7D%22
+)
 .format(
 me),%0A   
@@ -641,16 +641,23 @@
 .format(
+config.
 me),%0A   
@@ -699,16 +699,23 @@
 rer ' + 
+config.
 access_t
@@ -822,16 +822,23 @@
 %0A    if 
+config.
 access_t
@@ -1106,16 +1106,23 @@
 on_url, 
+config.
 client_i
@@ -1406,16 +1406,23 @@
 nt_id': 
+config.
 client_i
@@ -1449,16 +1449,23 @@
 ecret': 
+config.
 client_s

Simplify loading config for douban middleware