Skip to content

Commit 23bbd08

Browse files
committed
replace the xidel by jq fix haoel#248
1 parent af3bc24 commit 23bbd08

File tree

2 files changed

+92
-31
lines changed

2 files changed

+92
-31
lines changed

scripts/comments.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ get_question_slug ${leetcode_url}
7373

7474

7575
TRUE_CMD=`which true`
76-
xidel=`type -P xidel || ${TRUE_CMD}`
77-
if [ -z "${xidel}" ]; then
78-
echo "xidel not found !"
79-
install_xidel
76+
JQ=`type -P jq || ${TRUE_CMD}`
77+
if [ -z "${JQ}" ]; then
78+
echo "jq command is not found !"
79+
install_jq
8080
fi
8181

8282
#grab the problem information

scripts/lib/query_problem.sh

Lines changed: 88 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,8 @@ function get_question_slug()
1111
QUESTION_TITLE_SLUG=$(echo $QUESTION_TITLE_SLUG | sed 's/\/.*//g') # remove the needless url path
1212
}
1313

14-
function query_problem()
14+
function query_problem_xidel()
1515
{
16-
TMP_JSON_FILE=tmp.json
17-
18-
# curl -s "https://leetcode.com/graphql?query=query%20getQuestionDetail(%24titleSlug%3A%20String!)%20%7B%0A%20%20isCurrentUserAuthenticated%0A%20%20question(titleSlug%3A%20%24titleSlug)%20%7B%0A%20%20%20%20questionId%0A%20%20%20%20questionFrontendId%0A%20%20%20%20questionTitle%0A%20%20%20%20translatedTitle%0A%20%20%20%20questionTitleSlug%0A%20%20%20%20content%0A%20%20%20%20translatedContent%0A%20%20%20%20difficulty%0A%20%20%20%20stats%0A%20%20%20%20contributors%0A%20%20%20%20similarQuestions%0A%20%20%20%20discussUrl%0A%20%20%20%20mysqlSchemas%0A%20%20%20%20randomQuestionUrl%0A%20%20%20%20sessionId%0A%20%20%20%20categoryTitle%0A%20%20%20%20submitUrl%0A%20%20%20%20interpretUrl%0A%20%20%20%20codeDefinition%0A%20%20%20%20sampleTestCase%0A%20%20%20%20enableTestMode%0A%20%20%20%20metaData%0A%20%20%20%20enableRunCode%0A%20%20%20%20enableSubmit%0A%20%20%20%20judgerAvailable%0A%20%20%20%20infoVerified%0A%20%20%20%20envInfo%0A%20%20%20%20urlManager%0A%20%20%20%20article%0A%20%20%20%20questionDetailUrl%0A%20%20%20%20discussCategoryId%0A%20%20%20%20discussSolutionCategoryId%0A%20%20%20%20libraryUrl%0A%20%20%20%20companyTags%20%7B%0A%20%20%20%20%20%20name%0A%20%20%20%20%20%20slug%0A%20%20%20%20%20%20translatedName%0A%20%20%20%20%7D%0A%20%20%20%20topicTags%20%7B%0A%20%20%20%20%20%20name%0A%20%20%20%20%20%20slug%0A%20%20%20%20%20%20translatedName%0A%20%20%20%20%7D%0A%20%20%7D%0A%20%20interviewed%20%7B%0A%20%20%20%20interviewedUrl%0A%20%20%20%20companies%20%7B%0A%20%20%20%20%20%20id%0A%20%20%20%20%20%20name%0A%20%20%20%20%20%20slug%0A%20%20%20%20%7D%0A%20%20%20%20timeOptions%20%7B%0A%20%20%20%20%20%20id%0A%20%20%20%20%20%20name%0A%20%20%20%20%7D%0A%20%20%20%20stageOptions%20%7B%0A%20%20%20%20%20%20id%0A%20%20%20%20%20%20name%0A%20%20%20%20%7D%0A%20%20%7D%0A%20%20subscribeUrl%0A%20%20isPremium%0A%20%20loginUrl%0A%7D%0A&operationName=getQuestionDetail&variables=%7B%22titleSlug%22%3A%22${2}%22%7D" > ${TMP_JSON_FILE}
19-
20-
#
21-
# NOTE: leetcode server supports `br` encoding and make it high priority. But curl doesn't support it,
22-
# So, you need make sure the `accept-encoding` hasn't br in the request header.
23-
# (if you copy & paste the curl script from Chrome, the `br` could be added!)
24-
#
25-
curl -s 'https://leetcode.com/graphql' \
26-
-H 'origin: https://leetcode.com' \
27-
-H 'accept-encoding: gzip, deflate' \
28-
-H 'accept-language: zh-CN,zh;q=0.9,und;q=0.8,en;q=0.7' \
29-
-H 'cookie: __cfduid=dae082e425ee3916c04a5170b832e268e1524142659; _ga=GA1.2.1432146910.1524142661; _gid=GA1.2.650843898.1529736240; csrftoken=iSKedVXxGDkBBXbP9chsyXhbIrRedF7iw2EMRZiemtzKD8vjHSWZJKkKQVIwZKp7; __atuvc=2%7C25; __atuvs=5b2ded02313c83c4001; _gat=1' \
30-
-H 'x-csrftoken: iSKedVXxGDkBBXbP9chsyXhbIrRedF7iw2EMRZiemtzKD8vjHSWZJKkKQVIwZKp7' \
31-
-H 'pragma: no-cache' \
32-
-H 'content-type: application/json' \
33-
-H 'accept: */*' -H 'cache-control: no-cache' \
34-
-H 'authority: leetcode.com' \
35-
-H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36' \
36-
-H "referer: ${1}" \
37-
--data-binary '{"operationName":"questionData","variables":{"titleSlug":"'${2}'"},"query":"query questionData($titleSlug: String!) {\n question(titleSlug: $titleSlug) {\n questionId\n questionFrontendId\n boundTopicId\n title\n titleSlug\n content\n translatedTitle\n translatedContent\n isPaidOnly\n difficulty\n likes\n dislikes\n isLiked\n similarQuestions\n contributors {\n username\n profileUrl\n avatarUrl\n __typename\n }\n langToValidPlayground\n topicTags {\n name\n slug\n translatedName\n __typename\n }\n companyTagStats\n codeSnippets {\n lang\n langSlug\n code\n __typename\n }\n stats\n hints\n solution {\n id\n canSeeDetail\n __typename\n }\n status\n sampleTestCase\n metaData\n judgerAvailable\n judgeType\n mysqlSchemas\n enableRunCode\n enableTestMode\n envInfo\n __typename\n }\n}\n"}' --compressed > ${TMP_JSON_FILE}
38-
3916
# xidel change the -q option to -s after 0.9.4 version, so we have to check that
4017
# if xidel has -q option, then it will return error.
4118

@@ -64,9 +41,50 @@ function query_problem()
6441

6542
QUESTION_FRONTEND_ID=$(xidel ${OPT} ${TMP_JSON_FILE} -e '$json("data").question.questionFrontendId')
6643

67-
QUESTION_CATEGORY=$(xidel ${OPT} ${TMP_JSON_FILE} -e '$json("data").question.categoryTitle')
68-
69-
rm -f $TMP_JSON_FILE
44+
#QUESTION_CATEGORY=$(xidel ${OPT} ${TMP_JSON_FILE} -e '$json("data").question.categoryTitle')
45+
}
46+
47+
function query_problem_jq()
48+
{
49+
QUESTION_CONTENT=$(jq -r '.data.question.content' ${TMP_JSON_FILE} | sed -e 's/<sup>/\^/g' | sed -e 's/<[^>]*>//g; s/&nbsp;/ /g; s/&amp;/\&/g; s/&lt;/\</g; s/&gt;/\>/g; s/&quot;/\"/g; s/&#39;/\'"'"'/g; s/&ldquo;/\"/g;')
50+
51+
QUESTION_DIFFICULTY=$(jq '.data.question.difficulty' ${TMP_JSON_FILE})
52+
53+
QUESTION_TITLE=$(jq '.data.question.title' ${TMP_JSON_FILE})
54+
55+
QUESTION_ID=$(jq '.data.question.questionId' ${TMP_JSON_FILE})
56+
57+
QUESTION_FRONTEND_ID=$(jq '.data.question.questionFrontendId' ${TMP_JSON_FILE})
58+
59+
}
60+
61+
function query_problem()
62+
{
63+
TMP_JSON_FILE=tmp.json
64+
65+
# curl -s "https://leetcode.com/graphql?query=query%20getQuestionDetail(%24titleSlug%3A%20String!)%20%7B%0A%20%20isCurrentUserAuthenticated%0A%20%20question(titleSlug%3A%20%24titleSlug)%20%7B%0A%20%20%20%20questionId%0A%20%20%20%20questionFrontendId%0A%20%20%20%20questionTitle%0A%20%20%20%20translatedTitle%0A%20%20%20%20questionTitleSlug%0A%20%20%20%20content%0A%20%20%20%20translatedContent%0A%20%20%20%20difficulty%0A%20%20%20%20stats%0A%20%20%20%20contributors%0A%20%20%20%20similarQuestions%0A%20%20%20%20discussUrl%0A%20%20%20%20mysqlSchemas%0A%20%20%20%20randomQuestionUrl%0A%20%20%20%20sessionId%0A%20%20%20%20categoryTitle%0A%20%20%20%20submitUrl%0A%20%20%20%20interpretUrl%0A%20%20%20%20codeDefinition%0A%20%20%20%20sampleTestCase%0A%20%20%20%20enableTestMode%0A%20%20%20%20metaData%0A%20%20%20%20enableRunCode%0A%20%20%20%20enableSubmit%0A%20%20%20%20judgerAvailable%0A%20%20%20%20infoVerified%0A%20%20%20%20envInfo%0A%20%20%20%20urlManager%0A%20%20%20%20article%0A%20%20%20%20questionDetailUrl%0A%20%20%20%20discussCategoryId%0A%20%20%20%20discussSolutionCategoryId%0A%20%20%20%20libraryUrl%0A%20%20%20%20companyTags%20%7B%0A%20%20%20%20%20%20name%0A%20%20%20%20%20%20slug%0A%20%20%20%20%20%20translatedName%0A%20%20%20%20%7D%0A%20%20%20%20topicTags%20%7B%0A%20%20%20%20%20%20name%0A%20%20%20%20%20%20slug%0A%20%20%20%20%20%20translatedName%0A%20%20%20%20%7D%0A%20%20%7D%0A%20%20interviewed%20%7B%0A%20%20%20%20interviewedUrl%0A%20%20%20%20companies%20%7B%0A%20%20%20%20%20%20id%0A%20%20%20%20%20%20name%0A%20%20%20%20%20%20slug%0A%20%20%20%20%7D%0A%20%20%20%20timeOptions%20%7B%0A%20%20%20%20%20%20id%0A%20%20%20%20%20%20name%0A%20%20%20%20%7D%0A%20%20%20%20stageOptions%20%7B%0A%20%20%20%20%20%20id%0A%20%20%20%20%20%20name%0A%20%20%20%20%7D%0A%20%20%7D%0A%20%20subscribeUrl%0A%20%20isPremium%0A%20%20loginUrl%0A%7D%0A&operationName=getQuestionDetail&variables=%7B%22titleSlug%22%3A%22${2}%22%7D" > ${TMP_JSON_FILE}
66+
67+
#
68+
# NOTE: leetcode server supports `br` encoding and make it high priority. But curl doesn't support it,
69+
# So, you need make sure the `accept-encoding` hasn't br in the request header.
70+
# (if you copy & paste the curl script from Chrome, the `br` could be added!)
71+
#
72+
curl -s 'https://leetcode.com/graphql' \
73+
-H 'origin: https://leetcode.com' \
74+
-H 'accept-encoding: gzip, deflate' \
75+
-H 'accept-language: zh-CN,zh;q=0.9,und;q=0.8,en;q=0.7' \
76+
-H 'cookie: __cfduid=dae082e425ee3916c04a5170b832e268e1524142659; _ga=GA1.2.1432146910.1524142661; _gid=GA1.2.650843898.1529736240; csrftoken=iSKedVXxGDkBBXbP9chsyXhbIrRedF7iw2EMRZiemtzKD8vjHSWZJKkKQVIwZKp7; __atuvc=2%7C25; __atuvs=5b2ded02313c83c4001; _gat=1' \
77+
-H 'x-csrftoken: iSKedVXxGDkBBXbP9chsyXhbIrRedF7iw2EMRZiemtzKD8vjHSWZJKkKQVIwZKp7' \
78+
-H 'pragma: no-cache' \
79+
-H 'content-type: application/json' \
80+
-H 'accept: */*' -H 'cache-control: no-cache' \
81+
-H 'authority: leetcode.com' \
82+
-H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36' \
83+
-H "referer: ${1}" \
84+
--data-binary '{"operationName":"questionData","variables":{"titleSlug":"'${2}'"},"query":"query questionData($titleSlug: String!) {\n question(titleSlug: $titleSlug) {\n questionId\n questionFrontendId\n boundTopicId\n title\n titleSlug\n content\n translatedTitle\n translatedContent\n isPaidOnly\n difficulty\n likes\n dislikes\n isLiked\n similarQuestions\n contributors {\n username\n profileUrl\n avatarUrl\n __typename\n }\n langToValidPlayground\n topicTags {\n name\n slug\n translatedName\n __typename\n }\n companyTagStats\n codeSnippets {\n lang\n langSlug\n code\n __typename\n }\n stats\n hints\n solution {\n id\n canSeeDetail\n __typename\n }\n status\n sampleTestCase\n metaData\n judgerAvailable\n judgeType\n mysqlSchemas\n enableRunCode\n enableTestMode\n envInfo\n __typename\n }\n}\n"}' --compressed > ${TMP_JSON_FILE}
85+
86+
query_problem_jq
87+
#query_problem_xidel
7088
}
7189

7290
function detect_os()
@@ -91,6 +109,49 @@ function install_brew()
91109
fi
92110
}
93111

