#1 Hail Paul

Merged
darmor merged 1 commits from Tsutsu/master into darmor/master 6 years ago
1 changed files with 54 additions and 31 deletions
  1. 54 31
      pastebin_scraper.sh

+ 54 - 31
pastebin_scraper.sh

@@ -1,33 +1,56 @@
 #!/bin/bash
 
-if [ $# -eq 0 ];
-then
-    echo "Usage: pastebin_scraper [TERM]"
-    echo "example: patebin_scraper cats"
-    echo "example: patebin_scraper cats -c"
-fi
-# create directory if none exists
-SAVE_DIR=~/.paste_bin_scraper
-if [ -d "$SAVE_DIR" ]; 
-then 
-    echo "Files will be saved to $SAVE_DIR"
-else
-    mkdir ~/.paste_bin_scraper
-fi 
-
-if [ "$2" == "-c" ]; 
-then 
-    echo "Search term $1"
-    echo "Total files: "$(curl -s "https://psbdmp.ws/api/search/$1" | jq ".data" | wc -l)
-
-else
-    echo "Downloading files please wait"
-    # download search term files and store in directory 
-    curl "https://psbdmp.ws/api/search/$1" \
-                    | jq ".data" \
-                    | fgrep "id" \
-                    | cut -d: -f 2 \
-                    | sed 's/[\" \,]//g' \
-                    | awk '{print "https://pastebin.com/raw/"$1}'\
-                    | xargs -I{} wget -nc {} -P "$SAVE_DIR"
-fi 
+usage() {
+    cat <<EOF
+Usage: $0 -ch -d [ DIRECTORY ] -- [ TERM ... ]
+
+Example:
+
+    $0 cats
+    $0 -h
+    $0 -c -dl ~/HOME/pastes/ -- cats dogs
+EOF
+exit 0
+}
+
+err() {
+    printf 'E: %s\n' "$@"
+    exit 1
+}
+
+(( $# )) || usage
+
+while (( $# )); do
+    case "$1" in
+        (-d) [[ $2 ]] || err "-d requires an argument"
+            dir="$2";
+            shift
+            ;;
+        (-c) dl=1;;
+        (-h) usage;;
+        (--) shift; break;;
+        (*) terms+=("$1");;
+    esac
+    shift
+done
+terms+=("$@")
+
+(( ${#terms[@]} )) || err "You forgot to give search terms."
+
+SAVE_DIR=${dir:-~/.paste_bin_scraper}
+mkdir -p -- "$SAVE_DIR"
+echo "Files will be saved to $SAVE_DIR"
+
+for term in "${terms[@]}"; do
+    if (( dl )); then
+        echo "Downloading files please wait"
+        curl -s -- "https://psbdmp.ws/api/search/$term" \
+            | jq ".data" \
+            | awk -v FS=\" '$2 == "id" {print "https://pastebin.com/raw/"$4} ' \
+            | xargs -I{} -- wget -P "$SAVE_DIR" -nc -- {}
+    else
+        echo "Search term $term"
+        echo "Total files: $(curl -s "https://psbdmp.ws/api/search/$term" | jq ".data" | wc -l)"
+    fi
+
+done