ck_haidilao.Haidilao.checkin()   B
last analyzed

Complexity

Conditions 7

Size

Total Lines 61
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 51
nop 2
dl 0
loc 61
rs 7.2036
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", headers=headers, json=login_data
44
            ).json()
45
            if not login_res["success"]:
46
                return "登录失败"
47
            data = login_res["data"]
48
        except json.decoder.JSONDecodeError:
49
            return "登录请求失败"
50
51
        headers["_HAIDILAO_APP_TOKEN"] = data["token"]
52
        headers["ReqType"] = "APPH5"
53
        headers[
54
            "Referer"
55
        ] = f'{url}app-sign-in/?SignInToken={data["token"]}&source=MiniApp'
56
57
        try:
58
            signin_res = requests.post(
59
                f"{url}activity/wxapp/signin/signin",
60
                headers=headers,
61
                json={"signinSource": "MiniApp"},
62
            ).json()
63
            if "请勿重复操作" in signin_res["msg"]:
64
                return "今日签到过了"
65
        except json.decoder.JSONDecodeError:
66
            return "签到请求失败"
67
68
        try:
69
            fragment_res = requests.post(
70
                f"{url}activity/wxapp/signin/queryFragment", headers=headers
71
            ).json()
72
            if fragment_res["success"]:
73
                return (
74
                    f'账号:{data["name"]} 签到成功\n' f'碎片余额:{fragment_res["data"]["total"]}'
75
                )
76
        except json.decoder.JSONDecodeError:
77
            return "查询请求失败"
78
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