Passed
Branch master (0bcdb7)
by Leon
02:35
created

ck_haidilao.Haidilao.checkin()   B

Complexity

Conditions 7

Size

Total Lines 61
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 52
nop 2
dl 0
loc 61
rs 7.1709
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
# -*- coding: utf-8 -*-
2
"""
3
cron: 20 8 * * *
4
new Env('海底捞会员签到');
5
"""
6
7
import json
8
9
import requests
10
11
from notify_mtr import send
12
from utils import get_data
13
14
15
class Haidilao:
16
    def __init__(self, check_items):
17
        self.check_items = check_items
18
19
    @staticmethod
20
    def checkin(openid, uid):
21
        headers = {
22
            "Host": "superapp-public.kiwa-tech.com",
23
            "Content-Length": "115",
24
            "appId": "15",
25
            "content-type": "application/json",
26
            "Accept-Encoding": "gzip,compress,br,deflate",
27
            "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 15_5 like Mac OS X) "
28
            "AppleWebKit/605.1.15 (KHTML, like Gecko) "
29
            "Mobile MicroMessenger NetType/4G Language/en miniProgram",
30
            "Referer": "https://servicewechat.com/wx1ddeb67115f30d1a/14/page-frame.html",
31
        }
32
        login_data = {
33
            "openId": openid,
34
            "country": "CN",
35
            "uid": uid,
36
            "type": 1,
37
            "codeType": 1,
38
        }
39
        url = "https://superapp-public.kiwa-tech.com/"
40
41
        try:
42
            login_res = requests.post(
43
                f"{url}login/thirdCommLogin",
44
                headers=headers,
45
                json=login_data,
46
            ).json()
47
            if not login_res["success"]:
48
                return "登录失败"
49
            data = login_res["data"]
50
        except json.decoder.JSONDecodeError:
51
            return "登录请求失败"
52
53
        headers["_HAIDILAO_APP_TOKEN"] = data["token"]
54
        headers["ReqType"] = "APPH5"
55
        headers[
56
            "Referer"
57
        ] = f'{url}app-sign-in/?SignInToken={data["token"]}&source=MiniApp'
58
59
        try:
60
            signin_res = requests.post(
61
                f"{url}activity/wxapp/signin/signin",
62
                headers=headers,
63
                json={"signinSource": "MiniApp"},
64
            ).json()
65
            if "请勿重复操作" in signin_res["msg"]:
66
                return "今日签到过了"
67
        except json.decoder.JSONDecodeError:
68
            return "签到请求失败"
69
70
        try:
71
            fragment_res = requests.post(
72
                f"{url}activity/wxapp/signin/queryFragment", headers=headers
73
            ).json()
74
            if fragment_res["success"]:
75
                return (
76
                    f'账号:{data["name"]} 签到成功\n' f'碎片余额:{fragment_res["data"]["total"]}'
77
                )
78
        except json.decoder.JSONDecodeError:
79
            return "查询请求失败"
80
81
    def main(self):
82
        msg_all = ""
83
        for check_item in self.check_items:
84
            msg = self.checkin(
85
                openid=str(check_item.get("openid")), uid=str(check_item.get("uid"))
86
            )
87
            msg_all += msg + "\n\n"
88
        return msg_all
89
90
91
if __name__ == "__main__":
92
    _data = get_data()
93
    _check_items = _data.get("HAIDILAO", [])
94
    result = Haidilao(check_items=_check_items).main()
95
    send("海底捞会员签到", result)
96