Browse Source

changed to use SLogin module

master
myitinos 2 years ago
parent
commit
f14658b0b8
2 changed files with 108 additions and 75 deletions
  1. +28
    -25
      SCheck.py
  2. +80
    -50
      STract.py

+ 28
- 25
SCheck.py View File

@ -1,36 +1,39 @@
#!/usr/bin/env python3.6
import argparse # argparse
import requests # requests.Session
import time # time.time
from SLogin import SLogin
from time import time as now
URL = "http://sion.stikom-bali.ac.id/load_login.php"
class SCheck(object):
def __init__(self, nim, pin):
self.conn0 = SLogin(nim, pin)
self.conn1 = SLogin(nim, pin[::-1])
def connect(self):
self.conn0.login()
self.conn1.login()
return self
def is_both_equal(self):
return self.conn0.text == self.conn1.text
if __name__ == "__main__":
import argparse
def main():
parser = argparse.ArgumentParser()
parser.add_argument("nim", help="NIM to try login")
parser.add_argument("pin", help="PIN to try login")
args = parser.parse_args()
print('Sending requests...')
start_time = time.time()
with requests.Session() as session:
resp1 = session.post(URL, data={'uname': args.nim, 'passwd': args.pin})
resp1 = resp1.text
resp2 = session.post(URL, data={'uname': args.nim, 'passwd': args.pin[::-1]})
resp2 = resp2.text
print('Requests completed in %0.2f' % (time.time() - start_time))
if resp1 == resp2:
print('Got only one response, please check your NIM and PIN:')
print(resp1)
start_time = now()
checker = SCheck(args.nim, args.pin)
checker.connect()
if checker.is_both_equal():
print('Got same response, please check your NIM/PIN')
else:
print('First Response:')
print(resp1)
print('Second Response:')
print(resp2)
if __name__ == "__main__":
main()
print('Got two different result. Writing to conn0.txt and conn1.txt')
with open('conn0.txt', 'w') as outfile:
outfile.write(checker.conn0.text)
with open('conn1.txt', 'w') as outfile:
outfile.write(checker.conn1.text)

+ 80
- 50
STract.py View File

@ -1,61 +1,91 @@
#!/usr/bin/python3
import argparse
import requests
import time
from requests import Session
from SLogin import SLogin
def main():
start_time = time.time()
class STractNotLoggedIn(Exception):
pass
URL1 = "http://sion.stikom-bali.ac.id/load_login.php"
URL2 = "http://sion.stikom-bali.ac.id/reg/mhsscrdet.php"
OK_TXT = """<script language="javascript">window.location ='/reg/'</script>"""
class STract(object):
url = "http://180.250.7.188/reg/mhsscrdet.php"
def __init__(self, nim, pin):
self.connection = SLogin(nim, pin)
self.session = Session()
def __enter__(self):
return self
def __exit__(self, *args):
self.session.close()
def connect(self):
self.connection.login(session=self.session)
return self.connection.success
def extract(self):
txt0 = "<center><strong>[HASIL PENCARIAN DATA MAHASISWA]</strong></center><table><tr><th align='center'>NIM</th><th align='center'>NAMA</th><th align='center'>DETAIL</th></tr>"
txt1 = "<tr class='labelhitam' bgcolor=#CCCCCC><td width='120' align='center'>"
txt2 = "<tr class='labelhitam' bgcolor=#FFFFFF><td width='120' align='center'>"
txt3 = "</td><td width='450'>"
txt4 = "</td><td><a href='mhsdet.php?nim="
txt5 = "' class='linkhitam'>Detail</a></td></tr>"
txt6 = "</table>"
if self.connection.success:
data = self.session.post(self.url).text
data = data.replace(txt0, "")
data = data.replace(txt1, "")
data = data.replace(txt2, "")
data = data.replace(txt3, "\n")
data = data.replace(txt4, "\n")
data = data.replace(txt5, "\n")
data = data.replace(txt6, "")
data = data.split("\n")[:-1]
data = [int(t) if n % 3 == 0 else 0 for (n, t)
in enumerate(data)]
data = sorted(list(set(data)))
data.remove(0)
data = list(map(str, data))
return data
else:
raise STractNotLoggedIn("Please login first")
if __name__ == "__main__":
import argparse
from time import time as now
start_time = now()
parser = argparse.ArgumentParser()
parser.add_argument("nim", help="NIM to try login")
parser.add_argument("pin", help="PIN to try login")
parser.add_argument("--outfile", nargs=1, metavar="FILE", default=["temp.txt"], help="outfile target")
parser.add_argument("nim",
help="NIM to try login")
parser.add_argument("pin",
help="PIN to try login")
parser.add_argument("--outfile",
metavar="FILE",
default="temp.txt",
help="outfile target")
args = parser.parse_args()
print("Trying to login...")
s = requests.Session()
r = s.post(URL1, data={"uname": args.nim, "passwd": args.pin})
if not OK_TXT in r.text:
print("Wrong NIM / PIN please try again")
exit(1)
else:
print("Login succes, trying to extract data...")
TXT0 = "<center><strong>[HASIL PENCARIAN DATA MAHASISWA]</strong></center><table><tr><th align='center'>NIM</th><th align='center'>NAMA</th><th align='center'>DETAIL</th></tr>"
TXT1 = "<tr class='labelhitam' bgcolor=#CCCCCC><td width='120' align='center'>"
TXT2 = "<tr class='labelhitam' bgcolor=#FFFFFF><td width='120' align='center'>"
TXT3 = "</td><td width='450'>"
TXT4 = "</td><td><a href='mhsdet.php?nim="
TXT5 = "' class='linkhitam'>Detail</a></td></tr>"
TXT6 = "</table>"
data = str(s.get(URL2).text)
data = data.replace(TXT0, "")
data = data.replace(TXT1, "")
data = data.replace(TXT2, "")
data = data.replace(TXT3, "\n")
data = data.replace(TXT4, "\n")
data = data.replace(TXT5, "\n")
data = data.replace(TXT6, "")
data = data.split("\n")[:-1]
data = [int(t) if n % 3 == 0 else 0 for (n, t) in enumerate(data)]
data = sorted(list(set(data)))
data.remove(0)
with open(args.outfile[0], 'w') as outfile:
data = list(map(str, data))
outfile.write('\n'.join(data))
total_time = time.time() - start_time
print("Extracted {} data in {}s".format(len(data), total_time))
with STract(args.nim, args.pin) as extraction:
print('Trying to connect with {} {}'.format(args.nim, args.pin))
if extraction.connect():
print('Login success, extracing data...')
data = extraction.extract()
print('Extraced {} data'.format(len(data)))
with open(args.outfile, 'w') as output:
print('Writing data to output file...')
output.write("\n".join(data))
if __name__ == "__main__":
main()
print('Done in {:0.2f}s'.format(now() - start_time))
else:
print('Login failed')

Loading…
Cancel
Save