forked from ninochang/punchin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
105 lines (90 loc) · 2.95 KB
/
utils.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import requests
import click
import os
import sys
import re
ACCOUNT = os.environ['ACCOUNT']
PASSWORD = os.environ['PASSWORD']
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Language': 'zh-TW,zh;q=0.9',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Origin': 'https://femascloud.com',
'Pragma': 'no-cache',
'Referer': 'https://femascloud.com/swag/accounts/login',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-User': '?1',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36',
'sec-ch-ua': '"Chromium";v="104", " Not A;Brand";v="99", "Google Chrome";v="104"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
}
def initialize():
session = requests.Session()
session.head('https://femascloud.com/swag/accounts/login')
session.post(
'https://femascloud.com/swag/Accounts/login',
headers=headers,
data={
'data[Account][username]': ACCOUNT,
'data[Account][passwd]': PASSWORD,
'data[remember]': 0
},
cookies=session.cookies,
)
return session
def get_user_id(session: requests.Session):
PATTERN = re.compile(r'value=\"(?P<user_id>\w+)\" id=\"AttRecordUserId\"')
response = session.get(
'https://femascloud.com/swag/users/main?from=/Accounts/login?ext=html',
)
return next(
PATTERN.finditer(response.text)
)['user_id']
def punch_in():
print('punch in')
session = initialize()
uid = get_user_id(session)
session.post(
'https://femascloud.com/swag/users/clock_listing',
headers=headers,
data={
'_method': 'POST',
'data[ClockRecord][user_id]': uid,
'data[AttRecord][user_id]': uid,
'data[ClockRecord][shift_id]': '2',
'data[ClockRecord][period]': '1',
'data[ClockRecord][clock_type]': 'S',
},
cookies=session.cookies,
)
def punch_out():
print('punch out')
session = initialize()
uid = get_user_id(session)
session.post(
'https://femascloud.com/swag/users/clock_listing',
headers=headers,
data={
'_method': 'POST',
'data[ClockRecord][user_id]': uid,
'data[AttRecord][user_id]': uid,
'data[ClockRecord][shift_id]': '2',
'data[ClockRecord][period]': '1',
'data[ClockRecord][clock_type]': 'E',
},
cookies=session.cookies,
)
@click.command()
@click.option('--mode', default='in', help='in/out')
def attentance(mode: str=None):
{
'in': punch_in,
'out': punch_out,
}[mode]()
if __name__ == '__main__':
attentance()