ck_v2ex   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 69
dl 0
loc 105
rs 10
c 0
b 0
f 0
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A V2ex.__init__() 0 3 1
B V2ex.sign() 0 43 8
A V2ex.main() 0 31 3
1
# -*- coding: utf-8 -*-
2
"""
3
cron: 10 19 * * *
4
new Env('V2EX');
5
"""
6
7
import re
8
9
import requests
10
import urllib3
11
12
from notify_mtr import send
13
from utils import get_data
14
15
urllib3.disable_warnings()
16
17
18
class V2ex:
19
    def __init__(self, check_items):
20
        self.check_items = check_items
21
        self.url = "https://www.v2ex.com/mission/daily"
22
23
    def sign(self, session):
24
        msg = ""
25
26
        response = session.get(self.url, verify=False)
27
        pattern = (
28
            r"<input type=\"button\" class=\"super normal button\""
29
            r" value=\".*?\" onclick=\"location\.href = \'(.*?)\';\" />"
30
        )
31
        urls = re.findall(pattern, response.text)
32
        url = urls[0] if urls else None
33
        if url is None:
34
            return "cookie 可能过期"
35
        if url != "/balance":
36
            data = {"once": url.split("=")[-1]}
37
            session.get(
38
                f'https://www.v2ex.com{url.split("?")[0]}', verify=False, params=data
39
            )
40
41
        response = session.get("https://www.v2ex.com/balance", verify=False)
42
        totals = re.findall(
43
            r"<td class=\"d\" style=\"text-align: right;\">(\d+\.\d+)</td>",
44
            response.text,
45
        )
46
        total = totals[0] if totals else "签到失败"
47
        today = re.findall(
48
            r'<td class="d"><span class="gray">(.*?)</span></td>', response.text
49
        )
50
        today = today[0] if today else "签到失败"
51
52
        usernames = re.findall(
53
            r"<a href=\"/member/.*?\" class=\"top\">(.*?)</a>", response.text
54
        )
55
        username = usernames[0] if usernames else "用户名获取失败"
56
57
        msg += f"帐号信息: {username}\n今日签到: {today}\n帐号余额: {total}"
58
59
        response = session.get(url=self.url, verify=False)
60
        datas = re.findall(r"<div class=\"cell\">(.*?)天</div>", response.text)
61
        data = f"{datas[0]} 天" if datas else "获取连续签到天数失败"
62
        msg += f"\n签到天数: {data}"
63
64
        msg = msg.strip()
65
        return msg
66
67
    def main(self):
68
        msg_all = ""
69
        for check_item in self.check_items:
70
            cookie = {
71
                item.split("=")[0]: item.split("=")[1]
72
                for item in check_item.get("cookie").split("; ")
73
            }
74
75
            session = requests.session()
76
            if check_item.get("proxy", ""):
77
                proxies = {
78
                    "http": check_item.get("proxy", ""),
79
                    "https": check_item.get("proxy", ""),
80
                }
81
                session.proxies.update(proxies)
82
            session.cookies.update(cookie)
83
            session.headers.update(
84
                {
85
                    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
86
                    "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36",
87
                    "referer": self.url,
88
                    "accept": "text/html,application/xhtml+xml,application/xml;"
89
                    "q=0.9,image/webp,image/apng,*/*;"
90
                    "q=0.8,application/signed-exchange;v=b3;q=0.9",
91
                    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
92
                }
93
            )
94
95
            msg = self.sign(session)
96
            msg_all += msg + "\n\n"
97
        return msg_all
98
99
100
if __name__ == "__main__":
101
    _data = get_data()
102
    _check_items = _data.get("V2EX", [])
103
    result = V2ex(check_items=_check_items).main()
104
    send("V2EX", result)
105