From 7e661ca2de9c3398f0f7027815909d146b46765e Mon Sep 17 00:00:00 2001 From: fatalerrors Date: Tue, 10 Mar 2026 17:45:26 +0100 Subject: [PATCH] add dwl (downloader wrapper) and make use of it, fixed myextip --- profile.d/info.sh | 2 +- profile.d/net.sh | 78 +++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 69 insertions(+), 11 deletions(-) diff --git a/profile.d/info.sh b/profile.d/info.sh index 0a887c7..e685b78 100644 --- a/profile.d/info.sh +++ b/profile.d/info.sh @@ -104,7 +104,7 @@ meteo() for city in "${cities[@]}"; do encoded=$(urlencode "$city") - curl -s "https://wttr.in/$encoded" || \ + dwl "https://wttr.in/$encoded" || \ disp E "Failed fetching datas for $city." done } diff --git a/profile.d/net.sh b/profile.d/net.sh index b04b5f6..6ad28a1 100644 --- a/profile.d/net.sh +++ b/profile.d/net.sh @@ -34,6 +34,54 @@ # * OF SUCH DAMAGE. # ------------------------------------------------------------------------------ +# ------------------------------------------------------------------------------ +# Download a resource using curl, wget, or fetch. +# Usage: dwl [output_file] +dwl() +{ + case "$1" in + --help|-h) + echo "Usage: dwl [output_file]" + echo "Downloads a resource using curl, wget, or fetch." + echo "" + echo "Arguments:" + echo " url The full URL to download (http/https/ftp)." + echo " output_file (Optional) Path to save the file. If omitted, prints to stdout." + return 0 + ;; + "") + echo "Error: URL argument is missing." >&2 + echo "Try 'get_resource --help' for usage." >&2 + return 1 + ;; + esac + + case "$1" in + http://*|https://*|ftp://*) ;; + *) + echo "Error: '$1' does not look like a valid URL. Must start with http://, https://, or ftp://" >&2 + return 1 + ;; + esac + + local url="$1" + local output="$2" + + if command -v curl >/dev/null 2>&1; then + [ -z "$output" ] && curl -sL "$url" || curl -sL -o "$output" "$url" + elif command -v wget >/dev/null 2>&1; then + [ -z "$output" ] && wget -qO- "$url" || wget -q -O "$output" "$url" + elif command -v fetch >/dev/null 2>&1; then + [ -z "$output" ] && fetch -o - "$url" || fetch -o "$output" "$url" + else + echo "Error: No download utility (curl, wget, or fetch) found." >&2 + return 1 + fi +} +export -f dwl +# ------------------------------------------------------------------------------ + + # ------------------------------------------------------------------------------ # Determine if parameter is a valid IPv4 address # Usage: isipv4 @@ -113,13 +161,16 @@ export -f urlencode # Usage: myextip [-i|--ip] [-s|--isp] [-l|--loc] [-c|--coord] # If no option is provided, all information will be displayed. # Options: +# -h, --help Display help screen # -i, --ip Display only the external IP address # -s, --isp Display only the ISP name # -l, --loc Display only the location (city, region, country) # -c, --coord Display only the coordinates (latitude, longitude) +# -a, --as Display only the Autonomous System (AS) information +# -R, --raw Display raw JSON response myextip() { local show_ip=false show_isp=false show_loc=false - local show_coord=false show_as=false + local show_coord=false show_as=false show_raw=false local all=true # Parse arguments @@ -145,15 +196,21 @@ myextip() { show_as=true all=false ;; + -R|--raw) + all=false + show_raw=true + ;; -h|--help) - printf "Usage: myextip [-i|--ip] [-s|--isp] [-l|--loc] [-c|--coord] [-a|--as]\n" printf "Fetch and display external IP information.\n\n" + printf "Usage: myextip [-i|--ip] [-s|--isp] [-l|--loc] [-c|--coord] [-a|--as] [-R|--raw]\n\n" printf "Options:\n" - printf " -i, --ip Display only the external IP address\n" - printf " -s, --isp Display only the ISP name\n" - printf " -l, --loc Display only the location (city, region, country)\n" - printf " -c, --coord Display only the coordinates (latitude, longitude)\n" - printf " -a, --as Display only the Autonomous System (AS) information\n" + printf "\t-h, --help\tDisplay this help screen\n" + printf "\t-i, --ip\tDisplay only the external IP address\n" + printf "\t-s, --isp\tDisplay only the ISP name\n" + printf "\t-l, --loc\tDisplay only the location (city, region, country)\n" + printf "\t-c, --coord\tDisplay only the coordinates (latitude, longitude)\n" + printf "\t-a, --as\tDisplay only the Autonomous System (AS) information\n" + printf "\t-R, --raw\tDisplay raw JSON response\n" return 0 ;; --) @@ -161,7 +218,7 @@ myextip() { break ;; *) - disp E "Unknown option: $1" + disp E "Unknown option: $1, use \"myextip --help\" to display usage." return 1 ;; esac @@ -170,10 +227,10 @@ myextip() { # Fetch data local response - response=$(curl -s http://ip-api.com/json/) + response=$(dwl http://ip-api.com/json/) # Parse with jq - if command -v jq >/dev/null 2>&1; then + if command -v jq >/dev/null 2>&1 && [[ "$show_raw" != true ]]; then echo "$response" | jq -r --argjson all "$all" --argjson ip "$show_ip" \ --argjson isp "$show_isp" --argjson loc "$show_loc" \ --argjson coord "$show_coord" --argjson as "$show_as" ' @@ -185,6 +242,7 @@ myextip() { (if $all or $as then "AS : \(.as)" else empty end) ] | .[]' else + [[ "$show_raw" != true ]] && disp W "jq is not installed, displaying raw JSON response." echo "$response" fi }