From 6093274d68c02033b6fb86c4534f04a4d9abee8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BD=A6=E4=BA=AE=E4=BA=AE?= Date: Tue, 17 Jan 2017 23:23:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DjangoBlog/settings.py | 4 +-- DjangoBlog/spider_notify.py | 6 +++-- blog/management/commands/ping_baidu.py | 37 ++++++++++++++++++++------ 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/DjangoBlog/settings.py b/DjangoBlog/settings.py index 8975987..4c03e5e 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'] @@ -213,5 +213,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'))