aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authortypebrook <typebrook@gmail.com>2020-05-17 14:29:09 +0800
committertypebrook <typebrook@gmail.com>2020-05-17 22:43:48 +0800
commit47697ba48a36e43306fc5a5557d2f546c846ac7b (patch)
treec3d190261e0a69d56c5a60f53c04e35bef73575f
parent8efa0db70bf12565d3b080194e5e98be4ae49a0e (diff)
Refactor method of parsing gists
- Do not set indices in _parse_response() unless 'index' is set - Change INDEX_FORMAT
-rwxr-xr-xgist46
1 files changed, 23 insertions, 23 deletions
diff --git a/gist b/gist
index 01c72c7..a3b54ed 100755
--- a/gist
+++ b/gist
@@ -60,7 +60,7 @@ GITHUB_API=https://api.github.com
60CONFIG=~/.config/gist.conf; mkdir -p ~/.config 60CONFIG=~/.config/gist.conf; mkdir -p ~/.config
61per_page=100 61per_page=100
62 62
63INDEX_FORMAT=('index' 'url' 'tags_string' 'blob_code' 'file_array' 'file_num' 'comment_num' 'author' 'created_at' 'updated_at' 'description') 63INDEX_FORMAT=('index' 'public' 'url' 'tags_string' 'blob_code' 'file_array' 'file_num' 'comment_num' 'author' 'created_at' 'updated_at' 'description')
64TAG_CHAR='-_[:alnum:]' 64TAG_CHAR='-_[:alnum:]'
65if [[ ! -t 0 ]]; then 65if [[ ! -t 0 ]]; then
66 INPUT=$(cat) 66 INPUT=$(cat)
@@ -360,8 +360,8 @@ _parse_gists() {
360 _process_json ' 360 _process_json '
361raw = json.load(sys.stdin) 361raw = json.load(sys.stdin)
362for gist in raw: 362for gist in raw:
363 print(gist["html_url"], end=" ")
364 print(gist["public"], end=" ") 363 print(gist["public"], end=" ")
364 print(gist["html_url"], end=" ")
365 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=" ")
366 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=" ")
367 print(len(gist["files"]), end=" ") 367 print(len(gist["files"]), end=" ")
@@ -375,13 +375,8 @@ for gist in raw:
375 375
376# Parse response from 'gist fetch' to the format for index file 376# Parse response from 'gist fetch' to the format for index file
377_parse_response() { 377_parse_response() {
378 local num=$start_from_num
379 _parse_gists \ 378 _parse_gists \
380 | nl -s' ' \ 379 | while read -r "${INDEX_FORMAT[@]:1:2}" file_url_array "${INDEX_FORMAT[@]:5:7}"; do
381 | while read -r "${INDEX_FORMAT[@]:0:2}" public file_url_array "${INDEX_FORMAT[@]:4:7}"; do
382 local private_prefix=''; [[ $public == 'False' ]] && private_prefix=p
383 [[ -n $num ]] && local index=${num} && num=$(($num+1)); index=${private_prefix}${prefix}${index}
384
385 local blob_code=$(echo "$file_url_array" | tr ',' '\n' | sed -E -e 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -s -d '-' -) 380 local blob_code=$(echo "$file_url_array" | tr ',' '\n' | sed -E -e 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -s -d '-' -)
386 file_array=${file_array//@None/@Text} 381 file_array=${file_array//@None/@Text}
387 382
@@ -397,15 +392,13 @@ _parse_response() {
397# Get latest list of gists from Github API 392# Get latest list of gists from Github API
398_fetch_gists() { 393_fetch_gists() {
399 local route="users/$user/gists" 394 local route="users/$user/gists"
400 local prifix=''
401 if [[ $mark == s ]]; then 395 if [[ $mark == s ]]; then
402 route='gists/starred' 396 route='gists/starred'
403 prefix=s
404 fi 397 fi
405 398
406 # set global variable HEADER in http_method, so prevent using pipe 399 # set global variable HEADER in http_method, so prevent using pipe
407 HEADER=$(tmp_file HEADER) 400 HEADER=$(tmp_file HEADER)
408 HEADER=$HEADER http_method GET $GITHUB_API/$route${1} | start_from_num=$2 prefix=$prefix _parse_response 401 HEADER=$HEADER http_method GET $GITHUB_API/$route${1} | _parse_response
409} 402}
410 403
411# consider if HEADER is not exist 404# consider if HEADER is not exist
@@ -422,13 +415,17 @@ _fetch_gists_with_pagnation() {
422 [[ -z $next_page ]] && break 415 [[ -z $next_page ]] && break
423 printf "%-4s gists fetched\n" $(( ($next_page -1) * $per_page )) 416 printf "%-4s gists fetched\n" $(( ($next_page -1) * $per_page ))
424 417
425 _fetch_gists "?per_page=$per_page&page=$next_page" $(( $(wc -l <$fetched_records) +1 )) >> $fetched_records 418 _fetch_gists "?per_page=$per_page&page=$next_page" >> $fetched_records
426 done || return 1 419 done || return 1
427 420
428 [[ ! -s $fetched_records ]] && echo 'Not a single valid gist' && return 0 421 [[ ! -s $fetched_records ]] && echo 'Not a single valid gist' && return 0
429 422
430 sed -i'' -Ee "/^$mark/ d" $INDEX 423 sed -i'' -Ee "/^$mark/ d" $INDEX
431 tac <$fetched_records | nl -s' ' | sed -Ee 's/^ *([^ ]+) ([[:alpha:]]?)[[:digit:]]+/\2\1/' >> $INDEX 424 tac <$fetched_records | nl -s' ' \
425 | while read -r "${INDEX_FORMAT[@]:0:2}" extra; do
426 local prefix=''; [[ $public == False ]] && prefix=p
427 echo $prefix$index $public $extra
428 done >> $INDEX
432 hint=$hint _show_list 429 hint=$hint _show_list
433 430
434 [[ $auto_sync != false ]] && (_sync_repos &> /dev/null &) 431 [[ $auto_sync != false ]] && (_sync_repos &> /dev/null &)
@@ -439,9 +436,8 @@ _fetch_gists_with_pagnation() {
439 436
440# Get a single JSON object of gist from response, and update index file 437# Get a single JSON object of gist from response, and update index file
441_update_gist() { 438_update_gist() {
442 local index=$1 439 local record="$(sed -e '1 s/^/[/; $ s/$/]/' | index=$1 _parse_response)"
443 local record=$(sed -e '1 s/^/[/; $ s/$/]/' | start_from_num="${index#[[:alpha:]]}" _parse_response) 440 [[ -n $record ]] && sed -i'' -Ee "/^$1 / s^.+^$record^" $INDEX
444 [[ -n $record ]] && sed -i'' -Ee "/^$index / s^.+^$record^" $INDEX
445} 441}
446 442
447# Fetch gists for a given user 443# Fetch gists for a given user
@@ -478,7 +474,7 @@ _pull_if_needed() {
478# Update local git repos 474# Update local git repos
479_sync_repos() { 475_sync_repos() {
480 comm -1 <(ls -A "$folder" | sort) \ 476 comm -1 <(ls -A "$folder" | sort) \
481 <(cut -d' ' -f1-2 < "$INDEX" | sed -ne "/^$mark/ s#.*/##p" | sort) \ 477 <(while read -r ${INDEX_FORMAT[@]}; do echo $index $url; done < "$INDEX" | sed -ne "/^$mark/ s#.*/##p" | sort) \
482 | { 478 | {
483 result=$(cat) 479 result=$(cat)
484 480
@@ -505,7 +501,8 @@ _repo_url() {
505 501
506# Get gist id from index files 502# Get gist id from index files
507_gist_id() { 503_gist_id() {
508 GIST_ID=$(sed -En -e "/^$1 / s#^$1 [^ ]+/([[:alnum:]]+) .+#\1#p" $INDEX | head -1) 504 read -r ${INDEX_FORMAT[@]} <<<"$(sed -ne "/$1 / p" $INDEX)"
505 GIST_ID=${url##*/}
509 if [[ -z $GIST_ID || ! $1 =~ [0-9a-z]+ ]]; then 506 if [[ -z $GIST_ID || ! $1 =~ [0-9a-z]+ ]]; then
510 echo -e "$(hint=false _show_list | sed -Ee 's/^( *[0-9a-z]+)/\\e[5m\1\\e[0m/')" 507 echo -e "$(hint=false _show_list | sed -Ee 's/^( *[0-9a-z]+)/\\e[5m\1\\e[0m/')"
511 echo 508 echo
@@ -574,7 +571,7 @@ _delete_gist() {
574# Remove repos which are not in index file anymore 571# Remove repos which are not in index file anymore
575_clean_repos() { 572_clean_repos() {
576 comm -23 <(find $folder -maxdepth 1 -type d | sed -e '1d; s#.*/##' | sort) \ 573 comm -23 <(find $folder -maxdepth 1 -type d | sed -e '1d; s#.*/##' | sort) \
577 <(cut -d' ' -f2 < "$INDEX" | sed -e 's#.*/##' | sort 2> /dev/null ) \ 574 <(while read -r ${INDEX_FORMAT[@]}; do echo $url; done < "$INDEX" | sed -e 's#.*/##' | sort 2> /dev/null ) \
578 | while read -r dir; do 575 | while read -r dir; do
579 mv $folder/"$dir" /tmp && echo $folder/"$dir" is moved to /tmp 576 mv $folder/"$dir" /tmp && echo $folder/"$dir" is moved to /tmp
580 done 577 done
@@ -691,17 +688,20 @@ _create_gist() {
691 _set_gist "$@" || return 1 688 _set_gist "$@" || return 1
692 [[ -z ${files[*]} ]] && files+=($(_new_file "$filename")) 689 [[ -z ${files[*]} ]] && files+=($(_new_file "$filename"))
693 [[ -z $description ]] && read -e -r -p 'Type description: ' description < /dev/tty 690 [[ -z $description ]] && read -e -r -p 'Type description: ' description < /dev/tty
691 local index=$([[ $public == False ]] && echo p)$(( $(sed -e '/^s/ d' $INDEX | wc -l) +1 ))
694 692
695 echo 'Creating a new gist...' 693 echo 'Creating a new gist...'
696 http_data=$(tmp_file PATLOAD.CREATE) 694 http_data=$(tmp_file PATLOAD.CREATE)
697 echo -e "${files[*]}\n$description" \ 695 echo -e "${files[*]}\n$description" \
698 | _gist_body > "$http_data" \ 696 | _gist_body > "$http_data" \
699 && http_method POST $GITHUB_API/gists \ 697 && http_method POST $GITHUB_API/gists \
700 | xargs -I{} -0 echo "[{}]" \ 698 | sed -e '1 s/^/\[/; $ s/$/\]/' \
701 | start_from_num=$(( $(sed -e '/^s/ d' $INDEX | wc -l) +1 )) _parse_response \ 699 | index=$index _parse_response \
702 | tee -a $INDEX \ 700 | tee -a $INDEX \
703 | cut -d' ' -f2 | sed -E -e 's#.*/##' \ 701 | while read -r "${INDEX_FORMAT[@]}"; do
704 | (xargs -I{} git clone "$(_repo_url {})" $folder/{} &> /dev/null &) 702 local gist_id=${url/##*/}
703 (git clone "$(_repo_url $gist_id)" $folder/$gist_id &> /dev/null &)
704 done
705 705
706 # shellcheck disable=2181 706 # shellcheck disable=2181
707 if [[ $? -eq 0 ]]; then 707 if [[ $? -eq 0 ]]; then