Skip to content

Commit 3341e75

Browse files
committed
Fix errors when the user is not a member
1 parent 30b0e25 commit 3341e75

File tree

1 file changed

+27
-87
lines changed

1 file changed

+27
-87
lines changed

autoload/leetcode.py

Lines changed: 27 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
LC_CHECK = LC_BASE + '/submissions/detail/{submission}/check/'
3232
LC_PROBLEM_SET_ALL = LC_BASE + '/problemset/all/'
3333

34+
EMPTY_FREQUENCIES = [0, 0, 0, 0, 0, 0, 0, 0]
35+
3436
session = None
3537
task_running = False
3638
task_done = False
@@ -221,11 +223,12 @@ def get_problem(slug):
221223
return None
222224

223225
q = res.json()['data']['question']
224-
if q is None:
226+
content = q['translatedContent'] or q['content']
227+
if content is None:
225228
_echoerr('cannot get the problem: {}'.format(slug))
226229
return None
227230

228-
soup = BeautifulSoup(q['translatedContent'] or q['content'], features='html.parser')
231+
soup = BeautifulSoup(content, features='html.parser')
229232
problem = {}
230233
problem['id'] = q['questionId']
231234
problem['title'] = q['title']
@@ -565,51 +568,9 @@ def get_problems_of_topic(topic_slug):
565568
stats
566569
difficulty
567570
isPaidOnly
568-
topicTags {
569-
name
570-
translatedName
571-
slug
572-
__typename
573-
}
574-
companyTags {
575-
name
576-
translatedName
577-
slug
578-
__typename
579-
}
580-
__typename
581571
}
582572
frequencies
583-
__typename
584-
}
585-
favoritesLists {
586-
publicFavorites {
587-
...favoriteFields
588-
__typename
589-
}
590-
privateFavorites {
591-
...favoriteFields
592-
__typename
593-
}
594-
__typename
595-
}
596-
}
597-
598-
fragment favoriteFields on FavoriteNode {
599-
idHash
600-
id
601-
name
602-
isPublicFavorite
603-
viewCount
604-
creator
605-
isWatched
606-
questions {
607-
questionId
608-
title
609-
titleSlug
610-
__typename
611573
}
612-
__typename
613574
}
614575
'''}
615576

@@ -624,10 +585,17 @@ def get_problems_of_topic(topic_slug):
624585

625586
if res.status_code != 200:
626587
_echoerr('cannot get problems of the topic')
627-
return None
588+
return {'topic_name': topic_slug, 'problems': []}
628589

629590
topic_tag = res.json()['data']['topicTag']
630-
id_to_frequency_map = json.loads(topic_tag['frequencies'])
591+
592+
if not topic_tag:
593+
return {'topic_name': topic_slug, 'problems': []}
594+
595+
if topic_tag['frequencies']:
596+
id_to_frequency_map = json.loads(topic_tag['frequencies'])
597+
else:
598+
id_to_frequency_map = {}
631599

632600
def process_problem(p):
633601
stats = json.loads(p['stats'])
@@ -642,7 +610,7 @@ def process_problem(p):
642610
'ac_rate': stats['totalAcceptedRaw'] / stats['totalSubmissionRaw'],
643611
'level': p['difficulty'],
644612
'favor': False,
645-
'frequency': id_to_frequency_map[p['questionId']]}
613+
'frequency': id_to_frequency_map.get(p['questionId'], 0)}
646614

647615
return {
648616
'topic_name': topic_tag['name'],
@@ -660,40 +628,10 @@ def get_problems_of_company(company_slug):
660628
frequencies
661629
questions {
662630
...questionFields
663-
__typename
664-
}
665-
__typename
666-
}
667-
favoritesLists {
668-
publicFavorites {
669-
...favoriteFields
670-
__typename
671631
}
672-
privateFavorites {
673-
...favoriteFields
674-
__typename
675-
}
676-
__typename
677632
}
678633
}
679634
680-
fragment favoriteFields on FavoriteNode {
681-
idHash
682-
id
683-
name
684-
isPublicFavorite
685-
viewCount
686-
creator
687-
isWatched
688-
questions {
689-
questionId
690-
title
691-
titleSlug
692-
__typename
693-
}
694-
__typename
695-
}
696-
697635
fragment questionFields on QuestionNode {
698636
status
699637
questionId
@@ -704,14 +642,7 @@ def get_problems_of_company(company_slug):
704642
stats
705643
difficulty
706644
isPaidOnly
707-
topicTags {
708-
name
709-
translatedName
710-
slug
711-
__typename
712-
}
713645
frequencyTimePeriod
714-
__typename
715646
}
716647
'''}
717648

@@ -727,10 +658,18 @@ def get_problems_of_company(company_slug):
727658

728659
if res.status_code != 200:
729660
_echoerr('cannot get problems of the company')
730-
return None
661+
return {'company_name': company_slug, 'problems': []}
731662

732663
company_tag = res.json()['data']['companyTag']
733-
id_to_frequency_map = json.loads(company_tag['frequencies'])
664+
665+
if not company_tag:
666+
_echoerr('cannot get problems of the company')
667+
return {'company_name': company_slug, 'problems': []}
668+
669+
if company_tag['frequencies']:
670+
id_to_frequency_map = json.loads(company_tag['frequencies'])
671+
else:
672+
id_to_frequency_map = {}
734673

735674
def process_problem(p):
736675
stats = json.loads(p['stats'])
@@ -745,7 +684,8 @@ def process_problem(p):
745684
'ac_rate': stats['totalAcceptedRaw'] / stats['totalSubmissionRaw'],
746685
'level': p['difficulty'],
747686
'favor': False,
748-
'frequencies': id_to_frequency_map[p['questionId']][4:]}
687+
'frequencies': id_to_frequency_map.get(p['questionId'],
688+
EMPTY_FREQUENCIES)[4:]}
749689

750690
return {
751691
'company_name': company_tag['name'],

0 commit comments

Comments
 (0)