function download(), downloads URL to file in data-bot-bash

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2019-05-20 18:42:21 +02:00
parent e5f7b2da50
commit d34c22f156
3 changed files with 31 additions and 7 deletions

View File

@ -12,7 +12,7 @@
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
# #
#### $$VERSION$$ v0.80-pre-4-gd1a3372 #### $$VERSION$$ v0.80-pre-5-ge5f7b2d
# #
# Exit Codes: # Exit Codes:
# - 0 sucess (hopefully) # - 0 sucess (hopefully)
@ -132,6 +132,20 @@ fi
# internal functions # internal functions
# $1 URL, $2 filename in TMPDIR
# outputs final filename
download() {
local empty="no.file" file="${2:-${empty}}"
if [[ "$file" = *"/"* ]] || [[ "$file" = "."* ]]; then file="${empty}"; fi
while [ -f "${TMPDIR:-.}/${file}" ] ; do file="$RAMDOM-${file}"; done
getJson "$1" >"${TMPDIR:-.}/${file}" || return
printf '%s\n' "${TMPDIR:-.}/${file}"
}
# easy handling of users:
_is_botadmin() {
user_is_botadmin "${USER[ID]}"
}
# $1 postfix, e.g. chatid # $1 postfix, e.g. chatid
# $2 prefix, back- or startbot- # $2 prefix, back- or startbot-
procname(){ procname(){

View File

@ -544,6 +544,20 @@ You must include ```source modules/aliases.sh``` in 'commands.sh' to have the f
### Helper functions ### Helper functions
##### download
Download the fiven URL ans returns the final filename in TMPDIR. If the given filename exists,the filename is prefixed with a
random number. filename is not allowed to contain '/' or '..'.
*usage:* download URL filename
*example:*
```bash
file="$(download "https://avatars.githubusercontent.com/u/13046303" "avatar.jpg")"
echo "$file" -> ./data-bot-bash/avatar.jpg
file="$(download "https://avatars.githubusercontent.com/u/13046303" "avatar.jpg")"
echo "$file" -> ./data-bot-bash/12345-avatar.jpg
```
##### _exists ##### _exists
Returns true if the given function exist, can be used to check if a module is loaded. Returns true if the given function exist, can be used to check if a module is loaded.
@ -629,5 +643,5 @@ The name of your bot is availible as bash variable "$ME", there is no need to ca
#### [Prev Best Practice](5_practice.md) #### [Prev Best Practice](5_practice.md)
#### [Next Notes for Developers](7_develop.md) #### [Next Notes for Developers](7_develop.md)
#### $$VERSION$$ v0.80-pre-0-gdd7c66d #### $$VERSION$$ v0.80-pre-5-ge5f7b2d

View File

@ -5,14 +5,10 @@
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
# #
#### $$VERSION$$ v0.80-pre-0-gdd7c66d #### $$VERSION$$ v0.80-pre-5-ge5f7b2d
# #
# source from commands.sh to use the aliases # source from commands.sh to use the aliases
# easy handling of users:
_is_botadmin() {
user_is_botadmin "${USER[ID]}"
}
_is_admin() { _is_admin() {
user_is_admin "${CHAT[ID]}" "${USER[ID]}" user_is_admin "${CHAT[ID]}" "${USER[ID]}"
} }