小雨宙

Let's debug the world.

blog_mini gravatar被墙问题解决方案(需要博客本身部署在墙外)

blog_mini gravatar被墙,以及多说无用的问题,我的解决方案(需要博客本身部署在墙外)


增加依赖:flask-cache, redis, requests

diff --git a/app/main/views.py b/app/main/views.py
index 91f24c3..3ebc407 100644
--- a/app/main/views.py
+++ b/app/main/views.py
@@ -1,12 +1,14 @@
 #coding:utf-8
 from flask import render_template, request, current_app, redirect,\
-    url_for, flash
+    url_for, flash, Response, abort
 from . import main
 from ..models import Article, ArticleType, article_types, Comment, \
     Follow, User, Source, BlogView
 from .forms import CommentForm
 from .. import db
-
+from urlparse import urlsplit, urlunsplit
+from flask_cache import Cache
+import requests
 
 @main.route('/')
 def index():
@@ -20,6 +22,19 @@ def index():
                            pagination=pagination, endpoint='.index')
 
 
+@main.route('/avatar/<string:path>')
+def get_avatar(path):
+    if 'rains.im/' not in str(request.referrer):
+        return abort(404)
+    scheme, host, path, query, fragment = urlsplit(request.url)
+    url = urlunsplit(('http', 'secure.gravatar.com', path, query, fragment))
+    cache = Cache(current_app)
+    @cache.memoize(86400)
+    def _get_avatar(url):
+        ret = requests.get(url)
+        return ret.content
+    return Response(_get_avatar(url), mimetype='image/png')
+
 @main.route('/article-types/<int:id>/')
 def articleTypes(id):
     BlogView.add_view(db)
diff --git a/app/models.py b/app/models.py
index d052ec4..3714231 100644
--- a/app/models.py
+++ b/app/models.py
@@ -48,7 +48,7 @@ class User(UserMixin, db.Model):
         #     url = 'https://secure.gravatar.com/avatar'
         # else:
         #     url = 'http://www.gravatar.com/avatar'
-        url = 'http://gravatar.duoshuo.com/avatar'
+        url = '/avatar'
         hash = self.avatar_hash or hashlib.md5(
             self.email.encode('utf-8')).hexdigest()
         return '{url}/{hash}?s={size}&d={default}&r={rating}'.format(
@@ -241,7 +241,7 @@ class Comment(db.Model):
         #     url = 'https://secure.gravatar.com/avatar'
         # else:
         #     url = 'http://www.gravatar.com/avatar'
-        url = 'http://gravatar.duoshuo.com/avatar'
+        url = '/avatar'
         hash = self.avatar_hash or hashlib.md5(
             self.author_email.encode('utf-8')).hexdigest()
         return '{url}/{hash}?s={size}&d={default}&r={rating}'.format(
diff --git a/config.py b/config.py
index 97f7943..3441fdf 100644
--- a/config.py
+++ b/config.py
@@ -4,6 +4,11 @@ basedir = os.path.abspath(os.path.dirname(__file__))
 
 class Config():
     # DEBUG = True
+    CACHE_TYPE = 'redis'
+    CACHE_REDIS_HOST = '127.0.0.1'
+    CACHE_REDIS_PORT = 6379
+    CACHE_REDIS_DB = None
+    CACHE_REDIS_PASSWORD = None
     SQLALCHEMY_TRACK_MODIFICATIONS = True
     # SQLALCHEMY_COMMIT_ON_TEARDOWN = True
     SQLALCHEMY_RECORD_QUERIES = True

博文最后更新时间:


评论

  • 暂无评论

发表评论

博客统计

访问量:3647

博文总数:9 评论总数:1

原创9 翻译0 转载0