From 3892ad0aa703c521750cc031d0f5e33a2d90a99d Mon Sep 17 00:00:00 2001 From: liangliangyy Date: Tue, 17 Jan 2017 15:41:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0ping=5Fbaidu=20=E5=91=BD?= =?UTF-8?q?=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DjangoBlog/spider_notify.py | 4 ++-- blog/management/__init__.py | 0 blog/management/commands/__init__.py | 0 blog/management/commands/ping_baidu.py | 33 ++++++++++++++++++++++++++ blog/models.py | 8 ++++++- 5 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 blog/management/__init__.py create mode 100644 blog/management/commands/__init__.py create mode 100644 blog/management/commands/ping_baidu.py diff --git a/DjangoBlog/spider_notify.py b/DjangoBlog/spider_notify.py index 63f24d7..03304f7 100644 --- a/DjangoBlog/spider_notify.py +++ b/DjangoBlog/spider_notify.py @@ -18,7 +18,7 @@ from django.conf import settings class sipder_notify(): - def __baidu_notify(self, url): + def baidu_notify(self, url): try: result = requests.post(settings.BAIDU_NOTIFY_URL, data=url) print(result.text) @@ -32,5 +32,5 @@ class sipder_notify(): print(e) def notify(self, url): - self.__baidu_notify(url) + self.baidu_notify(url) self.__google_notify() diff --git a/blog/management/__init__.py b/blog/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/blog/management/commands/__init__.py b/blog/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/blog/management/commands/ping_baidu.py b/blog/management/commands/ping_baidu.py new file mode 100644 index 0000000..ae1c396 --- /dev/null +++ b/blog/management/commands/ping_baidu.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python +# encoding: utf-8 + + +""" +@version: ?? +@author: liangliangyy +@license: MIT Licence +@contact: liangliangyy@gmail.com +@site: https://www.lylinux.org/ +@software: PyCharm +@file: ping_baidu.py +@time: 2017/1/17 下午15:29 +""" + +from django.core.management.base import BaseCommand, CommandError +from blog.models import Article +from DjangoBlog.spider_notify import sipder_notify + + +class Command(BaseCommand): + help = 'notify baidu url' + + def handle(self, *args, **options): + 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)) + self.stdout.write(self.style.SUCCESS('finish notify')) diff --git a/blog/models.py b/blog/models.py index 93d7680..4dcb669 100644 --- a/blog/models.py +++ b/blog/models.py @@ -4,6 +4,7 @@ from django.conf import settings # from django.template.defaultfilters import slugify from uuslug import slugify from DjangoBlog.spider_notify import sipder_notify +from django.contrib.sites.models import Site class Article(models.Model): @@ -74,7 +75,7 @@ class Article(models.Model): self.slug = slugify(self.title) try: notify = sipder_notify() - notify.notify(self.get_absolute_url()) + notify.notify(self.get_full_url()) except Exception as e: print(e) super().save(*args, **kwargs) @@ -91,6 +92,11 @@ class Article(models.Model): info = (self._meta.app_label, self._meta.model_name) return reverse('admin:%s_%s_change' % info, args=(self.pk,)) + def get_full_url(self): + site = Site.objects.get_current().domain + article_url = "https://{site}{path}".format(site=site, path=self.get_absolute_url()) + return article_url + ''' class BlogPage(models.Model):