Skip to content

Commit e9037e2

Browse files
author
xdcn4066
committed
修复get_submission失败的问题
1 parent 50d931b commit e9037e2

File tree

1 file changed

+85
-40
lines changed

1 file changed

+85
-40
lines changed

autoload/leetcode.py

Lines changed: 85 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
LC_TEST = LC_BASE + '/problems/{slug}/interpret_solution/'
3939
LC_SUBMIT = LC_BASE + '/problems/{slug}/submit/'
4040
LC_SUBMISSIONS = LC_BASE + '/api/submissions/{slug}'
41-
LC_SUBMISSION = LC_BASE + '/submissions/detail/{submission}/'
41+
LC_SUBMISSION = LC_BASE + '/graphql'
4242
LC_CHECK = LC_BASE + '/submissions/detail/{submission}/check/'
4343
LC_PROBLEM_SET_ALL = LC_BASE + '/problemset/all/'
4444
LC_PROGRESS_ALL = LC_BASE + '/api/progress/all/'
@@ -71,7 +71,8 @@ def _make_headers():
7171
headers = {'Origin': LC_BASE,
7272
'Referer': LC_BASE,
7373
'X-Requested-With': 'XMLHttpRequest',
74-
'X-CSRFToken': session.cookies.get('csrftoken', '')}
74+
'X-CSRFToken': session.cookies.get('csrftoken', ''),
75+
}
7576
return headers
7677

7778

@@ -500,35 +501,81 @@ def _unescape(s):
500501
return s.encode().decode('unicode_escape')
501502

502503

504+
def make_submission_post_data(sid):
505+
queryfunc = """
506+
query mySubmissionDetail($id: ID!) {
507+
submissionDetail(submissionId: $id) {
508+
id
509+
code
510+
runtime
511+
memory
512+
rawMemory
513+
statusDisplay
514+
timestamp
515+
lang
516+
passedTestCaseCnt
517+
totalTestCaseCnt
518+
sourceUrl
519+
question {
520+
titleSlug
521+
title
522+
translatedTitle
523+
questionId
524+
__typename
525+
}
526+
... on GeneralSubmissionNode {
527+
outputDetail {
528+
codeOutput
529+
expectedOutput
530+
input
531+
compileError
532+
runtimeError
533+
lastTestcase
534+
__typename
535+
}
536+
__typename
537+
}
538+
submissionComment {
539+
comment
540+
flagType
541+
__typename
542+
}
543+
__typename
544+
}
545+
}
546+
"""
547+
data = {'operationName': 'mySubmissionDetail', 'variables': {'id': sid}, 'query': queryfunc}
548+
import json
549+
return json.dumps(data)
550+
551+
503552
def get_submission(sid):
504553
assert is_login()
505-
headers = _make_headers()
506-
url = LC_SUBMISSION.format(submission=sid)
507-
_echoerr(url)
554+
headers = {k:v for k, v in _make_headers().items()}
555+
headers['content-type'] = 'application/json'
556+
url = LC_SUBMISSION
508557
log.info('get submission request: url="%s" headers="%s"', url, headers)
509-
res = session.get(url, headers=headers)
558+
res = session.post(url, headers=headers, data=make_submission_post_data(sid))
510559
log.info('get submission response: status="%s" body="%s"', res.status_code, res.text)
560+
_echoerr(res.status_code)
511561
if res.status_code != 200:
512562
_echoerr('cannot find the submission: ' + sid)
513563
return None
514564

515565
# we need to parse the data from the Javascript snippet
516-
s = res.text
566+
detail = res.json()['data']['submissionDetail']
517567
submission = {
518568
'id': sid,
519-
'state': _status_to_name(int(_group1(re.search(r"status_code: parseInt\('([^']*)'", s),
520-
'not found'))),
521-
'state': _status_to_name(10),
522-
'runtime': _group1(re.search("runtime: '([^']*)'", s), 'not found'),
523-
'passed': _group1(re.search("total_correct : '([^']*)'", s), 'not found'),
524-
'total': _group1(re.search("total_testcases : '([^']*)'", s), 'not found'),
525-
'testcase': _split(_unescape(_group1(re.search("input : '([^']*)'", s), ''))),
526-
'answer': _split(_unescape(_group1(re.search("code_output : '([^']*)'", s), ''))),
527-
'expected_answer': _split(_unescape(_group1(re.search("expected_output : '([^']*)'", s),
528-
''))),
529-
'problem_id': _group1(re.search("questionId: '([^']*)'", s), 'not found'),
530-
'slug': _group1(re.search("editCodeUrl: '([^']*)'", s), '///').split('/')[2],
531-
'filetype': _group1(re.search("getLangDisplay: '([^']*)'", s), 'not found'),
569+
'state': detail['statusDisplay'],
570+
'runtime': detail['runtime'],
571+
'passed': detail['passedTestCaseCnt'],
572+
'total': detail['totalTestCaseCnt'],
573+
'testcase': [detail['outputDetail']['input']],
574+
'answer': [detail['outputDetail']['codeOutput']],
575+
'expected_answer': [detail['outputDetail']['expectedOutput']],
576+
'problem_id': detail['question']['questionId'],
577+
'slug': detail['question']['titleSlug'],
578+
'filetype': detail['lang'],
532579
'error': [],
533580
'stdout': [],
534581
}
@@ -540,30 +587,28 @@ def get_submission(sid):
540587
# to unscape the string, we do the trick '\\u0010'.encode().decode('unicode_escape') ==> '\n'
541588
# submission['code'] = _break_code_lines(_unescape(_group1(
542589
# re.search("submissionCode: '([^']*)'", s), '')))
543-
submission['code'] = _unescape_with_Chinese(
544-
_group1(re.search("submissionCode: '([^']*)'", s), ''))
545-
590+
submission['code'] = _unescape_with_Chinese(detail['code'])
546591

547-
dist_str = _unescape(_group1(re.search("runtimeDistributionFormatted: '([^']*)'", s),
548-
'{"distribution":[]}'))
549-
dist = json.loads(dist_str)['distribution']
550-
dist.reverse()
592+
#dist_str = _unescape(_group1(re.search("runtimeDistributionFormatted: '([^']*)'", s),
593+
# '{"distribution":[]}'))
594+
#dist = json.loads(dist_str)['distribution']
595+
#dist.reverse()
551596

552597
# the second key "runtime" is the runtime in milliseconds
553598
# we need to search from the position after the first "runtime" key
554-
prev_runtime = re.search("runtime: '([^']*)'", s)
555-
if not prev_runtime:
556-
my_runtime = 0
557-
else:
558-
my_runtime = int(_group1(re.search("runtime: '([^']*)'", s[prev_runtime.end():]), 0))
559-
560-
accum = 0
561-
for runtime, frequency in dist:
562-
accum += frequency
563-
if my_runtime >= int(runtime):
564-
break
565-
566-
submission['runtime_percentile'] = '{:.1f}%'.format(accum)
599+
#prev_runtime = re.search("runtime: '([^']*)'", s)
600+
#if not prev_runtime:
601+
# my_runtime = 0
602+
#else:
603+
# my_runtime = int(_group1(re.search("runtime: '([^']*)'", s[prev_runtime.end():]), 0))
604+
605+
#accum = 0
606+
#for runtime, frequency in dist:
607+
# accum += frequency
608+
# if my_runtime >= int(runtime):
609+
# break
610+
611+
submission['runtime_percentile'] = '{:.1f}%'.format(0)
567612
return submission
568613

569614

0 commit comments

Comments
 (0)