diff --git a/DjangoBlog/settings.py b/DjangoBlog/settings.py index ac6df0b..3f0e707 100644 --- a/DjangoBlog/settings.py +++ b/DjangoBlog/settings.py @@ -23,7 +23,7 @@ SECRET_KEY = '&3g0bdza#c%dm1lf%5gi&0-*53p3t0m*hmcvo29cn^$ji7je(c' # SECURITY WARNING: don't run with debug turned on in production! # DEBUG = True -DEBUG = False +DEBUG = True # ALLOWED_HOSTS = [] ALLOWED_HOSTS = ['www.lylinux.net', '127.0.0.1'] @@ -179,11 +179,11 @@ CACHE_CONTROL_MAX_AGE = 2592000 # cache setting CACHES = { - 'default': { + 'default1': { 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 'LOCATION': '127.0.0.1:11211', }, - 'localmem': { + 'default': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 'LOCATION': 'unique-snowflake', } @@ -214,5 +214,5 @@ EMAIL_HOST_USER = os.environ.get('DJANGO_EMAIL_USER') EMAIL_HOST_PASSWORD = os.environ.get('DJANGO_EMAIL_PASSWORD') DEFAULT_FROM_EMAIL = EMAIL_HOST_USER SERVER_EMAIL = os.environ.get('DJANGO_EMAIL_USER') -#设置debug=false 未处理异常邮件通知 +# 设置debug=false 未处理异常邮件通知 ADMINS = [('liangliang', 'liangliangyy@gmail.com')] diff --git a/DjangoBlog/spider_notify.py b/DjangoBlog/spider_notify.py index 03304f7..1f24083 100644 --- a/DjangoBlog/spider_notify.py +++ b/DjangoBlog/spider_notify.py @@ -12,15 +12,17 @@ @file: spider_notify.py @time: 2017/1/15 下午1:41 """ + from django.contrib.sitemaps import ping_google import requests from django.conf import settings class sipder_notify(): - def baidu_notify(self, url): + def baidu_notify(self, urls): try: - result = requests.post(settings.BAIDU_NOTIFY_URL, data=url) + data = '\n'.join(urls) + result = requests.post(settings.BAIDU_NOTIFY_URL, data=data) print(result.text) except Exception as e: print(e) diff --git a/blog/management/commands/ping_baidu.py b/blog/management/commands/ping_baidu.py index ae1c396..6ec8b97 100644 --- a/blog/management/commands/ping_baidu.py +++ b/blog/management/commands/ping_baidu.py @@ -14,20 +14,41 @@ """ from django.core.management.base import BaseCommand, CommandError -from blog.models import Article +from blog.models import Article, Tag, Category from DjangoBlog.spider_notify import sipder_notify +from django.contrib.sites.models import Site + +site = Site.objects.get_current().domain class Command(BaseCommand): help = 'notify baidu url' + def add_arguments(self, parser): + parser.add_argument('data_type', type=str, choices=['all', 'article', 'tag', 'category'], + help='article : all article,tag : all tag,category: all category,all: All of these') + + def get_full_url(self, path): + url = "https://{site}{path}".format(site=site, path=path) + return url + def handle(self, *args, **options): + type = options['data_type'] + self.stdout.write('start get %s' % type) notify = sipder_notify() - for article in Article.objects.filter(status='p'): - try: - url = article.get_full_url() - notify.baidu_notify(url=url) - self.stdout.write(self.style.SUCCESS('Successfully notify article id : "%s"' % article.pk)) - except Exception as e: - self.stdout.write('error:' + str(e)) + urls = [] + if type == 'article' or type == 'all': + for article in Article.objects.filter(status='p'): + urls.append(article.get_full_url()) + if type == 'tag' or type == 'all': + for tag in Tag.objects.all(): + url = tag.get_absolute_url() + urls.append(self.get_full_url(url)) + if type == 'category' or type == 'all': + for category in Category.objects.all(): + url = category.get_absolute_url() + urls.append(self.get_full_url(url)) + + self.stdout.write(self.style.SUCCESS('start notify %d urls' % len(urls))) + notify.baidu_notify(urls) self.stdout.write(self.style.SUCCESS('finish notify')) diff --git a/blog/middleware.py b/blog/middleware.py index dccc53d..b79d432 100644 --- a/blog/middleware.py +++ b/blog/middleware.py @@ -31,18 +31,17 @@ class OnlineMiddleware(object): return online_ips = cache.get("online_ips", []) - if online_ips: online_ips = cache.get_many(online_ips).keys() - + online_ips = list(online_ips) ip = get_real_ip(request) cache.set(ip, 0, 5 * 60) if ip not in online_ips: online_ips.append(ip) - - cache.set("online_ips", online_ips) + s = type(online_ips) + cache.set("online_ips", online_ips) def process_response(self, request, response): cast_time = time.time() - self.start_time