-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuncher.py
107 lines (87 loc) · 3.68 KB
/
puncher.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
106
107
import typing as t
from http import HTTPStatus
import requests
from bs4 import BeautifulSoup
class FemasPuncher:
def __init__(self, account: str, password: str, subdomain: str):
self.account = account
self.password = password
self.subdomain = subdomain
def _api(self, api_name: str) -> str:
return f"https://femascloud.com/{self.subdomain}" + api_name
def __enter__(self):
self.session = requests.Session()
r = self.session.get(self._api("/accounts/login"))
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', # noqa
'Accept-Language': 'zh-TW,zh;q=0.9',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
'Origin': 'https://femascloud.com',
'Referer': f'https://femascloud.com/{self.subdomain}/accounts/login', # noqa
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-User': '?1',
'Upgrade-Insecure-Requests': '1',
}
data = {
'data[Account][username]': self.account,
'data[Account][passwd]': self.password,
'data[remember]': '0',
}
r = self.session.post(
self._api("/Accounts/login"),
headers=headers,
data=data,
)
soup = BeautifulSoup(r.text, "html.parser")
self.user_id = soup.find(
"input",
{"name": "data[EboardBrowser][user_id]"},
).attrs["value"]
return self
def __exit__(self, type, value, traceback):
self.session.close()
def punch_in_myself(self):
self.punch_in(user_id=self.user_id)
def punch_out_myself(self):
self.punch_out(user_id=self.user_id)
def punch_in(self, user_id: t.Union[int, str]):
self._punch(user_id=user_id, action='S')
def punch_out(self, user_id: t.Union[int, str]):
self._punch(user_id=user_id, action='E')
def _punch(self, user_id: t.Union[int, str], action: str):
if action not in ['E', 'S']:
raise ValueError(f"action should be E or S but {action} is given")
headers = {
'Accept': 'text/javascript, text/html, application/xml, text/xml, */*', # noqa
'Accept-Language': 'zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-CN;q=0.6,ja;q=0.5', # noqa
'Connection': 'keep-alive',
'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Origin': 'https://femascloud.com',
'Referer': f'https://femascloud.com/{self.subdomain}/users/main?from=/Accounts/login?ext=html', # noqa
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'X-Prototype-Version': '1.7',
'X-Requested-With': 'XMLHttpRequest',
'X-Update': 'clock_listing',
}
data = {
'_method': 'POST',
'data[ClockRecord][user_id]': str(user_id),
'data[AttRecord][user_id]': str(user_id),
'data[ClockRecord][shift_id]': '10',
'data[ClockRecord][period]': '1',
'data[ClockRecord][clock_type]': action,
'data[ClockRecord][latitude]': '',
'data[ClockRecord][longitude]': '',
}
r = self.session.post(
self._api("/users/clock_listing"),
headers=headers,
data=data,
)
if r.status_code != HTTPStatus.OK:
raise requests.exceptions.HTTPError(f"status {r.status_code}")