Skip to content

Instantly share code, notes, and snippets.

@jamornsriwasansak
Created August 16, 2014 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamornsriwasansak/6be81737bc59b263abe2 to your computer and use it in GitHub Desktop.
Save jamornsriwasansak/6be81737bc59b263abe2 to your computer and use it in GitHub Desktop.
Reg chula warrior. Cri evri tim.
"""
Assuming, you launch reg_warrior from this file.
For first time. Setting up is required.
The modules in needed are
BeautifulSoup : Expand the features of html management in python
pygame : Play some files
The file in needed is
alert.mp3 : Your own alert sound
Placing in same level of dir with this file.
The variables' values in needed are
request_site : Search page of regchula
course_id : Your course number
For each time you run you need to obtain cookie values
(Which will appears in chrome debugger tool under the tab resource after you logged in to reg chula)
JSESSIONID
sto-id-12345-reg-sg : 12345 Might be other numbers
During the run, if you hear your setup sound raising, you need to register the course on your own as fast as possible.
Note : This reg_war.py is not against reg chula regulation about login since, we never login and register subject from this program.
You need to login and register subject on your own.
"""
from bs4 import BeautifulSoup
from HTMLParser import HTMLParser
import time
import wave
import requests
import sys
import pygame
import webbrowser
from subprocess import call
cookies = {}
cookies['JSESSIONID'] = '0000nCCC322jk6b123456I9KDF4k:-1'
cookies['sto-id-12345-reg-sg'] = 'VIXCKXMBXAXC'
course_id = 2223041
request_site = 'https://www.reg.chula.ac.th/servlet/com.dtm.chula.cs.servlet.QueryCourseScheduleNew.CourseScheduleDtlNewServlet?courseNo=' + str(course_id) + '&studyProgram=S'
add_remove_subject_site = 'https://www.reg.chula.ac.th/cu/reg/register/maintaincourse/RegisterAdviserStudent.html'
delay_between_each_request = 1
alert_interval = 60
class RegTableParser(HTMLParser):
def __init__(self):
self.row_count = 0
self.inside_row = False
self.col_count = 0
self.inside_col = False
self.seat_available = False
HTMLParser.__init__(self)
def handle_starttag(self, tag, attrs):
if (tag == 'tr'):
self.inside_row = True
self.col_count = 0
elif (tag == 'td'):
self.inside_col = True
def handle_endtag(self, tag):
if (tag == 'tr'):
self.inside_row = False
self.row_count += 1
elif (tag == 'td'):
self.inside_col = False
self.col_count += 1
def handle_data(self, data):
clean_data = data.strip()
if ('/' in clean_data and self.row_count % 2 == 0 and self.col_count >= 8):
occupied, full = clean_data.split('/')
print occupied
print full
if (int(occupied) < int(full)):
self.seat_available = True or self.seat_available
else:
self.seat_available = False or self.seat_available
print self.seat_available
class RegWarrior():
def __init__(self):
pygame.init()
#incase of some computer without pygame lib
def os_based_alert(self):
if sys.platform == 'linux2':
call(["xdg-open","alert.mp3"])
elif sys.platform == 'darwin':
call(["afplay","alert.mp3"])
def pygame_based_alert(self):
pygame.mixer.music.load("alert.mp3")
pygame.mixer.music.play()
time.sleep(alert_interval)
def open_add_remove_site(self):
webbrowser.open_new(add_remove_subject_site)
def warcry(self):
while (True):
if (self.cry()):
#auto open browser code remove due to overhead
#self.open_add_remove_site()
self.pygame_based_alert()
time.sleep(alert_interval)
else:
print 'No Luck'
time.sleep(delay_between_each_request)
def cry(self):
print "Crying"
print request_site
r = requests.get(request_site, cookies=cookies)
html = r.text
soup = BeautifulSoup(html)
parser = RegTableParser()
parser.feed(str(soup.find(id="Table3")))
return parser.seat_available
if (__name__ == "__main__"):
warrior = RegWarrior()
warrior.warcry()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment