折腾:
【未解决】Python用WordPress的REST接口实现查到之前同名的category目录和tag标签
期间,先去看看用Python调用Wordpress的REST的api,去新建目录分类category
找到之前的接口说明:
“categories
array
The terms assigned to the object in the category taxonomy.
Context: view, edit
tags
array
The terms assigned to the object in the post_tag taxonomy.
Context: view, edit“
看了下,貌似是:
没有直接从 name 去查询是否存在的接口
只能通过接口去 list all的category/tag
然后对于返回的所有的category/tag,通过name对比
如果name有相同,则得到对应id
否则就调用post 去新建
突然想到,要不要直接去
POST /wp/v2/tags
看看返回的是啥
如果返回已存在的tag
那么就不用列出所有,这么麻烦了。去试试
去试试
此处调试之前,先去看看此处WordPress后台已有的tag:
->
tag_ID=8123
POST /wp/v2/categories
写代码:
# https://developer.wordpress.org/rest-api/reference/categories/#create-a-category self.apiCategories = self.host + "/wp-json/wp/v2/categories" # 'https://www.crifan.com/wp-json/wp/v2/categories' def createCategory(self, name, parent=None, slug=None, description=None): """Create wordpress category by call REST api: POST /wp-json/wp/v2/categories Args: name (str): category name parent (int): category parent slug (str): category slug description (str): category description Returns: (bool, dict) True, uploaded category info False, error detail Raises: """ curHeaders = { "Authorization": self.authorization, "Content-Type": "application/json", "Accept": "application/json", } logging.info("curHeaders=%s", curHeaders) # postDict = { "name": name, # } if parent: postDict["parent"] = parent if slug: postDict["slug"] = slug if description: postDict["description"] = description logging.info("postDict=%s", postDict) # createCategoryUrl = self.apiCategories resp = requests.post( createCategoryUrl, proxies=self.requestsProxies, headers=curHeaders, json=postDict, ) logging.info("resp=%s", resp) isUploadOk, respInfo = crifanWordpress.processCreateResponse(resp) return isUploadOk, respInfo
去调试
先去创建 Mac
此处返回201,是我们希望看到的
-》如果没有,新建,返回200
如果已有,返回201
不对,是201=Created,新创建了
'{"id":13222,"count":0,"description":"","link":"https:\\/\\/www.crifan.com\\/category\\/mac\\/","name":"Mac","slug":"mac","taxonomy":"category","parent":0,"meta":[],"_links":{"self":[{"href":"https:\\/\\/www.crifan.com\\/wp-json\\/wp\\/v2\\/categories\\/13222"}],"collection":[{"href":"https:\\/\\/www.crifan.com\\/wp-json\\/wp\\/v2\\/categories"}],"about":[{"href":"https:\\/\\/www.crifan.com\\/wp-json\\/wp\\/v2\\/taxonomies\\/category"}],"wp:post_type":[{"href":"https:\\/\\/www.crifan.com\\/wp-json\\/wp\\/v2\\/posts?categories=13222"}],"curies":[{"name":"wp","href":"https:\\/\\/api.w.org\\/{rel}","templated":true}]}}'
是新建了一个分类category
那去WordPress后台看看,是否多出一个,同名的Mac分类
红色:之前已有的,name=Mac,slug=os_mac,已有305个帖子了
绿色:刚新建的,name=Mac,slug=mac(看来就是针对于原先name的小写,不知道中文时,如何处理),已有帖子0个
-》从逻辑上来说,代码这么自动处理,是没问题的。
不过,为了测试和已有的标签的name和slug都相同,则:
删除刚创建的
name=Mac,slug=mac
且去把已有的
name=Mac,slug=os_mac
改为:
name=Mac,slug=mac
且复制编辑的链接是:
-》得知:tag_ID=1374
然后重新调用一次代码,看看会不会冲突,报错,或者说已存在
还是新建了一个:
'{"id":13223,"count":0,"description":"","link":"https:\\/\\/www.crifan.com\\/category\\/mac-2\\/","name":"Mac","slug":"mac-2","taxonomy":"category","parent":0,"meta":[],"_links":{"self":[{"href":"https:\\/\\/www.crifan.com\\/wp-json\\/wp\\/v2\\/categories\\/13223"}],"collection":[{"href":"https:\\/\\/www.crifan.com\\/wp-json\\/wp\\/v2\\/categories"}],"about":[{"href":"https:\\/\\/www.crifan.com\\/wp-json\\/wp\\/v2\\/taxonomies\\/category"}],"wp:post_type":[{"href":"https:\\/\\/www.crifan.com\\/wp-json\\/wp\\/v2\\/posts?categories=13223"}],"curies":[{"name":"wp","href":"https:\\/\\/api.w.org\\/{rel}","templated":true}]}}'
很明显,此处会:
直接新建一个。。。slug是xxx_2
【总结】
至此,用代码
# https://developer.wordpress.org/rest-api/reference/categories/#create-a-category self.apiCategories = self.host + "/wp-json/wp/v2/categories" # 'https://www.crifan.com/wp-json/wp/v2/categories' def createCategory(self, name, parent=None, slug=None, description=None): """Create wordpress category by call REST api: POST /wp-json/wp/v2/categories Args: name (str): category name parent (int): category parent slug (str): category slug description (str): category description Returns: (bool, dict) True, uploaded category info False, error detail Raises: """ curHeaders = { "Authorization": self.authorization, "Content-Type": "application/json", "Accept": "application/json", } logging.debug("curHeaders=%s", curHeaders) # curHeaders={'Authorization': 'Bearer eyJxx9.eyJpc3Mxxx19fQ.5IwxxLm-6Yxxx5s', 'Content-Type': 'application/json', 'Accept': 'application/json'} postDict = { "name": name, # } if parent: postDict["parent"] = parent if slug: postDict["slug"] = slug if description: postDict["description"] = description logging.info("postDict=%s", postDict) # postDict={'name': 'Mac'} createCategoryUrl = self.apiCategories resp = requests.post( createCategoryUrl, proxies=self.requestsProxies, headers=curHeaders, json=postDict, ) logging.info("resp=%s", resp) isUploadOk, respInfo = crifanWordpress.processCreateResponse(resp) return isUploadOk, respInfo
是可以用于新建category的。
注意:
如果之前已有一个category的name(和slug)
则:新建时,即使name相同,也会重新创建一个的(slug=xxx_2)
返回的结果是:
{ "id": 13223, "count": 0, "description": "", "link": "https://www.crifan.com/category/mac-2/", "name": "Mac", "slug": "mac-2", "taxonomy": "category", "parent": 0, "meta": [], "_links": { "self": [{ "href": "https://www.crifan.com/wp-json/wp/v2/categories/13223" }], "collection": [{ "href": "https://www.crifan.com/wp-json/wp/v2/categories" }], "about": [{ "href": "https://www.crifan.com/wp-json/wp/v2/taxonomies/category" }], "wp:post_type": [{ "href": "https://www.crifan.com/wp-json/wp/v2/posts?categories=13223" }], "curies": [{ "name": "wp", "href": "https://api.w.org/{rel}", "templated": True }] } }
供参考。
【后记20201204】
继续去优化:
【已解决】Python调用WordPress的REST接口新建Taxonomy分类包括category目录和tag标签