猫史档案馆


【零成本】创建一个可以通过python调用的数据库

用户:治愈绾兮治愈绾兮查看:4 回复:3 评论:4 创建时间:2023-01-14T21:47:55


center_image

源代码:

from bs4 import BeautifulSoup
import requests
import json

_headers = {
    "Content-Type": "application/json",
    "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; ) '
    'AppleWebKit/537.36 (KH喵L, like Gecko) '
    'Chrome/81.0.4044.138 Safari/537.36'}


def _get(url: str, cookies={}):
    return requests.get(url, headers=_headers, cookies=cookies)


def _post(url: str, data={}, cookies={}):
    return requests.post(url, headers=_headers, data=json.dumps(data), cookies=cookies)


def _patch(url: str, data={}, cookies={}):
    return requests.patch(url, headers=_headers, data=json.dumps(data), cookies=cookies)


def _delete(url: str, cookies={}):
    return requests.delete(url, headers=_headers, cookies=cookies)


class _Error(Exception):
    pass


class Create_DataBase:
    def __init__(self, identity: str, password: str, post_id: int):
        if not (type(identity) == str and type(password) == str and type(post_id) == int):
            raise _Error("Error: parameter error")
        response = _post(
            "https://api.codemao.cn/tiger/v3/web/accounts/login", data={"identity": identity, "password": password, "pid": "65edCTyg"})
        if "user_info" not in json.loads(response.text):
            raise _Error("Error: identity or password error")
        self.cookies = response.cookies
        self.user_id = str(json.loads(response.text)["user_info"]["id"])
        response = _get("https://api.codemao.cn/web/forums/posts/" +
                        str(post_id) + "/details", cookies=self.cookies)
        if "user" not in json.loads(response.text):
            raise _Error("Error: post_id error")
        if json.loads(response.text)["user"]["id"] != self.user_id:
            raise _Error("Error: the identity is not the post's author")
        self.post_id = str(post_id)

    def _get_reply_list(self):
        reply_list = []
        i = 1
        while True:
            response = _get("https://api.codemao.cn/web/forums/posts/"+self.post_id +
                            "/replies?page="+str(i)+"&limit=30", cookies=self.cookies)
            if len(json.loads(response.text)["items"]) == 0:
                break
            reply_list.extend(json.loads(response.text)["items"])
            i += 1
        return reply_list

    def show(self):
        datas = {}
        for reply in self._get_reply_list():
            try:
                if reply["user"]["id"] != self.user_id:
                    raise _Error("Error")
                soup = BeautifulSoup(reply["content"], "lxml")
                datas[soup.select("p")[0].text.split("ᮨ")[0]] = soup.select("p")[
                    0].text.split("ᮨ")[1]
            except:
                _delete("https://api.codemao.cn/web/forums/replies/" +
                        str(reply["id"]), cookies=self.cookies)
        return datas

    def select(self, key: str):
        if type(key) != str:
            raise _Error("Error: parameter error")
        datas = self.show()
        for data in datas:
            if data == key:
                return datas[data]
        raise _Error("Error: no this key")

    def insert(self, key: str, value: str):
        if not (type(key) == str and type(value) == str):
            raise _Error("Error: parameter error")
        if "ᮨ" in key or "ᮨ" in value:
            raise _Error("Error: key or value can not includes the char 'ᮨ '")
        for data in self.show():
            if data == key:
                raise _Error("Error: this key exists")
        _post("https://api.codemao.cn/web/forums/posts/" +
              self.post_id+"/replies", data={"content": "<p>" + key + "ᮨ" + value+"</p>"}, cookies=self.cookies)

    def modify(self, key: str, value: str):
        if not (type(key) == str and type(value) == str):
            raise _Error("Error: parameter error")
        if "ᮨ" in key or "ᮨ" in value:
            raise _Error("Error: key or value can not includes the char 'ᮨ '")
        back = True
        for data in self.show():
            if data == key:
                back = False
                break
        if back:
            raise _Error("Error: this key does not exist")
        for reply in self._get_reply_list():
            try:
                if reply["user"]["id"] != self.user_id:
                    raise _Error("Error")
                soup = BeautifulSoup(reply["content"], "lxml")
                if soup.select("p")[0].text.split("ᮨ")[0] == key:
                    _delete("https://api.codemao.cn/web/forums/replies/" +
                            str(reply["id"]), cookies=self.cookies)
                    self.insert(key, value)
            except:
                _delete("https://api.codemao.cn/web/forums/replies/" +
                        str(reply["id"]), cookies=self.cookies)

    def delete(self, key: str):
        if type(key) != str:
            raise _Error("Error: parameter error")
        back = True
        for data in self.show():
            if data == key:
                back = False
                break
        if back:
            raise _Error("Error: this key does not exist")
        for reply in self._get_reply_list():
            try:
                if reply["user"]["id"] != self.user_id:
                    raise _Error("Error")
                soup = BeautifulSoup(reply["content"], "lxml")
                if soup.select("p")[0].text.split("ᮨ")[0] == key:
                    _delete("https://api.codemao.cn/web/forums/replies/" +
                            str(reply["id"]), cookies=self.cookies)
            except:
                _delete("https://api.codemao.cn/web/forums/replies/" +
                        str(reply["id"]), cookies=self.cookies)

    def clear(self):
        for reply in self._get_reply_list():
            _delete("https://api.codemao.cn/web/forums/replies/" +
                    str(reply["id"]), cookies=self.cookies)

    def __str__(self):
        return "identity: " + self.user_id + "  post_id: " + self.post_id


回复

上一页1 页 / 共 1下一页
yh12i31yh12i31

作者,把"喵"去掉一下

点赞0


评论


SCS_user_LoeheodVOQSCS_user_LoeheodVOQ

那别人不都看到内容了吗(

点赞0


评论


oWuGi7s6oWuGi7s6

cdmaoapi不香吗(?)))

点赞0


评论