File size: 2,150 Bytes
58e7ec3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import requests
from pathlib import Path

from utils import *


class Checker:

    def __init__(
        self, 
        url, 
        app_id, 
        app_secret, 
        timeout = 5
    ):
        
        self.url = url
        self.headers = {
            "app_id" : app_id,
            "app_secret" : app_secret
        }
        self.timeout = timeout
        self.saved = {}
    
    @classmethod
    def from_file(
        self, 
        filename, 
        timeout=5
    ):
        p = Path(filename)
        if not p.is_file():
            raise FileNotFoundError(f"File {str(p.absolute())} not found.")
        p = str(p.absolute())
        with open(p, "r") as f:
            contents = f.readlines()[: 3]
        if len(contents) < 3:
            raise MissingInfoException(f"File {p} is not complete.")
        url, app_id, app_secret = [l.strip() for l in contents]
        return Checker(url, app_id, app_secret, timeout)

    def query(
        self, 
        code, 
        issue
    ):
        winning = self.saved.get((code, issue), None)
        if not winning:
            payload = {
                "code" : code,
                "expect" : issue
            }
            r = requests.get(self.url, params=payload, headers=self.headers, timeout=self.timeout)
            if  r.status_code != requests.codes.ok:
                raise requests.RequestException(f"网络请求出错,code: {r.status_code},请检查网络连接。")
            js = r.json()
            if js["code"] != 1:
                raise requests.RequestException(f"没有查询到期号为{issue}的彩票开奖信息,请确认是否已开奖。")
            winning = js["data"]["openCode"]
            if not winning:
                raise MissingInfoException("接口返回数据中未解析到开奖号码。")
            winning = winning_process(winning, code)
            self.saved[(code, issue)] = winning
        return winning
    
    def __call__(
        self, 
        code, 
        issue, 
        numbers
    ):
        winning = self.query(code, issue)
        hits = hit_check(numbers, winning)
        return hits, winning