折腾:
【已解决】给Python发布印象笔记帖子内容到WordPress文章时加上分类和标签
期间,希望不要新建,而是给定名字,查到之前已有(最好是唯一的)同名的category目录(和后续的tag标签)
结果之前已尝试:
【已解决】Python调用WordPress的REST接口新建Category目录
此处会,新建一个同名的category,而不是告知已有了。
哎,不是我希望的结果。
因为希望是,告知已有,这样就可以:不用新建了。
不过突然发现:
其中有个search
或许可以实现我们要的:通过
/wp-json/wp/v2/categories
加上search参数,比如搜mac,返回之前已有的,且唯一的目录,则即可实现:
判断是否已存在同名的category(和后续的tag)
去试试
def searchCategory(self, name): """Search wordpress category by call REST api: GET /wp-json/wp/v2/categories Args: name (str): category name to search Returns: (bool, dict) True, found category info False, error detail Raises: """ curHeaders = { "Authorization": self.authorization, "Accept": "application/json", } logging.debug("curHeaders=%s", curHeaders) queryDict = { "search": name, # 'Mac' } searchCategoryUrl = self.apiCategories resp = requests.get( searchCategoryUrl, proxies=self.requestsProxies, headers=curHeaders, data=queryDict, ) logging.info("resp=%s", resp) isSearchOk, respInfo = crifanWordpress.processCreateResponse(resp) return isSearchOk, respInfo
调试看看
{'code': 'jwt_auth_invalid_token', 'message': 'Expired token', 'data': {'status': 403}}
重新去更新一个jwt的token,再试试
可以返回了:
[{ "id": 4637, "count": 4, "description": "", "link": "https://www.crifan.com/category/life/computer_digit_soft/web_browser/soft_360/", "name": "360", "slug": "soft_360", "taxonomy": "category", "parent": 8816, "meta": [], "_links": { "self": [{ "href": "https://www.crifan.com/wp-json/wp/v2/categories/4637" }], "collection": [{ "href": "https://www.crifan.com/wp-json/wp/v2/categories" }], "about": [{ "href": "https://www.crifan.com/wp-json/wp/v2/taxonomies/category" }], "up": [{ "embeddable": True, "href": "https://www.crifan.com/wp-json/wp/v2/categories/8816" }], "wp:post_type": [{ "href": "https://www.crifan.com/wp-json/wp/v2/posts?categories=4637" }], "curies": [{ "name": "wp", "href": "https://api.w.org/{rel}", "templated": True }] } }, { ... }] } }]
-》竟然里面没有Mac这个关键字。。。
难道search没生效??
看了下,目前是返回了10个category
“per_page
Maximum number of items to be returned in result set.
Default: 10”
是这个参数决定的。
wp-json/wp/v2/categories search
fetching categories with rest APIv2 | WordPress.org
难道是requests中传递有误,参数没传递进去?
不是data=queryDict?
参考:
payload = {'key1': 'value1', 'key2': 'value2’} r = requests.get('https://httpbin.org/get', params=payload) print(r.url) # https://httpbin.org/get?key2=value2&key1=value1
果然是写错了。
改为:
params=queryDict, # {'search': 'Mac'}
结果:
就对了:
[{ "id": 1639, "count": 7, "description": "", "link": "https://www.crifan.com/category/work_and_job/operating_system_and_platform/mac/cocoa-mac/", "name": "Cocoa", "slug": "cocoa-mac", "taxonomy": "category", "parent": 1374, "meta": [], "_links": { "self": [{ "href": "https://www.crifan.com/wp-json/wp/v2/categories/1639" }], "collection": [{ "href": "https://www.crifan.com/wp-json/wp/v2/categories" }], "about": [{ "href": "https://www.crifan.com/wp-json/wp/v2/taxonomies/category" }], "up": [{ "embeddable": True, "href": "https://www.crifan.com/wp-json/wp/v2/categories/1374" }], "wp:post_type": [{ "href": "https://www.crifan.com/wp-json/wp/v2/posts?categories=1639" }], "curies": [{ "name": "wp", "href": "https://api.w.org/{rel}", "templated": True }] } }, { "id": 1374, "count": 350, "description": "", "link": "https://www.crifan.com/category/work_and_job/operating_system_and_platform/mac/", "name": "Mac", "slug": "mac", "taxonomy": "category", "parent": 4624, "meta": [], "_links": { "self": [{ "href": "https://www.crifan.com/wp-json/wp/v2/categories/1374" }], "collection": [{ "href": "https://www.crifan.com/wp-json/wp/v2/categories" }], "about": [{ "href": "https://www.crifan.com/wp-json/wp/v2/taxonomies/category" }], "up": [{ "embeddable": True, "href": "https://www.crifan.com/wp-json/wp/v2/categories/4624" }], "wp:post_type": [{ "href": "https://www.crifan.com/wp-json/wp/v2/posts?categories=1374" }], "curies": [{ "name": "wp", "href": "https://api.w.org/{rel}", "templated": True }] } }, { "id": 13223, "count": 0, "description": "", "link": "https://www.crifan.com/category/mac-2/", "name": "Mac", "slug": "mac-2", 。。。 }, { "id": 1553, "count": 201, "description": "", "link": "https://www.crifan.com/category/work_and_job/develop_ide_editors/xcode-mac/", "name": "Xcode", "slug": "xcode-mac", "taxonomy": "category", "parent": 2130, "meta": [], "_links": { "self": [{ "href": "https://www.crifan.com/wp-json/wp/v2/categories/1553" }], "collection": [{ "href": "https://www.crifan.com/wp-json/wp/v2/categories" }], "about": [{ "href": "https://www.crifan.com/wp-json/wp/v2/taxonomies/category" }], "up": [{ "embeddable": True, "href": "https://www.crifan.com/wp-json/wp/v2/categories/2130" }], "wp:post_type": [{ "href": "https://www.crifan.com/wp-json/wp/v2/posts?categories=1553" }], "curies": [{ "name": "wp", "href": "https://api.w.org/{rel}", "templated": True }] } }]
其中
第二个就是:我们要的:
"id": 1374, "count": 350, "description": "", "link": "https://www.crifan.com/category/work_and_job/operating_system_and_platform/mac/", "name": "Mac", "slug": "mac",
第三个就是我们刚新建的:
"id": 13223, "count": 0, "description": "", "link": "https://www.crifan.com/category/mac-2/", "name": "Mac", "slug": "mac-2",
【总结】
此处用代码:
def searchCategory(self, name): """Search wordpress category by call REST api: GET /wp-json/wp/v2/categories Args: name (str): category name to search Returns: (bool, dict) True, found category info False, error detail Raises: """ curHeaders = { "Authorization": self.authorization, "Accept": "application/json", } logging.debug("curHeaders=%s", curHeaders) queryParamDict = { "search": name, # 'Mac' } searchCategoryUrl = self.apiCategories resp = requests.get( searchCategoryUrl, proxies=self.requestsProxies, headers=curHeaders, # data=queryDict, # {'search': 'Mac'} params=queryParamDict, # {'search': 'Mac'} ) logging.info("resp=%s", resp) isSearchOk, respInfo = crifanWordpress.processCreateResponse(resp) return isSearchOk, respInfo
调用:
firstCategory = categories[0] isSearhOk, existedCategoryInfo = self.searchCategory(firstCategory) logging.info("isSearhOk=%s, existedCategoryInfo=%s", isSearhOk, existedCategoryInfo)
是可以返回:我们希望的数据的
一共9个,其中第二个就是我们要的:
[{ 。。。 }, { "id": 1374, "count": 350, "description": "", "link": "https://www.crifan.com/category/work_and_job/operating_system_and_platform/mac/", "name": "Mac", "slug": "mac", "taxonomy": "category", "parent": 4624, "meta": [], "_links": { "self": [{ "href": "https://www.crifan.com/wp-json/wp/v2/categories/1374" }], "collection": [{ "href": "https://www.crifan.com/wp-json/wp/v2/categories" }], "about": [{ "href": "https://www.crifan.com/wp-json/wp/v2/taxonomies/category" }], "up": [{ "embeddable": True, "href": "https://www.crifan.com/wp-json/wp/v2/categories/4624" }], "wp:post_type": [{ "href": "https://www.crifan.com/wp-json/wp/v2/posts?categories=1374" }], "curies": [{ "name": "wp", "href": "https://api.w.org/{rel}", "templated": True }] } }, { 。。。
即:
name=Mac,slug=mac
的category。
即可供后续使用。
【后记20201204】
后续已优化代码:
【已解决】Python用WordPress的REST接口实现查询已有同名的taxonomy分类支持category目录和tag标签