aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authortypebrook <typebrook@gmail.com>2020-05-17 00:57:15 +0800
committertypebrook <typebrook@gmail.com>2020-05-17 11:25:40 +0800
commit48657936432fbbea797c4822234f41f739fd05e9 (patch)
tree4ac8caf350c994e62ec6d466fc8cea44ac711693
parentee801613ab7d72dcd760449a10585cd7bb33a1ef (diff)
Add pagnation to fetch even if user's gists >100
-rwxr-xr-xgist75
1 files changed, 48 insertions, 27 deletions
diff --git a/gist b/gist
index fe3ee42..b9cb192 100755
--- a/gist
+++ b/gist
@@ -58,6 +58,7 @@ configuredClient=""
58NAME=${GISTSCRIPT:-$(basename $0)} #show hint and helper message with current script name 58NAME=${GISTSCRIPT:-$(basename $0)} #show hint and helper message with current script name
59GITHUB_API=https://api.github.com 59GITHUB_API=https://api.github.com
60CONFIG=~/.config/gist.conf; mkdir -p ~/.config 60CONFIG=~/.config/gist.conf; mkdir -p ~/.config
61per_page=100
61 62
62INDEX_FORMAT=('index' 'url' 'tags_string' 'blob_code' 'file_array' 'file_num' 'comment_num' 'author' 'created_at' 'updated_at' 'description') 63INDEX_FORMAT=('index' 'url' 'tags_string' 'blob_code' 'file_array' 'file_num' 'comment_num' 'author' 'created_at' 'updated_at' 'description')
63TAG_CHAR='-_[:alnum:]' 64TAG_CHAR='-_[:alnum:]'
@@ -125,7 +126,7 @@ http_method() {
125 case "$configuredClient" in 126 case "$configuredClient" in
126 curl) [[ -n $token ]] && header_opt="--header" header="Authorization: token $token" 127 curl) [[ -n $token ]] && header_opt="--header" header="Authorization: token $token"
127 [[ $METHOD =~ (POST|PATCH) ]] && data_opt='--data' 128 [[ $METHOD =~ (POST|PATCH) ]] && data_opt='--data'
128 curl -X "$METHOD" -A curl -s $header_opt "$header" $data_opt "@$http_data" "$@" ;; 129 curl -X "$METHOD" -A curl -s $header_opt "$header" $data_opt ${HEADER:+-D $HEADER} "@$http_data" "$@" ;;
129 wget) [[ -n $token ]] && header_opt="--header" header="Authorization: token $token" 130 wget) [[ -n $token ]] && header_opt="--header" header="Authorization: token $token"
130 [[ $METHOD =~ (POST|PATCH) ]] && data_opt='--body-file' 131 [[ $METHOD =~ (POST|PATCH) ]] && data_opt='--body-file'
131 wget --method="$METHOD" -qO- $header_opt "$header" $data_opt "$http_data" "$@" ;; 132 wget --method="$METHOD" -qO- $header_opt "$header" $data_opt "$http_data" "$@" ;;
@@ -356,29 +357,30 @@ _grep_content() {
356 357
357# Parse JSON object of the result of gist fetch 358# Parse JSON object of the result of gist fetch
358_parse_gists() { 359_parse_gists() {
359 _process_json ' 360 _process_json '
360raw = json.load(sys.stdin) 361raw = json.load(sys.stdin)
361for gist in raw: 362for gist in raw:
362 print(gist["html_url"], end=" ") 363 print(gist["html_url"], end=" ")
363 print(gist["public"], end=" ") 364 print(gist["public"], end=" ")
364 print(",".join(file["raw_url"] for file in gist["files"].values()), end=" ") 365 print(",".join(file["raw_url"] for file in gist["files"].values()), end=" ")
365 print(",".join(file["filename"].replace(" ", "-") + "@" + str(file["language"]).replace(" ", "-") for file in gist["files"].values()), end=" ") 366 print(",".join(file["filename"].replace(" ", "-") + "@" + str(file["language"]).replace(" ", "-") for file in gist["files"].values()), end=" ")
366 print(len(gist["files"]), end=" ") 367 print(len(gist["files"]), end=" ")
367 print(gist["comments"], end=" ") 368 print(gist["comments"], end=" ")
368 print(gist["owner"]["login"], end=" ") 369 print(gist["owner"]["login"], end=" ")
369 print(gist["created_at"], end=" ") 370 print(gist["created_at"], end=" ")
370 print(gist["updated_at"], end=" ") 371 print(gist["updated_at"], end=" ")
371 print(gist["description"]) 372 print(gist["description"])
372 ' 373 '
373} 374}
374 375
375# Parse response from 'gist fetch' to the format for index file 376# Parse response from 'gist fetch' to the format for index file
376_parse_response() { 377_parse_response() {
378 local num=$start_from_num
377 _parse_gists \ 379 _parse_gists \
378 | tac | nl -s' ' \ 380 | tac | nl -s' ' \
379 | while read -r "${INDEX_FORMAT[@]:0:2}" public file_url_array "${INDEX_FORMAT[@]:4:7}"; do 381 | while read -r "${INDEX_FORMAT[@]:0:2}" public file_url_array "${INDEX_FORMAT[@]:4:7}"; do
380 local private_prefix=''; [[ $public == 'False' ]] && private_prefix=p 382 local private_prefix=''; [[ $public == 'False' ]] && private_prefix=p
381 [[ -n $1 ]] && local index=${1}; index=${private_prefix}${prefix}${index} 383 [[ -n $num ]] && local index=${num} && num=$(($num+1)); index=${private_prefix}${prefix}${index}
382 384
383 local blob_code=$(echo "$file_url_array" | tr ',' '\n' | sed -E -e 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -s -d '-' -) 385 local blob_code=$(echo "$file_url_array" | tr ',' '\n' | sed -E -e 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -s -d '-' -)
384 file_array=${file_array//@None/@Text} 386 file_array=${file_array//@None/@Text}
@@ -393,27 +395,45 @@ _parse_response() {
393} 395}
394 396
395# Get latest list of gists from Github API 397# Get latest list of gists from Github API
396# TODO pagnation for more than 100 gists
397_fetch_gists() { 398_fetch_gists() {
398 echo "fetching $user's gists from $GITHUB_API..."
399 echo
400 local route="users/$user/gists" 399 local route="users/$user/gists"
401 local prifix='' 400 local prifix=''
402 if [[ $mark == s ]]; then 401 if [[ $mark == s ]]; then
403 route='gists/starred' 402 route='gists/starred'
404 prefix=s 403 prefix=s
405 extra="s0 https://gist.github.com/b0d2e7e67aa50298fdf8111ae7466b56 #bash,#gist NONE README.md@Markdown,gist@Shell 2 30 typebrook 2019-12-26T06:49:40Z 2020-05-15T13:00:31Z [bash-gist] A bash script for gist management"
406 fi 404 fi
407 405
408 result=$(http_method GET $GITHUB_API/$route?per_page=100 | prefix=$prefix _parse_response) 406 # set global variable HEADER in http_method, so prevent using pipe
409 result=$(printf "${extra:+$extra\n}$result") 407 HEADER=$(tmp_file HEADER)
410 [[ -z $result ]] && echo 'Not a single valid gist' && return 0 408 HEADER=$HEADER http_method GET $GITHUB_API/$route${1} | start_from_num=$2 prefix=$prefix _parse_response
409}
410
411# consider if HEADER is not exist
412_fetch_gists_with_pagnation() {
413 echo "fetching $user's gists from $GITHUB_API..."
414 echo
415
416 local fetched_records=$(tmp_file fetched)
417 _fetch_gists "?per_page=$per_page" >> $fetched_records
418
419 while true; do
420 local next_page=''
421 [[ -e $HEADER ]] && next_page=$(sed -Ene '/^[lL]ink: / s/.+page=([[:digit:]]+)>; rel=\"next\".+/\1/p' $HEADER)
422 [[ -z $next_page ]] && break
423 printf "%-4s gists fetched\n" $(( ($next_page -1) * $per_page ))
424
425 _fetch_gists "?per_page=$per_page&page=$next_page" $(( $(wc -l <$fetched_records) +1 )) >> $fetched_records
426 done || return 1
427
428 [[ ! -s $fetched_records ]] && echo 'Not a single valid gist' && return 0
411 429
412 sed -i'' -Ee "/^$mark/ d" $INDEX && echo "$result" >> $INDEX 430 sed -i'' -Ee "/^$mark/ d" $INDEX; cat $fetched_records >> $INDEX
413 hint=$hint _show_list 431 hint=$hint _show_list
414 432
415 [[ $auto_sync != false ]] && (_sync_repos &> /dev/null &) 433 [[ $auto_sync != false ]] && (_sync_repos &> /dev/null &)
416 true 434 true
435 #extra="s0 https://gist.github.com/b0d2e7e67aa50298fdf8111ae7466b56 #bash,#gist NONE README.md@Markdown,gist@Shell 2 30 typebrook 2019-12-26T06:49:40Z 2020-05-15T13:00:31Z [bash-gist] A bash script for gist management"
436 #result=$(printf "${extra:+$extra\n}$result")
417} 437}
418 438
419# Fetch gists for a given user 439# Fetch gists for a given user
@@ -601,7 +621,8 @@ _push_to_remote() {
601 git add . && git commit -m 'update' 621 git add . && git commit -m 'update'
602 fi 622 fi
603 if [[ -n $(git cherry) ]]; then 623 if [[ -n $(git cherry) ]]; then
604 git push origin master && (hint=false _fetch_gists &> /dev/null &) 624 # FIXME only fetch one gist
625 git push origin master && (hint=false _fetch_gists_with_pagnation &> /dev/null &)
605 fi 626 fi
606} 627}
607 628
@@ -669,7 +690,7 @@ _create_gist() {
669 | _gist_body > "$http_data" \ 690 | _gist_body > "$http_data" \
670 && http_method POST $GITHUB_API/gists \ 691 && http_method POST $GITHUB_API/gists \
671 | xargs -I{} -0 echo "[{}]" \ 692 | xargs -I{} -0 echo "[{}]" \
672 | _parse_response $(( $(sed -e '/^s/ d' $INDEX | wc -l) +1 )) \ 693 | start_from_num=$(( $(sed -e '/^s/ d' $INDEX | wc -l) +1 )) _parse_response \
673 | tee -a $INDEX \ 694 | tee -a $INDEX \
674 | cut -d' ' -f2 | sed -E -e 's#.*/##' \ 695 | cut -d' ' -f2 | sed -E -e 's#.*/##' \
675 | (xargs -I{} git clone "$(_repo_url {})" $folder/{} &> /dev/null &) 696 | (xargs -I{} git clone "$(_repo_url {})" $folder/{} &> /dev/null &)
@@ -701,7 +722,7 @@ _edit_gist() {
701 echo '{' \"description\": \""${DESC//\"/\\\"}"\" '}' > "$http_data" 722 echo '{' \"description\": \""${DESC//\"/\\\"}"\" '}' > "$http_data"
702 new_record=$(http_method PATCH "$GITHUB_API/gists/$GIST_ID" \ 723 new_record=$(http_method PATCH "$GITHUB_API/gists/$GIST_ID" \
703 | sed -e '1 s/^/[/; $ s/$/]/' \ 724 | sed -e '1 s/^/[/; $ s/$/]/' \
704 | _parse_response "${index#[[:alpha:]]}" ) 725 | start_from_index="${index#[[:alpha:]]}" _parse_response)
705 [[ -n $new_record ]] && sed -i'' -E -e "/^$index / s^.+^$new_record^" $INDEX \ 726 [[ -n $new_record ]] && sed -i'' -E -e "/^$index / s^.+^$new_record^" $INDEX \
706 && hint=false mark="$index " _show_list \ 727 && hint=false mark="$index " _show_list \
707 || echo 'Fail to modify gist description' 728 || echo 'Fail to modify gist description'
@@ -811,7 +832,7 @@ _access_last_index() {
811} 832}
812 833
813_apply_config "$@" || exit 1 834_apply_config "$@" || exit 1
814if [[ $init ]]; then _fetch_gists; exit 0; fi 835if [[ $init ]]; then _fetch_gists_with_pagnation; exit 0; fi
815case "$1" in 836case "$1" in
816 "") 837 "")
817 _show_list ;; 838 _show_list ;;
@@ -821,7 +842,7 @@ case "$1" in
821 mark=.; _show_list ;; 842 mark=.; _show_list ;;
822 fetch | f) 843 fetch | f)
823 [[ $2 =~ ^(s|star)$ ]] && mark=s || mark=[^s] 844 [[ $2 =~ ^(s|star)$ ]] && mark=s || mark=[^s]
824 _fetch_gists ;; 845 _fetch_gists_with_pagnation ;;
825 new | n) 846 new | n)
826 shift 847 shift
827 _create_gist "$@" ;; 848 _create_gist "$@" ;;