Created
August 25, 2023 03:33
-
-
Save linnil1/2aaf421b8aa96a309463fce35fcea861 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import time | |
import json | |
import random | |
import requests | |
from pathlib import Path | |
from datetime import datetime | |
cookie_path = "./cookie.json" | |
user = { | |
'user': "", | |
'pass': "", | |
} | |
header = { | |
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8", | |
"Accept-Encoding": "gzip, deflate, br", | |
"Accept-Language": "en-US,zh-TW;q=0.8,en;q=0.5,zh;q=0.3", | |
"Cache-Control": "no-cache", | |
"Connection": "keep-alive", | |
"Host": "my.ntu.edu.tw", | |
"Pragma": "no-cache", | |
"Referer": "https://my.ntu.edu.tw/", | |
"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 (Windows NT 10.0; Win64; x64; rv:106.0) Gecko/20100101 Firefox/106.0", | |
} | |
def createHeader() -> dict: | |
cookie = json.load(open(cookie_path)) | |
jar = requests.cookies.RequestsCookieJar() | |
for k, v in cookie.items(): | |
jar.set(k, v, domain="my.ntu.edu.tw") | |
print(jar) | |
return { | |
"cookies": jar, | |
"headers": header, | |
} | |
def updateCookie(data: dict) -> None: | |
cookie = json.load(open(cookie_path)) | |
cookie.update(data) | |
json.dump(cookie, open(cookie_path, "w")) | |
def requestSignAPI(request_type : str = "") -> requests.Response: | |
""" | |
landing | |
* web | |
* type=1: Time | |
* type=3: Today's Signin and Signout Time | |
* type=4: History | |
* type=OTC: Allow OTC (overtime confirm?) | |
Signin | |
* type=6,t=1: Signin | |
* type=3 | |
* type=4 | |
* type=OTC | |
SignOut | |
* type=6,t=2: Signin | |
* type=3 | |
* type=4 | |
* type=OTC | |
""" | |
if request_type == "time": | |
data: dict = {'type': 1} | |
elif request_type == "is_alive": | |
data = {'type': 2} | |
elif request_type == "status": | |
data = {'type': 3} | |
elif request_type == "history": | |
data = {'type': 4, 'day': 7} | |
elif request_type == "extra_history": | |
data = {'type': 5} | |
elif request_type == "signin": | |
data = {'type': 6, 't': 1, "otA": 0} | |
elif request_type == "signout": | |
data = {'type': 6, 't': 2, "otA": 0} | |
elif request_type == "allow_overtime": | |
data = {'type': "OTC", 'Agr': 0} | |
elif request_type == "web": | |
rep = requests.get( | |
"https://my.ntu.edu.tw/mattend/ssi.aspx", | |
**createHeader(), | |
) | |
print(request_type, rep.text) | |
return rep | |
else: | |
raise ValueError | |
rep = requests.post( | |
"https://my.ntu.edu.tw/mattend/ajax/signInR2.ashx", | |
data=data, | |
**createHeader(), | |
) | |
print(request_type, rep.json()) | |
return rep | |
def landing() -> None: | |
requestSignAPI("web") | |
requestSignAPI("time") | |
requestSignAPI("status") | |
requestSignAPI("history") | |
requestSignAPI("allow_overtime") | |
def signOut() -> None: | |
requestSignAPI("signout") | |
requestSignAPI("status") | |
requestSignAPI("history") | |
requestSignAPI("allow_overtime") | |
def signIn() -> None: | |
requestSignAPI("signin") | |
requestSignAPI("status") | |
requestSignAPI("history") | |
requestSignAPI("allow_overtime") | |
def checkLogin() -> bool: | |
rep = requestSignAPI("is_alive") | |
return rep.json()['d'] == "1" | |
def getCookieViaLogin() -> dict: | |
s = requests.Session() | |
meta = createHeader() | |
s.cookies = meta['cookies'] | |
s.headers.update(meta['headers']) | |
rep = s.get("https://my.ntu.edu.tw/mattend/ssi.aspx?type=login") | |
assert rep.ok | |
rep = s.post("https://web2.cc.ntu.edu.tw/p/s/login2/p1.php", data={ | |
'Submit': "登入", | |
**user, | |
}) | |
assert rep.ok | |
return dict(s.cookies) | |
def main() -> None: | |
print(datetime.now().strftime("%c")) | |
print(sys.argv) | |
# random delay time | |
delay_sec = random.randint(0, 15 * 60) | |
time.sleep(delay_sec) | |
print(datetime.now().strftime("%c")) | |
# init cookie if not exists | |
if not Path(cookie_path).exists(): | |
json.dump({}, open(cookie_path, "w")) | |
# login if not available | |
if not checkLogin(): | |
cookie = getCookieViaLogin() | |
updateCookie(cookie) | |
# load first page | |
landing() | |
time.sleep(3) | |
assert len(sys.argv) == 2 | |
command = sys.argv[1] | |
# signin/singout | |
if command == "in": | |
print("In") | |
signIn() | |
elif command == "out": | |
print("Out") | |
signOut() | |
else: | |
raise NotImplementedError | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment