Update ping_baidu.py

master
p36kxhw2t 1 month ago
parent 9db6f6ae37
commit 4bc28093f6

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

Loading…
Cancel
Save