Skip to content

Commit

Permalink
Register course with retry
Browse files Browse the repository at this point in the history
Remove failed course and retry
  • Loading branch information
tribela committed Feb 17, 2016
1 parent 57ba221 commit a886cc1
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions djuintra/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ def register_course(self, courses):
'local_get_time': local_get_time,
}

courses = list(courses)

for idx in range(30):
try:
data['curi_num{0}'.format(idx)] = courses[idx][0]
Expand All @@ -442,7 +444,20 @@ def register_course(self, courses):

if errors:
error_msgs = [error.text_content().strip() for error in errors]
raise ValueError(error_msgs)
failed_courses = self._collect_failed_courses(tree)
raise RegisterError(error_msgs, 0, failed_courses)

def register_course_recurse(self, courses):
courses = set(courses)
for retry_count in range(len(courses)):
try:
self.register_course(courses)
except RegisterError as e:
if not hasattr(e, 'failed_courses'):
raise e
courses -= set(e.failed_courses)
else:
break

def register_toeic(self):
"""Register simulated toeic
Expand Down Expand Up @@ -495,11 +510,27 @@ def _skip_change_pw(self, userid, userpw):
headers={'referer': self.URL_LOGIN}
)

@staticmethod
def _collect_failed_courses(tree):
table = tree.xpath('//div/table')[3]
trs = table.xpath('*/tr')[2:]
msgs = zip(trs[::2], trs[1::2])

results = []

for msg in msgs:
if msg[1].find('*[@bgcolor="red"]') is not None:
code = msg[0].find('*//input[@size="6"]').value
cls = msg[0].find('*//input[@size="2"]').value
results.append((code, cls))

return results;

def __repr__(self):
return '<{}: {}>'.format(self.__class__.__name__, self.userid)

@classmethod
def _get_error_code(cls, content):
@staticmethod
def _get_error_code(content):
tree = html.fromstring(content)
error = tree.xpath('//td')[0].text_content().strip()
code = int(re.search(r'\d+', error).group())
Expand All @@ -513,9 +544,10 @@ def _get_error_code(cls, content):

class RegisterError(Exception):

def __init__(self, msg, code):
def __init__(self, msg, code, failed_courses=None):
super(RegisterError, self).__init__(msg)
self.code = code
self.failed_courses = failed_courses

def __repr__(self):
return '<RegisterError code={}>'.format(self.code)

0 comments on commit a886cc1

Please sign in to comment.