Last active
April 24, 2016 16:04
-
-
Save iomz/3169837 to your computer and use it in GitHub Desktop.
gist: 2383539 in Python
This file contains hidden or 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# SFC-SFS "Report for overnight stay" script | |
# Last modified: 4/12/2016 | |
# | |
# **Fisrt complete the values below** | |
# =begin=========================================================== | |
login = "" # Ex. "s58000yf" | |
passwd = "" # Ex. "yukichifukuzawa" (passwd is hardcoded here) | |
phone = "" # Ex. "0X0-XXX-XXXXX" | |
home = "" # Ex. "0466-49-3411" | |
# Time to go home next day | |
time = "" # Ex. "9:00" | |
# Choose a corresponfing value for the building you stay | |
[Kappa,Epsilon,Iota,Omicron,Lambda,Delta,Tau,Zeta,Nu,Omega,Other] \ | |
= ["1","2","3","4","5","6","7","8","9","10","11"] | |
bldg = # Ex. "1" or Kappa | |
# Floor options varies depending on the building you stay | |
# Kappa, Epsilon, Iota, Omicron, Lambda: [1,2,3,4,5] | |
# Delta: [n1,n2,s1,s2] | |
# Tau, Zeta: [1,2,3] | |
# Nu: [1,2,3,4,5] (IIJ,docomo,DNP,Tateuchi,Mori for each) | |
# Omega: [1,2] | |
# Other: [1] | |
floor = "" # Ex. "1" | |
# Room number identifier | |
room = "" # Ex. "11" | |
# Whitespaces must be escaped | |
reason = u"研究活動のため" # Ex. "Term\ project" | |
# 1. Go to your class page on SFC-SFS | |
# 2. Find 'ks=XXXXX&yc=XXXX_XXXXX' in URI | |
ks = "" # Ex. "00001" | |
yc = "" # Ex. "2012_00001" | |
# CGI path | |
login_cgi = "https://vu.sfc.keio.ac.jp/sfc-sfs/login.cgi" | |
stay_cgi = "https://vu.sfc.keio.ac.jp/sfc-sfs/sfs_class/stay/stay_input.cgi" | |
# =end============================================================= | |
import re | |
import requests | |
import sys | |
def main(): | |
try: | |
r = requests.post(login_cgi, data={'u_login': login, 'u_pass': passwd}) | |
enc_id = re.sub('^.+id=|&type=.+$', '', r.text) | |
pass | |
except: | |
print "# Couldn't establish a connection with SFC-SFS" | |
sys.exit(1) | |
try: | |
reason_enc = reason.encode("eucjp") | |
r = requests.post(stay_cgi, data={ \ | |
'stay_phone': phone, \ | |
'stay_p_phone': home, \ | |
'stay_time': time, \ | |
'selectRoom': bldg, \ | |
'selectFloor': floor, \ | |
'stay_room_other': room, \ | |
'stay_reason': reason_enc, \ | |
'ks': ks, \ | |
'yc': yc, \ | |
'mode': 'submit', \ | |
'enc_id': enc_id \ | |
}) | |
except: | |
print "# Couldn't submit to SFC-SFS! There might be a syntax error." | |
print "Unexpected error:", sys.exc_info() | |
sys.exit(2) | |
try: | |
r = requests.post(stay_cgi, data={ \ | |
'ks': ks, \ | |
'yc': yc, \ | |
'mode': u'オンライン残留届'.encode("eucjp"),\ | |
'enc_id': enc_id, \ | |
'lang': 'en' \ | |
}) | |
except: | |
print "# Couldn't submit to SFC-SFS! There might be a syntax error." | |
print "Unexpected error:", sys.exc_info() | |
sys.exit(3) | |
#r.encoding = 'euc-jp' | |
r.encoding = r.apparent_encoding | |
for i, l in enumerate(r.text.encode('utf-8').split("\n")): | |
if i == 105: | |
if u'本日の残留届'.encode('utf-8') in l: | |
print re.sub('<p>', "\n", l) | |
else: | |
print "# Something went wrong." | |
sys.exit(4) | |
if 105 < i and i < 115: | |
print l | |
print "# Successfully submitted!" | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment