Skip to content

Commit 0d7a741

Browse files
committed
feat: Implement main functionality
1 parent 938f767 commit 0d7a741

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

scripts/leetcode.bash

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env bash
2+
3+
source scripts/common.bash
4+
source scripts/leetcode_helper.bash
5+
6+
function main() {
7+
load_env_vars
8+
9+
# Check if required commands are installed
10+
check_command "jq"
11+
check_command "bash"
12+
13+
# Check if the problem title-slug or URL is provided
14+
if [ "$#" -eq 1 ]; then
15+
input="$1"
16+
if [[ "$input" == *"https://leetcode.com/problems/"* ]]; then
17+
# If the input is a LeetCode URL
18+
problem_slug=$(extract_problem_name "$input")
19+
else
20+
# If the input is already a title-slug
21+
problem_slug="$input"
22+
fi
23+
else
24+
echo "Please provide the LeetCode problem title-slug or URL."
25+
exit 1
26+
fi
27+
28+
echo "Problem Slug: $problem_slug"
29+
30+
# GraphQL query to fetch problem details
31+
query=$(make_query "$problem_slug")
32+
33+
echo "Requesting problem details from LeetCode GraphQL API..."
34+
35+
# Send a POST request to the LeetCode GraphQL API
36+
response=$(request "$query")
37+
38+
# Check if the response contains valid data
39+
if ! echo -E "$response" | jq -e '.data.question' >/dev/null 2>&1; then
40+
echo -E "$response"
41+
echo "Failed to receive a valid response from the LeetCode API. Exiting the script."
42+
exit 1
43+
fi
44+
45+
echo "Received problem details response from LeetCode."
46+
47+
# Create the file
48+
49+
create_file "$response"
50+
}
51+
52+
# Call the main function
53+
main "$@"

0 commit comments

Comments
 (0)