Skip to content

Commit 1a767d2

Browse files
committed
2 parents 46bdae0 + 3329346 commit 1a767d2

File tree

160 files changed

+12924
-2357
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

160 files changed

+12924
-2357
lines changed

.github/workflows/github-pages.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: github-pages
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
workflow_run:
9+
workflows: [run-cron]
10+
types:
11+
- completed
12+
13+
workflow_dispatch:
14+
15+
jobs:
16+
build-and-deploy:
17+
runs-on: ubuntu-20.04
18+
steps:
19+
- name: Checkout Repository
20+
uses: actions/checkout@v3
21+
22+
- name: Install dependencies and build
23+
uses: actions/setup-node@v3
24+
with:
25+
node-version: '14'
26+
- run: npm ci
27+
- run: npm run build
28+
29+
- name: Check GitHub Pages status
30+
uses: crazy-max/ghaction-github-status@v3
31+
with:
32+
pages_threshold: major_outage
33+
34+
- name: Deploy to gh-pages branch
35+
if: success()
36+
uses: crazy-max/ghaction-github-pages@v3
37+
with:
38+
target_branch: gh-pages
39+
build_dir: build
40+
env:
41+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/run-cron.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: run-cron
2+
3+
on:
4+
schedule:
5+
- cron: '0 12 * * 0'
6+
7+
workflow_dispatch:
8+
9+
jobs:
10+
update:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
- uses: actions/setup-python@v4
15+
with:
16+
python-version: '3.x'
17+
architecture: 'x64'
18+
- run: |
19+
python -m pip install --upgrade pip
20+
pip install requests python-leetcode
21+
- run: |
22+
python cron/update_questions.py
23+
env:
24+
LEETCODE_SESSION_TOKEN: ${{ secrets.LEETCODE_SESSION_TOKEN }}
25+
- uses: stefanzweifel/git-auto-commit-action@v4
26+
with:
27+
commit_message: Update questions via run-cron GitHub Action

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@
2222
npm-debug.log*
2323
yarn-debug.log*
2424
yarn-error.log*
25+
26+
__pycache__

README.md

Lines changed: 7 additions & 66 deletions

cron/update_questions.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import os
2+
import json
3+
import leetcode
4+
import leetcode.auth
5+
from datetime import datetime
6+
from leetcode.rest import ApiException
7+
8+
9+
def create_leetcode_api():
10+
LEETCODE_SESSION_TOKEN = os.environ.get("LEETCODE_SESSION_TOKEN")
11+
csrf_token = leetcode.auth.get_csrf_cookie(LEETCODE_SESSION_TOKEN)
12+
13+
configuration = leetcode.Configuration()
14+
15+
configuration.api_key["x-csrftoken"] = csrf_token
16+
configuration.api_key["csrftoken"] = csrf_token
17+
configuration.api_key["LEETCODE_SESSION"] = LEETCODE_SESSION_TOKEN
18+
configuration.api_key["Referer"] = "https://leetcode.com"
19+
configuration.debug = False
20+
21+
return leetcode.DefaultApi(leetcode.ApiClient(configuration))
22+
23+
24+
def get_question_metadata(api, title_slug):
25+
graphql_request = leetcode.GraphqlQuery(
26+
query='''query questionData($titleSlug: String!) {
27+
question(titleSlug: $titleSlug) {
28+
title
29+
difficulty
30+
companyTagStats
31+
isPaidOnly
32+
}
33+
}
34+
''',
35+
variables=leetcode.GraphqlQueryGetQuestionDetailVariables(
36+
title_slug=title_slug)
37+
)
38+
39+
try:
40+
response = api.graphql_post(body=graphql_request)
41+
return response
42+
except ApiException as e:
43+
print(
44+
f'Exception occurred when contacting the Leetcode GraphQL API: ${e}')
45+
exit()
46+
47+
48+
def construct_company_tag_list(company_tags_json, sections):
49+
companies = []
50+
51+
for section in sections:
52+
for company in company_tags_json[section]:
53+
companies.append({
54+
"name": company["name"],
55+
"slug": company["slug"],
56+
"frequency": company["timesEncountered"]
57+
})
58+
59+
return sorted(companies, key=lambda d: d['frequency'], reverse=True)
60+
61+
62+
def update_question_metadata(question, response):
63+
print(f'''🔄 Updating question metadata for {question["title"]}''')
64+
65+
question_title = response.data.question.title
66+
question_difficulty = response.data.question.difficulty
67+
question_company_tags = json.loads(
68+
response.data.question.company_tag_stats)
69+
question_is_premium = response.data.question.is_paid_only
70+
71+
# Retrieve companies who have asked this question for the following two
72+
# company_tag_stat sections:
73+
# 1. 0-6 months
74+
# 2. 6 months to 1 year
75+
companies = construct_company_tag_list(
76+
question_company_tags, ["1", "2"])
77+
78+
question["title"] = question_title
79+
question["difficulty"] = question_difficulty
80+
question["companies"] = companies
81+
question["premium"] = question_is_premium
82+
83+
84+
def read_questions(file_name):
85+
print(f"💾 Loading {file_name}")
86+
87+
try:
88+
with open(file_name, "r") as file:
89+
questions = json.load(file)
90+
print(f"✅ Finished loading {file_name}")
91+
return questions
92+
except Exception as e:
93+
print(
94+
f"❌ Exception occurred when reading {file_name}: {e}")
95+
exit()
96+
97+
98+
def write_questions(file_name, questions):
99+
print(f"💾 Updating {file_name}")
100+
101+
try:
102+
with open(file_name, "w") as file:
103+
questions["updated"] = str(datetime.now().isoformat())
104+
json.dump(questions, file, indent=2)
105+
print(f"✅ Finished updating {file_name}")
106+
except Exception as e:
107+
print(
108+
f"❌ Exception occurred when writing {file_name}: {e}")
109+
exit()
110+
111+
112+
def main(file_name):
113+
api = create_leetcode_api()
114+
questions = read_questions(file_name)
115+
116+
for question in questions["data"]:
117+
title_slug = question["slug"]
118+
119+
response = get_question_metadata(api, title_slug)
120+
121+
update_question_metadata(question, response)
122+
123+
write_questions(file_name, questions)
124+
125+
126+
if __name__ == "__main__":
127+
file_name = os.getcwd() + "/src/data/questions.json"
128+
startTime = datetime.now()
129+
130+
main(file_name)
131+
132+
print(f"⏱️ Data updated in {datetime.now() - startTime} seconds")

package-lock.json

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"react-ga": "^2.7.0",
1818
"react-icons": "^3.11.0",
1919
"react-markdown": "^4.3.1",
20-
"react-scripts": "4.0.0",
20+
"react-scripts": "^4.0.0",
2121
"react-scroll": "^1.8.0",
2222
"react-table": "^7.6.3",
2323
"react-test-renderer": "^16.14.0",

public/assets/icons/Apple.png

-1.57 KB
Binary file not shown.

public/assets/icons/Facebook.png

-1.8 KB
Binary file not shown.

public/static/icons/accenture.png

2.83 KB

0 commit comments

Comments
 (0)