You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
git-test/src/DjangoBlog-master/blog/management/commands/ping_baidu.py

51 lines
2.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from django.core.management.base import BaseCommand
from djangoblog.spider_notify import SpiderNotify
from djangoblog.utils import get_current_site
from blog.models import Article, Tag, Category
site = get_current_site().domain #ZNY 获取当前站点的域名
class Command(BaseCommand): #ZNY 定义通知百度URL命令类继承BaseCommand
help = 'notify baidu url' #ZNY 命令帮助信息通知百度URL
def add_arguments(self, parser): #ZNY 添加命令行参数方法
parser.add_argument( #ZNY 添加数据类型参数
'data_type', #ZNY 参数名称
type=str, #ZNY 参数类型为字符串
choices=[ #ZNY 参数可选值
'all', #ZNY 所有类型
'article', #ZNY 仅文章
'tag', #ZNY 仅标签
'category'], #ZNY 仅分类
help='article : all article,tag : all tag,category: all category,all: All of these') #ZNY 参数帮助信息
def get_full_url(self, path): #ZNY 获取完整URL方法
url = "https://{site}{path}".format(site=site, path=path) #ZNY 构建完整的HTTPS URL
return url #ZNY 返回完整URL
def handle(self, *args, **options): #ZNY 命令处理主方法
type = options['data_type'] #ZNY 从参数获取数据类型
self.stdout.write('start get %s' % type) #ZNY 输出开始获取数据的提示
urls = [] #ZNY 初始化URL列表
if type == 'article' or type == 'all': #ZNY 如果类型是文章或全部
for article in Article.objects.filter(status='p'): #ZNY 遍历所有已发布的文章
urls.append(article.get_full_url()) #ZNY 将文章完整URL添加到列表
if type == 'tag' or type == 'all': #ZNY 如果类型是标签或全部
for tag in Tag.objects.all(): #ZNY 遍历所有标签
url = tag.get_absolute_url() #ZNY 获取标签的相对URL
urls.append(self.get_full_url(url)) #ZNY 转换为完整URL并添加到列表
if type == 'category' or type == 'all': #ZNY 如果类型是分类或全部
for category in Category.objects.all(): #ZNY 遍历所有分类
url = category.get_absolute_url() #ZNY 获取分类的相对URL
urls.append(self.get_full_url(url)) #ZNY 转换为完整URL并添加到列表
self.stdout.write( #ZNY 输出开始通知的提示
self.style.SUCCESS( #ZNY 使用成功样式
'start notify %d urls' % #ZNY 显示要通知的URL数量
len(urls)))
SpiderNotify.baidu_notify(urls) #ZNY 调用百度通知功能推送URL
self.stdout.write(self.style.SUCCESS('finish notify')) #ZNY 输出完成通知的提示