-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilite-list.sh
executable file
·159 lines (139 loc) · 4.2 KB
/
filite-list.sh
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
# Made by Kamen Mladenov, aka Syndamia, under the GNU GPL-3.0 license
# Source: https://github.com/Syndamia/filite-list
help_info=$(cat <<HELP
filite-list.sh [OPTIONS] -- script to show you the IDs and their values of filite entries on a given server
Where:
-v, --version Shows the version of the script
-h, --help, -? Display this help message
-d, --dependencies List required dependencies for the script
-ho, --host [LINK] Use the given host. MUST be in the format of "https://example:port.com" (port is not required), without the trailing forward slash!
-f, --files Show only the file entries
-l, --links Show only the link entries
-t, --text Show only the text entries
-oi, --only-id Show only IDs of entries (and NOT their values)
-n, --number-limit [AMOUNT] Limit how many entries to show from each type
-nid, --numerical-id Show the numerical IDs, rather than the text IDs (the ones that are used in links)
-a, --auth Authenticate with these credentials
HELP
)
filite_host="https://filite.raphaeltheriault.com"
types=(f l t)
type_name=(Files Links Texts)
info_name=(filepath forward contents)
# Process arguments
overwrite_shown=-1
only_link=false
show_limit=-1
show_numerical_id=false
while [ ! -z $1 ]; do
case $1 in
-v|--version)
printf "filite-list.sh 1.2\n"
exit
;;
-d|--dependencies)
printf "\nDependencies:\n-------------\ncurl\njq\n\n"
exit
;;
-ho|--host)
shift
filite_host=$1
;;
-f|--files)
overwrite_shown=0
;;
-l|--types)
overwrite_shown=1
;;
-t|--text)
overwrite_shown=2
;;
-oi|--only-id)
only_link=true
;;
-n|--number-limit)
shift
show_limit=$1
;;
-nid|--numerical-id)
show_numerical_id=true
;;
-a|--auth)
shift
auth=$1
if [[ $auth != *":"* ]]; then
auth=":$1"
fi
;;
-h|--help|-?)
printf "$help_info\n\n"
exit
;;
*) ;;
esac
shift
done
if ! [ -x "$(command -v curl)" ]; then
printf "You need to install curl!\n"
exit
elif ! [ -x "$(command -v jq)" ]; then
printf "You need to install jq!\n"
exit
fi
# Automatically switch on show_numerical_id, when the server doesn't support getting string IDs from numeric ID
if [ -z "$(curl -u $auth -sX GET $filite_host/id/0)" ] && [[ $show_numerical_id == false ]]; then
printf "\nWARNING: The host's version of filite doesn't support get string IDs by giving numerical IDs!\n Only numerical ID's will be shown!\n"
show_numerical_id=true
fi
# Show entries from only one category
if (( overwrite_shown > -1 ))
then
types=(${types[overwrite_shown]})
type_name=(${type_name[overwrite_shown]})
info_name=(${info_name[overwrite_shown]})
fi
# Main logic
l=0
while (( l < ${#types[@]} )); do
printf '\n%s\n------\n' "${type_name[$l]}"
# Check if you can even get the type entries
if (( 399 < $(curl -s -o /dev/null -u $auth -w "%{http_code}" $filite_host/${types[$l]}) )); then
printf "Server returned an error!\n"
((l++))
continue
fi
# Get all available info and ids
data=$(curl -sX GET -u $auth $filite_host/${types[$l]})
ids=($(echo $data | jq -r .[].id | tr ' ' '\n'))
if [[ $only_link == false ]]; then
# Get all desired information (content or smth else) and from each one:
# - take from beginning to first space or new line or tab or carriage return
# - or take from beginning to end (if those characters don't exist)
# - take only the first 50 characters
info=($(echo $data | jq -r ".[].${info_name[$l]} | match(\"^.*?(?=[ \n\t\r])|^.*$\") | .string | .[0:50]"))
fi
# Print information for each file/link/text
i=0
while (( i < ${#ids[@]} && ( show_limit < 0 || i < show_limit ) )); do
# Make the id, depending on script parameters
if [[ $show_numerical_id == false ]]; then
id=$(curl -u $auth -sX GET $filite_host/id/${ids[$i]})
else
id=${ids[$i]}
fi
# Don't print info, if it isn't wanted
if [[ $only_link == false ]]; then
if [[ $show_numerical_id == false ]]; then
printf '%-6s : %s \n' "$id" "${info[$i]}"
else
printf '%-10s : %s \n' "$id" "${info[$i]}"
fi
else
printf "$id\n"
fi
((i++))
done
((l++))
done
printf '\n'