ck_tieba.Tieba.__init__()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
"""
3
cron: 12 16 * * *
4
new Env('百度贴吧');
5
"""
6
7
import hashlib
8
import re
9
10
import requests
11
12
from notify_mtr import send
13
from utils import get_data
14
15
16
class Tieba:
17
    def __init__(self, check_items):
18
        self.check_items = check_items
19
20
    @staticmethod
21
    def login_info(session):
22
        return session.get("https://zhidao.baidu.com/api/loginInfo").json()
23
24
    def valid(self, session):
25
        try:
26
            res = session.get("http://tieba.baidu.com/dc/common/tbs").json()
27
        except Exception as e:
28
            return False, f"登录验证异常,错误信息: {e}"
29
        if res["is_login"] == 0:
30
            return False, "登录失败,cookie 异常"
31
        tbs = res["tbs"]
32
        user_name = self.login_info(session=session)["userName"]
33
        return tbs, user_name
34
35
    @staticmethod
36
    def tieba_list_more(session):
37
        res = session.get(
38
            "https://tieba.baidu.com/f/like/mylike?&pn=1",
39
            timeout=(5, 20),
40
            allow_redirects=False,
41
        ).text
42
        try:
43
            pn = int(
44
                re.match(r".*/f/like/mylike\?&pn=(.*?)\">尾页.*", res, re.S | re.I)[1]
45
            )
46
47
        except Exception:
48
            pn = 1
49
        pattern = re.compile(r".*?<a href=\"/f\?kw=.*?title=\"(.*?)\">")
50
        for next_page in range(2, pn + 2):
51
            yield from pattern.findall(res)
52
            res = session.get(
53
                f"https://tieba.baidu.com/f/like/mylike?&pn={next_page}",
54
                timeout=(5, 20),
55
                allow_redirects=False,
56
            ).text
57
58
    def get_tieba_list(self, session):
59
        return list(self.tieba_list_more(session))
60
61
    @staticmethod
62
    def sign(session, tb_name_list, tbs):
63
        success_count, error_count, exist_count, shield_count = 0, 0, 0, 0
64
        for tb_name in tb_name_list:
65
            md5 = hashlib.md5(
66
                f"kw={tb_name}tbs={tbs}tiebaclient!!!".encode("utf-8")
67
            ).hexdigest()
68
            data = {"kw": tb_name, "tbs": tbs, "sign": md5}
69
            try:
70
                res = session.post(
71
                    url="http://c.tieba.baidu.com/c/c/forum/sign", data=data
72
                ).json()
73
                if res["error_code"] == "0":
74
                    success_count += 1
75
                elif res["error_code"] == "160002":
76
                    exist_count += 1
77
                elif res["error_code"] == "340006":
78
                    shield_count += 1
79
                else:
80
                    error_count += 1
81
            except Exception as e:
82
                print(f"贴吧 {tb_name} 签到异常,原因{str(e)}")
83
        return (
84
            f"贴吧总数: {len(tb_name_list)}\n"
85
            f"签到成功: {success_count}\n"
86
            f"已经签到: {exist_count}\n"
87
            f"被屏蔽的: {shield_count}\n"
88
            f"签到失败: {error_count}"
89
        )
90
91
    def main(self):
92
        msg_all = ""
93
        for check_item in self.check_items:
94
            cookie = {
95
                item.split("=")[0]: item.split("=")[1]
96
                for item in check_item.get("cookie").split("; ")
97
            }
98
            session = requests.session()
99
            session.cookies.update(cookie)
100
            session.headers.update({"Referer": "https://www.baidu.com/"})
101
            tbs, user_name = self.valid(session)
102
            if tbs:
103
                tb_name_list = self.get_tieba_list(session)
104
                msg = f"帐号信息: {user_name}\n{self.sign(session, tb_name_list, tbs)}"
105
            else:
106
                msg = f"帐号信息: {user_name}\n签到状态: Cookie 可能过期"
107
            msg_all += msg + "\n\n"
108
        return msg_all
109
110
111
if __name__ == "__main__":
112
    _data = get_data()
113
    _check_items = _data.get("TIEBA", [])
114
    result = Tieba(check_items=_check_items).main()
115
    send("百度贴吧", result)
116