Skip to content

Commit

Permalink
Leetcode changed the frontend to GraphQL API, the scripts need to ada…
Browse files Browse the repository at this point in the history
…pt it. fix issue #138
  • Loading branch information
haoel committed Feb 1, 2018
1 parent 92562b3 commit ca71b67
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 30 deletions.
77 changes: 52 additions & 25 deletions scripts/comments.sh
Expand Up @@ -2,12 +2,19 @@
set -e

AUTHOR="NOBODY"
LEETCODE_URL=https://leetcode.com/problems/
LEETCODE_NEW_URL=https://leetcode.com/problems/
LEETCODE_OLD_URL=https://oj.leetcode.com/problems/
COMMENT_TAG="//"
FILE_EXT=".cpp"

pushd `dirname $0` > /dev/null
SCRIPTPATH=`pwd -P`
popd > /dev/null
SCRIPTFILE=`basename $0`

COLOR_INFO='\033[0;36m'
COLOR_NONE='\033[0m'

source ${SCRIPTPATH}/lib/query_problem.sh

function usage()
{

Expand Down Expand Up @@ -99,6 +106,7 @@ function install_xidel()
}



if [ $# -lt 1 ] || [[ "${1}" != ${LEETCODE_NEW_URL}* ]] && [[ "${1}" != ${LEETCODE_OLD_URL}* ]]; then
usage
exit 255
Expand All @@ -108,17 +116,29 @@ if [[ "${1}" == ${LEETCODE_OLD_URL}* ]]; then
LEETCODE_URL=${LEETCODE_OLD_URL}
fi

IS_SHELL=`curl ${1} 2>/dev/null | grep Bash |wc -l`
if [ ${IS_SHELL} -gt 0 ]; then
COMMENT_TAG='#'
FILE_EXT='.sh'
fi


leetcode_url=$1
current_time=`date +%Y-%m-%d`
platform=`detect_os`

get_question_slug ${leetcode_url}


TRUE_CMD=`which true`
xidel=`type -P xidel || ${TRUE_CMD}`
if [ -z "${xidel}" ]; then
echo "xidel not found !"
install_xidel
fi

#grab the problem information
query_problem ${leetcode_url} ${QUESTION_TITLE_SLUG}

if [ ${QUESTION_CATEGORY} == "Shell" ]; then
COMMENT_TAG='#'
FILE_EXT='.sh'
fi

if [ $# -gt 1 ] && [ -f $2 ]; then
source_file=$2
if [[ "$platform" == "linux" ]]; then
Expand All @@ -127,8 +147,8 @@ if [ $# -gt 1 ] && [ -f $2 ]; then
current_time=`stat -f %a ${source_file} | xargs -I time date -r time +%Y-%m-%d`
fi
else
source_file=${1#${LEETCODE_URL}}
source_file=${source_file::${#source_file}-1}
source_file=$QUESTION_TITLE_SLUG
#source_file=${source_file::${#source_file}-1}
source_file=`echo $source_file | awk -F '-' '{for (i=1; i<=NF; i++) printf("%s", toupper(substr($i,1,1)) substr($i,2)) }'`${FILE_EXT}

if [ ! -f ${source_file} ]; then
Expand All @@ -144,6 +164,7 @@ else
fi
fi


# the source file is existed but it is empty, add a line,
# otherwise it could casue the comments inserts failed.
if [ ! -s $source_file ]; then
Expand All @@ -162,38 +183,44 @@ if ! grep -Fq "${COMMENT_TAG} Author :" $source_file ; then
rm ${source_file}.bak
fi

#grab the problem description and add the comments
TRUE_CMD=`which true`
xidel=`type -P xidel || ${TRUE_CMD}`
if [ -z "${xidel}" ]; then
echo "xidel not found !"
install_xidel
fi

# using xidel grab the problem description

#echo "$QUESTION_CONTENT"
#echo $QUESTION_DIFFICULTY
#echo $QUESTION_TITLE
#echo $QUESTION_ID
#echo $QUESTION_CATEGORY


TMP_FILE=/tmp/tmp.txt

# 1) the `fold` command is used to wrap the text at centain column
# 2) the last two `sed` commands are used to add the comments tags
case $FILE_EXT in
.cpp ) xidel ${leetcode_url} -q -e "css('div.question-content')" | \
grep -v ' ' | sed '/^$/N;/^\n$/D' | fold -w 85 -s |\
.cpp ) echo "$QUESTION_CONTENT" |\
grep -v ' ' | sed '/^$/N;/^\n$/D' | fold -w 85 -s |\
sed 's/^/ * /' | sed '1i\'$'\n'"/*$(printf '%.0s*' {0..85}) "$'\n' |\
sed '2i\'$'\n''!@#$%'$'\n' | sed 's/!@#$%/ */' | \
sed '$a\'$'\n'"*$(printf '%.0s*' {0..85})*/"$'\n'| \
sed 's/^*/ /' > /tmp/tmp.txt
sed 's/^*/ /' > ${TMP_FILE}
;;
.sh ) xidel ${leetcode_url} -q -e "css('div.question-content')" | \
.sh ) echo "$QUESTION_CONTENT" |\
grep -v ' ' |sed '/^$/N;/^\n$/D' | fold -w 85 -s| \
sed 's/^/# /' | sed '1i\'$'\n'"#$(printf '%.0s#' {0..85}) "$'\n' | \
sed '2i\'$'\n''#'$'\n' | sed '$a\'$'\n'"#$(printf '%.0s#' {0..85})"$'\n'\
> /tmp/tmp.txt
> ${TMP_FILE}
;;
* ) echo "Bad file extension!"
exit 1;

esac

#remove the ^M chars
cat -v ${TMP_FILE} | tr -d '^M' > ${TMP_FILE}.1
mv ${TMP_FILE}.1 ${TMP_FILE}

#insert the problem description into the source file, and remove it
sed -i.bak '4 r /tmp/tmp.txt' ${source_file}
sed -i.bak '4 r '${TMP_FILE}'' ${source_file}
rm -f ${source_file}.bak
rm -f /tmp/tmp.txt

Expand Down
42 changes: 42 additions & 0 deletions scripts/lib/query_problem.sh
@@ -0,0 +1,42 @@
#!/bin/bash

LEETCODE_URL=https://leetcode.com/problems/
LEETCODE_NEW_URL=https://leetcode.com/problems/
LEETCODE_OLD_URL=https://oj.leetcode.com/problems/


function get_question_slug()
{
QUESTION_TITLE_SLUG=${1#${LEETCODE_URL}}
QUESTION_TITLE_SLUG=$(echo $QUESTION_TITLE_SLUG | sed 's/\/.*//g') # remove the needless url path
}

function query_problem()
{
TMP_JSON_FILE=tmp.json

curl -s 'https://leetcode.com/graphql' \
-H 'origin: https://leetcode.com' \
-H 'accept-encoding: gzip, deflate, br' \
-H 'accept-language: zh-CN,zh;q=0.9,und;q=0.8,en;q=0.7' \
-H 'cookie: __atuvc=3%7C8%2C0%7C9%2C0%7C10%2C0%7C11%2C11%7C12; _ga=GA1.2.714733890.1464506754; __cfduid=d0091b1a13637142e21e82f5f43f8f3c61517304755; _gid=GA1.2.846248999.1517304756; csrftoken=W3F3gDMAmFE1024RMhNLUi0ofHb4gJ5Wqt4GcOaZHJKneSuXkY7kq2vHm6mA8f12; _gat=1' \
-H 'x-csrftoken: W3F3gDMAmFE1024RMhNLUi0ofHb4gJ5Wqt4GcOaZHJKneSuXkY7kq2vHm6mA8f12' \
-H 'pragma: no-cache' \
-H 'content-type: application/json' \
-H 'accept: */*' -H 'cache-control: no-cache' \
-H 'authority: leetcode.com' \
-H "referer: ${1}" \
--data-binary '{"operationName":"getQuestionDetail","variables":{"titleSlug":"'${2}'"},"query":"query getQuestionDetail($titleSlug: String!) {\n isCurrentUserAuthenticated\n question(titleSlug: $titleSlug) {\n questionId\n questionFrontendId\n questionTitle\n questionTitleSlug\n content\n difficulty\n stats\n contributors\n companyTags\n topicTags\n similarQuestions\n discussUrl\n mysqlSchemas\n randomQuestionUrl\n sessionId\n categoryTitle\n submitUrl\n interpretUrl\n codeDefinition\n sampleTestCase\n enableTestMode\n metaData\n enableRunCode\n enableSubmit\n judgerAvailable\n infoVerified\n envInfo\n urlManager\n article\n questionDetailUrl\n discussCategoryId\n discussSolutionCategoryId\n }\n interviewed {\n interviewedUrl\n companies {\n id\n name\n }\n timeOptions {\n id\n name\n }\n stageOptions {\n id\n name\n }\n }\n subscribeUrl\n isPremium\n loginUrl\n}\n"}' --compressed > ${TMP_JSON_FILE}

QUESTION_CONTENT=$(xidel -q ${TMP_JSON_FILE} -e '$json("data").question.content' | sed -e 's/<[^>]*>//g')

QUESTION_DIFFICULTY=$(xidel -q ${TMP_JSON_FILE} -e '$json("data").question.difficulty')

QUESTION_TITLE=$(xidel -q ${TMP_JSON_FILE} -e '$json("data").question.questionTitle')

QUESTION_ID=$(xidel -q ${TMP_JSON_FILE} -e '$json("data").question.questionId')

QUESTION_CATEGORY=$(xidel -q ${TMP_JSON_FILE} -e '$json("data").question.categoryTitle')

rm -f $TMP_JSON_FILE
}
27 changes: 22 additions & 5 deletions scripts/readme.sh
@@ -1,5 +1,14 @@
#!/bin/bash

pushd `dirname $0` > /dev/null
SCRIPTPATH=`pwd -P`
popd > /dev/null
SCRIPTFILE=`basename $0`

COLOR_INFO='\033[0;36m'
COLOR_NONE='\033[0m'

source ${SCRIPTPATH}/lib/query_problem.sh

function usage()
{
Expand All @@ -23,11 +32,19 @@ DIR=`cd $(dirname ${1}) && pwd -P`
FILE=${DIR}/$(basename ${1})

URL=`grep Source ${FILE} | awk '{print $4}'`
title_str=`xidel ${URL} -q -e "css('div.question-title h3')"`
NUM=`echo ${title_str} | awk -F '.' '{print $1}'`
TITLE=`echo ${title_str} | awk -F '.' '{print $2}' | sed -e 's/^[[:space:]]*//'`
DIFFCULT=`xidel ${URL} -q -e "css('.question-info')" | grep Difficulty | awk '{print $2}'`

URL=$(echo $URL | sed -e 's/oj\.leetcode\.com/leetcode\.com/g')

get_question_slug ${URL}
query_problem ${URL} ${QUESTION_TITLE_SLUG}

#echo "$QUESTION_CONTENT"
#echo $QUESTION_DIFFICULTY
#echo $QUESTION_TITLE
#echo $QUESTION_ID
#echo $QUESTION_CATEGORY


FILE=`echo ${FILE} | sed "s/.*\/algorithms/\.\/algorithms/"`

echo "|${NUM}|[${TITLE}](${URL}) | [C++](${FILE})|${DIFFCULT}|"
echo "|${QUESTION_ID}|[${QUESTION_TITLE}](${URL}) | [C++](${FILE})|${QUESTION_DIFFICULTY}|"

0 comments on commit ca71b67

Please sign in to comment.