-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitialize_docstring.sh
More file actions
executable file
·50 lines (38 loc) · 2.06 KB
/
initialize_docstring.sh
File metadata and controls
executable file
·50 lines (38 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
if [[ $# -eq 0 ]]; then
echo "Usage: ./initialize_docstring.sh <problem_id>"
exit 1
fi
PROBLEM_ID=$1
COOKIE_FILE="cookies.txt"
trap "rm -f $COOKIE_FILE" EXIT
echo "Getting CSRF token"
curl -c $COOKIE_FILE --silent "https://leetcode.com" &>/dev/null
CSRF_TOKEN=$(grep -E 'csrftoken\s+\w+' $COOKIE_FILE -o | cut -d $'\t' -f2)
echo "Getting all problems"
PROBLEMS=$(curl --silent https://leetcode.com/api/problems/all/ -H "cookie: csrftoken=$CSRF_TOKEN" -H "content-type: application/json" -H "accept: application/json" -H "authority: leetcode.com" -H "referer: https://leetcode.com" --compressed)
echo "Getting title slug of problem $PROBLEM_ID"
TITLE_SLUG=$(echo $PROBLEMS | jq -r ".stat_status_pairs | map(select(.stat.frontend_question_id == $PROBLEM_ID)) | .[0]? | .stat.question__title_slug")
if [[ -z $TITLE_SLUG || $TITLE_SLUG = "null" ]]; then
echo "Unable to find problem with ID: $PROBLEM_ID"
exit 1
fi
echo "Found problem title slug: $TITLE_SLUG"
echo "Getting problem details for problem $PROBLEM_ID, $TITLE_SLUG"
PROBLEM=$(curl --silent "https://leetcode.com/graphql" -H "cookie: csrftoken=$CSRF_TOKEN" -H "x-csrftoken: $CSRF_TOKEN" -H "content-type: application/json" -H "accept: application/json" -H "authority: leetcode.com" -H "referer: https://leetcode.com/" -d "{\"operationName\":\"getQuestionDetail\",\"variables\":{\"titleSlug\":\"$TITLE_SLUG\"},\"query\":\"query getQuestionDetail(\$titleSlug: String!) {\n question(titleSlug: \$titleSlug) {\n questionId\n questionFrontendId\n questionTitle\n content\n }\n}\n\"}")
echo
echo "Problem description docstring:"
echo
PROBLEM_TITLE=$(echo $PROBLEM | jq -r '.data.question.questionTitle')
PROBLEM_DESCRIPTION=$(echo $PROBLEM | jq -r '.data.question.content' | sed -e 's/<[^>]*>//g')
LINES=()
LINES+=("/**")
LINES+=(" * $PROBLEM_ID. $PROBLEM_TITLE")
LINES+=(" *")
for line in $PROBLEM_DESCRIPTION; do
LINES+=(" * $line")
done
LINES+=(" */")
printf "%s\n" "${LINES[@]}" | python3 -c 'import html, sys; [print(html.unescape(l), end="") for l in sys.stdin]'