-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_folder_download.sh
More file actions
71 lines (59 loc) · 1.92 KB
/
github_folder_download.sh
File metadata and controls
71 lines (59 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/bash
set -x # Enable debugging output
urlencode() {
local string="${1}"
local strlen=${#string}
local encoded=""
local pos c o
for (( pos=0 ; pos<strlen ; pos++ )); do
c=${string:$pos:1}
case "$c" in
[-_.~a-zA-Z0-9] ) o="${c}" ;;
* ) printf -v o '%%%02x' "'$c"
esac
encoded+="${o}"
done
echo "${encoded}"
}
download_folder() {
local path="$1"
local encoded_path=$(urlencode "$path")
local api_url="https://api.github.com/repos/$owner/$repo/contents/$encoded_path?ref=$commit"
local response=$(curl -sS -H "Accept: application/vnd.github.v3+json" "$api_url")
echo "$response" | jq -r '.[] | @base64' | while read -r item; do
decoded=$(echo $item | base64 --decode)
name=$(echo $decoded | jq -r '.name')
item_type=$(echo $decoded | jq -r '.type')
download_url=$(echo $decoded | jq -r '.download_url')
if [ "$item_type" == "file" ]; then
echo "Downloading: $name"
curl -sS -O "$download_url"
elif [ "$item_type" == "dir" ]; then
echo "Entering directory: $name"
mkdir -p "$name"
(cd "$name" && download_folder "$path/$name")
fi
done
}
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <github_permalink>"
exit 1
fi
# Extract owner, repo, commit, and path from the permalink
permalink=$1
owner=$(echo $permalink | cut -d'/' -f4)
repo=$(echo $permalink | cut -d'/' -f5)
commit=$(echo $permalink | cut -d'/' -f7)
path=$(echo $permalink | cut -d'/' -f8- | sed 's/%20/ /g')
echo "Owner: $owner"
echo "Repo: $repo"
echo "Commit: $commit"
echo "Path: $path"
# Create and enter the target directory
target_dir=$(basename "$path")
mkdir -p "$target_dir"
cd "$target_dir"
echo "Current directory: $(pwd)"
# Start the recursive download
download_folder "$path"
echo "Script completed. Files have been downloaded to: $(pwd)"