112+
113+
function manually_install_jq() {
114+
echo "Unknown platform, please install 'jq' manually!"
115+
echo "Dowload Site: https://stedolan.github.io/jq/download/"
116+
}
117+
function install_jq()
118+
{
119+
echo "Install jq ..."
120+
121+
platform=`detect_os`
122+
123+
if [[ "$platform" == "unknown" ]]; then
124+
manually_install_jq
125+
exit 1;
126+
fi
127+
128+
if [[ "$platform" == "linux" ]]; then
129+
OS_RELEASE="/etc/os-release"
130+
if [ ! -f ${OS_RELEASE} ]; then
131+
manually_install_jq
132+
exit 1;
133+
fi
134+
DISTRO=`awk -F= '/^NAME/{print $2}' `
135+
DISTRO="CentOS Linux"
136+
case $DISTRO in
137+
Ubuntu* ) sudo apt-get install jq
138+
;;
139+
CentOS* ) echo sudo yum install jq
140+
;;
141+
* ) manually_install_jq
142+
exit 1;
143+
esac
144+
fi
145+
146+
if [[ "$platform" == "macos" ]]; then
147+
install_brew
148+
echo "brew install jq..."
149+
brew install jq
150+
fi
151+
152+
echo "Install jq successfullly !"
153+
}
154+
94155
function install_xidel()
95156
{
96157
echo "Install xidel ..."

0 commit comments

Comments
 (0)