Garden bulk delete blog tags

Long time no blog park, and recently changed to change the topic and found a pile of garbage tag, but the official did not provide a place to park blog batch delete, so simple to write a script, Python code written by:

import requests
import json
import time

postids = []
tags = ['编程', '从入门到精通', 'Linux', '新手', '电脑', 'Linux入门', '编程', 'java', '计算机', '方法','菜鸟', 'J2SE', 'acm', 'Linux发展史', 'GUI', 'mui', 'c++细节', 'C++', '磁盘管理', '动态规划', 'wifi', 'round', 'python3', 'PHP', 'ORACLE', 'nyoj', 'MYSQL', 'Java Web', 'Firefox Browser', 'cnsimo', 'Chrome', '异常处理', '网络', '算法', '四舍五入']
postsapibase = 'https://i-beta.cnblogs.com/api/posts/'
postslistapi = 'https://i-beta.cnblogs.com/api/posts/list'
cookies = ''
headers = {"accept": "application/json, text/plain, */*", "accept-encoding": "gzip, deflate, br", "content-type": "application/json", "x-blog-id": ""}

def parseCookies():
    r = {}
    cl = cookies.split(";")
    for c in cl:
        m = c.split('=', 1)
        r[m[0].strip()] = m[1]
    return r

def getPostInfo(id):
    r = requests.get(postsapibase+id, cookies=cookies)
    return r.json()

def delTags(id):
    postinfo = getPostInfo(id)['blogPost']
    ts = postinfo['tags']
    newts = []
    for t in ts:
        if not t in tags:
            newts.append(t)
    postinfo['tags'] = newts
    headers["x-blog-id"] = str(postinfo["blogId"])
    r = requests.post(postsapibase, headers=headers, data=json.dumps(postinfo), cookies=cookies)
    return r.status_code
    pass

def getAllPosts():
    posts = []
    count = 0
    params = {'p': 1, 't': 1, 'cfg': 0}
    while True:
        r = requests.get(postslistapi, cookies=cookies, params=params)
        posts.extend(r.json()['postList'])
        count += len(r.json()['postList'])
        if count >= r.json()['postsCount']:
            return posts
        params['p'] += 1

if __name__ == '__main__':
    cookies = parseCookies()
    postsList = getAllPosts()
    for p in postsList:
        postids.append(str(p['id']))
    # print(postids)
    for id in postids:
        if delTags(id) != 200:
            print(id + ': 失败!')
        else:
            print(id + ': 成功!')
        time.sleep(1)

In cookiesfill their own cookies, delete run, there is no confirmation, so use caution.

Guess you like

Origin www.cnblogs.com/lxmwb/p/12574421.html