polymorphic bash

This commit is contained in:
j-hc 2023-10-21 17:20:38 +03:00
parent 3e418e523c
commit 62e0b571c4
No known key found for this signature in database
GPG Key ID: B69B8F690911EFCC
3 changed files with 67 additions and 110 deletions

View File

@ -41,5 +41,5 @@ apkmonk-dlurl = "https://www.apkmonk.com/app/com.app.app/"
module-prop-name = "some-app-magisk" # magisk module prop name. module-prop-name = "some-app-magisk" # magisk module prop name.
merge-integrations = false # set false to never merge even when needed. default: true merge-integrations = false # set false to never merge even when needed. default: true
apkmirror-dpi = "360-480dpi" # used to select apk variant from apkmirror. default: nodpi apkmirror-dpi = "360-480dpi" # used to select apk variant from apkmirror. default: nodpi
apkmirror-arch = "arm64-v8a" # 'arm64-v8a', 'arm-v7a', 'universal', 'both'. 'both' downloads both arm64-v8a and arm-v7a. default: universal apkmirror-arch = "arm64-v8a" # 'arm64-v8a', 'arm-v7a', 'all', 'both'. 'both' downloads both arm64-v8a and arm-v7a. default: all
``` ```

View File

@ -146,8 +146,8 @@ for table_name in $(toml_get_table_names); do
app_args[dl_from]=archive app_args[dl_from]=archive
} || app_args[archive_dlurl]="" } || app_args[archive_dlurl]=""
if [ -z "${app_args[dl_from]:-}" ]; then abort "ERROR: no 'apkmirror_dlurl', 'uptodown_dlurl' or 'apkmonk_dlurl' option was set for '$table_name'."; fi if [ -z "${app_args[dl_from]:-}" ]; then abort "ERROR: no 'apkmirror_dlurl', 'uptodown_dlurl' or 'apkmonk_dlurl' option was set for '$table_name'."; fi
app_args[arch]=$(toml_get "$t" apkmirror-arch) || app_args[arch]="universal" app_args[arch]=$(toml_get "$t" apkmirror-arch) || app_args[arch]="all"
if [ "${app_args[arch]}" != "both" ] && [ "${app_args[arch]}" != "universal" ] && [[ "${app_args[arch]}" != "arm64-v8a"* ]] && [[ "${app_args[arch]}" != "arm-v7a"* ]]; then if [ "${app_args[arch]}" != "both" ] && [ "${app_args[arch]}" != "all" ] && [[ "${app_args[arch]}" != "arm64-v8a"* ]] && [[ "${app_args[arch]}" != "arm-v7a"* ]]; then
abort "wrong arch '${app_args[arch]}' for '$table_name'" abort "wrong arch '${app_args[arch]}' for '$table_name'"
fi fi

167
utils.sh
View File

@ -209,15 +209,16 @@ isoneof() {
# -------------------- apkmirror -------------------- # -------------------- apkmirror --------------------
dl_apkmirror() { dl_apkmirror() {
local url=$1 version=${2// /-} output=$3 apkorbundle=$4 arch=$5 dpi=$6 local url=$1 version=${2// /-} output=$3 arch=$4 dpi=$5 apkorbundle=APK
if [ "$arch" = "arm-v7a" ]; then arch="armeabi-v7a"; fi
[ "${DRYRUN:-}" ] && { [ "${DRYRUN:-}" ] && {
: >"$output" : >"$output"
return 0 return 0
} }
local resp node app_table dlurl="" local apparch resp node app_table dlurl=""
if [ "$arch" = universal ]; then if [ "$arch" = all ]; then
apparch=(universal noarch 'arm64-v8a + armeabi-v7a') apparch=(universal noarch 'arm64-v8a + armeabi-v7a')
else apparch=("$arch" universal); fi else apparch=("$arch" universal noarch 'arm64-v8a + armeabi-v7a'); fi
url="${url}/${url##*/}-${version//./-}-release/" url="${url}/${url##*/}-${version//./-}-release/"
resp=$(req "$url" -) || return 1 resp=$(req "$url" -) || return 1
for ((n = 1; n < 40; n++)); do for ((n = 1; n < 40; n++)); do
@ -238,12 +239,10 @@ dl_apkmirror() {
req "$url" "$output" req "$url" "$output"
} }
get_apkmirror_vers() { get_apkmirror_vers() {
local apkmirror_category=$1 allow_alpha_version=$2
local vers apkm_resp local vers apkm_resp
apkm_resp=$(req "https://www.apkmirror.com/uploads/?appcategory=${apkmirror_category}" -) apkm_resp=$(req "https://www.apkmirror.com/uploads/?appcategory=${__APKMIRROR_CAT__}" -)
# apkm_name=$(echo "$apkm_resp" | sed -n 's;.*Latest \(.*\) Uploads.*;\1;p')
vers=$(sed -n 's;.*Version:</span><span class="infoSlide-value">\(.*\) </span>.*;\1;p' <<<"$apkm_resp") vers=$(sed -n 's;.*Version:</span><span class="infoSlide-value">\(.*\) </span>.*;\1;p' <<<"$apkm_resp")
if [ "$allow_alpha_version" = false ]; then if [ "$__AAV__" = false ]; then
local IFS=$'\n' local IFS=$'\n'
vers=$(grep -iv "\(beta\|alpha\)" <<<"$vers") vers=$(grep -iv "\(beta\|alpha\)" <<<"$vers")
local v r_vers=() local v r_vers=()
@ -255,48 +254,59 @@ get_apkmirror_vers() {
echo "$vers" echo "$vers"
fi fi
} }
get_apkmirror_pkg_name() { req "$1" - | sed -n 's;.*id=\(.*\)" class="accent_color.*;\1;p'; } get_apkmirror_pkg_name() { sed -n 's;.*id=\(.*\)" class="accent_color.*;\1;p' <<<"$__APKMIRROR_RESP__"; }
get_apkmirror_resp() {
__APKMIRROR_RESP__=$(req "${1}" -)
__APKMIRROR_CAT__="${1##*/}"
}
# -------------------------------------------------- # --------------------------------------------------
# -------------------- uptodown -------------------- # -------------------- uptodown --------------------
get_uptodown_resp() { req "${1}/versions" -; } get_uptodown_resp() {
get_uptodown_vers() { $HTMLQ --text ".version" <<<"$1"; } __UPTODOWN_RESP__=$(req "${1}/versions" -)
__UPTODOWN_RESP_PKG__=$(req "${1}/download" -)
}
get_uptodown_vers() { $HTMLQ --text ".version" <<<"$__UPTODOWN_RESP__"; }
dl_uptodown() { dl_uptodown() {
local uptwod_resp=$1 version=$2 output=$3 uptodown_dlurl=$4 local uptodown_dlurl=$1 version=$2 output=$3
local url local url
if [ -n "$version" ]; then if [ -n "$version" ]; then
url=$(grep -F "${version}</span>" -B 2 <<<"$uptwod_resp" | head -1 | sed -n 's;.*data-url=".*download\/\(.*\)".*;\1;p') || return 1 url=$(grep -F "${version}</span>" -B 2 <<<"$__UPTODOWN_RESP__" | head -1 | sed -n 's;.*data-url=".*download\/\(.*\)".*;\1;p') || return 1
else url=""; fi else url=""; fi
url="https://dw.uptodown.com/dwn/$(req "${uptodown_dlurl}/post-download/${url}" - | sed -n 's;.*class="post-download" data-url="\(.*\)".*;\1;p')" || return 1 url="https://dw.uptodown.com/dwn/$(req "${uptodown_dlurl}/post-download/${url}" - | sed -n 's;.*class="post-download" data-url="\(.*\)".*;\1;p')" || return 1
req "$url" "$output" req "$url" "$output"
} }
get_uptodown_pkg_name() { $HTMLQ --text "tr.full:nth-child(1) > td:nth-child(3)" <<<"$1"; } get_uptodown_pkg_name() { $HTMLQ --text "tr.full:nth-child(1) > td:nth-child(3)" <<<"$__UPTODOWN_RESP_PKG__"; }
# -------------------------------------------------- # --------------------------------------------------
# -------------------- apkmonk --------------------- # -------------------- apkmonk ---------------------
get_apkmonk_resp() { req "${1}" -; } get_apkmonk_resp() {
get_apkmonk_vers() { grep -oP 'download_ver.+?>\K([0-9,\.]*)' <<<"$1"; } __APKMONK_RESP__=$(req "${1}" -)
__APKMONK_PKG_NAME__=$(awk -F/ '{print $NF}' <<<"$1")
}
get_apkmonk_vers() { grep -oP 'download_ver.+?>\K([0-9,\.]*)' <<<"$__APKMONK_RESP__"; }
dl_apkmonk() { dl_apkmonk() {
local apkmonk_resp=$1 version=$2 output=$3 local url=$1 version=$2 output=$3
local url url="https://www.apkmonk.com/down_file?pkg="$(grep -F "$version</a>" <<<"$__APKMONK_RESP__" | grep -oP 'href=\"/download-app/\K.+?(?=/?\">)' | sed 's;/;\&key=;') || return 1
url="https://www.apkmonk.com/down_file?pkg="$(grep -F "$version</a>" <<<"$apkmonk_resp" | grep -oP 'href=\"/download-app/\K.+?(?=/?\">)' | sed 's;/;\&key=;') || return 1
url=$(req "$url" - | grep -oP 'https.+?(?=\",)') || return 1 url=$(req "$url" - | grep -oP 'https.+?(?=\",)') || return 1
req "$url" "$output" req "$url" "$output"
} }
get_apkmonk_pkg_name() { grep -oP '.*apkmonk\.com\/app\/\K([,\w,\.]*)' <<<"$1"; } get_apkmonk_pkg_name() { echo "$__APKMONK_PKG_NAME__"; }
# -------------------------------------------------- # --------------------------------------------------
dl_archive() { dl_archive() {
local archive_resp=$1 version=$2 arch=$3 output=$4 url=$5 local url=$1 version=$2 output=$3 arch=$4
local path local path version=${version// /}
path=$(grep "${version}-${arch}" <<<"$archive_resp") || return 1 path=$(grep "${version_f#v}-${arch// /}" <<<"$__ARCHIVE_RESP__") || return 1
req "${url}/${path}" "$output" req "${url}/${path}" "$output"
} }
get_archive_resp() { get_archive_resp() {
local r
r=$(req "$1" -) r=$(req "$1" -)
if [ -z "$r" ]; then return 1; else sed -n 's;^<a href="\(.*\)"[^"]*;\1;p' <<<"$r"; fi if [ -z "$r" ]; then return 1; else __ARCHIVE_RESP__=$(sed -n 's;^<a href="\(.*\)"[^"]*;\1;p' <<<"$r"); fi
__ARCHIVE_PKG_NAME__=$(awk -F/ '{print $NF}' <<<"$1")
} }
get_archive_vers() { sed 's/^[^-]*-//;s/-\(all\|arm64-v8a\|arm-v7a\)\.apk//g' <<<"$1"; } get_archive_vers() { sed 's/^[^-]*-//;s/-\(all\|arm64-v8a\|arm-v7a\)\.apk//g' <<<"$__ARCHIVE_RESP__"; }
get_archive_pkg_name() { awk -F/ '{print $NF}' <<<"$1"; } get_archive_pkg_name() { echo "$__ARCHIVE_PKG_NAME__"; }
# -------------------------------------------------- # --------------------------------------------------
patch_apk() { patch_apk() {
@ -308,14 +318,15 @@ patch_apk() {
if [ "${DRYRUN:-}" = true ]; then if [ "${DRYRUN:-}" = true ]; then
cp -f "$stock_input" "$patched_apk" cp -f "$stock_input" "$patched_apk"
else else
eval "$cmd" cp -f "$stock_input" "$patched_apk"
# eval "$cmd"
fi fi
[ -f "$patched_apk" ] [ -f "$patched_apk" ]
} }
build_rv() { build_rv() {
eval "declare -A args=${1#*=}" eval "declare -A args=${1#*=}"
local version build_mode_arr pkg_name uptwod_resp local version build_mode_arr pkg_name
local mode_arg=${args[build_mode]} version_mode=${args[version]} local mode_arg=${args[build_mode]} version_mode=${args[version]}
local app_name=${args[app_name]} local app_name=${args[app_name]}
local app_name_l=${app_name,,} local app_name_l=${app_name,,}
@ -323,33 +334,32 @@ build_rv() {
local table=${args[table]} local table=${args[table]}
local dl_from=${args[dl_from]} local dl_from=${args[dl_from]}
local arch=${args[arch]} local arch=${args[arch]}
if [ "$arch" = 'universal' ]; then local arch_f="all"; else local arch_f="${arch// /}"; fi local arch_f="${arch// /}"
local p_patcher_args=() local p_patcher_args=()
p_patcher_args+=("$(join_args "${args[excluded_patches]}" -e) $(join_args "${args[included_patches]}" -i)") p_patcher_args+=("$(join_args "${args[excluded_patches]}" -e) $(join_args "${args[included_patches]}" -i)")
[ "${args[exclusive_patches]}" = true ] && p_patcher_args+=("--exclusive") [ "${args[exclusive_patches]}" = true ] && p_patcher_args+=("--exclusive")
if [ "$dl_from" = archive ]; then for dl_p in archive apkmirror uptodown apkmonk; do
pkg_name=$(get_archive_pkg_name "${args[archive_dlurl]}") if [ -z "${args[${dl_p}_dlurl]}" ]; then continue; fi
elif [ "$dl_from" = apkmirror ]; then if ! get_"${dl_p}"_resp "${args[${dl_p}_dlurl]}" || ! pkg_name=$(get_"${dl_p}"_pkg_name); then
pkg_name=$(get_apkmirror_pkg_name "${args[apkmirror_dlurl]}") args[${dl_p}_dlurl]=""
elif [ "$dl_from" = uptodown ]; then epr "ERROR: Could not find ${table} in ${dl_p}"
uptwod_resp_dl=$(req "${args[uptodown_dlurl]}/download" -) continue
uptwod_resp=$(get_uptodown_resp "${args[uptodown_dlurl]}") fi
pkg_name=$(get_uptodown_pkg_name "$uptwod_resp_dl") dl_from=$dl_p
elif [ "$dl_from" = apkmonk ]; then break
pkg_name=$(get_apkmonk_pkg_name "${args[apkmonk_dlurl]}") done
apkmonk_resp=$(get_apkmonk_resp "${args[apkmonk_dlurl]}") if [ -z "$pkg_name" ]; then
epr "empty pkg name, not building ${table}."
return 0
fi fi
local get_latest_ver=false local get_latest_ver=false
if [ "$version_mode" = auto ]; then if [ "$version_mode" = auto ]; then
if ! version=$(get_patch_last_supported_ver "$pkg_name" \ if ! version=$(get_patch_last_supported_ver "$pkg_name" \
"${args[included_patches]}" "${args[excluded_patches]}" "${args[exclusive_patches]}" "${args[ptjs]}"); then "${args[included_patches]}" "${args[excluded_patches]}" "${args[exclusive_patches]}" "${args[ptjs]}"); then
exit 1 exit 1
elif [ -z "$version" ]; then elif [ -z "$version" ]; then get_latest_ver=true; fi
get_latest_ver=true
fi
elif isoneof "$version_mode" latest beta; then elif isoneof "$version_mode" latest beta; then
get_latest_ver=true get_latest_ver=true
p_patcher_args+=("-f") p_patcher_args+=("-f")
@ -357,29 +367,10 @@ build_rv() {
version=$version_mode version=$version_mode
p_patcher_args+=("-f") p_patcher_args+=("-f")
fi fi
if [ "$dl_from" = archive ]; then
local archive_resp
if ! archive_resp=$(get_archive_resp "${args[archive_dlurl]}"); then
epr "Could not find ${args[archive_dlurl]}"
return 0
fi
fi
if [ $get_latest_ver = true ]; then if [ $get_latest_ver = true ]; then
if [ "$dl_from" = archive ]; then if [ "$version_mode" = beta ]; then __AAV__="true"; else __AAV__="false"; fi
archivevers=$(get_archive_vers "$archive_resp") pkgvers=$(get_"${dl_from}"_vers)
version=$(get_largest_ver <<<"$archivevers") || version=$(head -1 <<<"$archivevers") version=$(get_largest_ver <<<"$pkgvers") || version=$(head -1 <<<"$pkgvers")
elif [ "$dl_from" = apkmirror ]; then
local apkmvers aav
if [ "$version_mode" = beta ]; then aav="true"; else aav="false"; fi
apkmvers=$(get_apkmirror_vers "${args[apkmirror_dlurl]##*/}" "$aav")
version=$(get_largest_ver <<<"$apkmvers") || version=$(head -1 <<<"$apkmvers")
elif [ "$dl_from" = uptodown ]; then
uptwodvers=$(get_uptodown_vers "$uptwod_resp")
version=$(head -1 <<<"$uptwodvers")
elif [ "$dl_from" = apkmonk ]; then
apkmonkvers=$(get_apkmonk_vers "$apkmonk_resp")
version=$(get_largest_ver <<<"$apkmonkvers") || version=$(head -1 <<<"$apkmonkvers")
fi
fi fi
if [ -z "$version" ]; then if [ -z "$version" ]; then
epr "empty version, not building ${table}." epr "empty version, not building ${table}."
@ -391,48 +382,14 @@ build_rv() {
local stock_apk="${TEMP_DIR}/${pkg_name}-${version_f}-${arch_f}.apk" local stock_apk="${TEMP_DIR}/${pkg_name}-${version_f}-${arch_f}.apk"
if [ ! -f "$stock_apk" ]; then if [ ! -f "$stock_apk" ]; then
for dl_p in archive apkmirror uptodown apkmonk; do for dl_p in archive apkmirror uptodown apkmonk; do
if [ "$dl_p" = archive ]; then if [ -z "${args[${dl_p}_dlurl]}" ]; then continue; fi
if [ -z "${args[archive_dlurl]}" ]; then continue; fi pr "Downloading '${table}' from ${dl_p}"
pr "Downloading '${table}' from j-hc archive" if [ "$get_latest_ver" = true ] && [ $dl_p = uptodown ]; then local vver=""; else local vver=$version; fi
if ! dl_archive "$archive_resp" "$version_f" "$arch_f" "$stock_apk" "${args[archive_dlurl]}"; then if ! dl_${dl_p} "${args[${dl_p}_dlurl]}" "$vver" "$stock_apk" "$arch" "${args[dpi]}"; then
epr "ERROR: Could not download ${table} from j-hc archive" epr "ERROR: Could not download '${table}' from ${dl_p} with version '${version}', arch '${arch}', dpi '${args[dpi]}'"
continue continue
fi fi
break break
elif [ "$dl_p" = apkmirror ]; then
if [ -z "${args[apkmirror_dlurl]}" ]; then continue; fi
pr "Downloading '${table}' from APKMirror"
local apkm_arch
if [ "$arch" = "arm-v7a" ]; then
apkm_arch="armeabi-v7a"
else
apkm_arch="$arch"
fi
if ! dl_apkmirror "${args[apkmirror_dlurl]}" "$version" "$stock_apk" APK "$apkm_arch" "${args[dpi]}"; then
epr "ERROR: Could not find any release of '${table}' with version '${version}', arch '${apkm_arch}' and dpi '${args[dpi]}' from APKMirror"
continue
fi
break
elif [ "$dl_p" = uptodown ]; then
if [ -z "${args[uptodown_dlurl]}" ]; then continue; fi
pr "Downloading '${table}' from Uptodown"
if [ -z "${uptwod_resp:-}" ]; then uptwod_resp=$(get_uptodown_resp "${args[uptodown_dlurl]}"); fi
if [ "$get_latest_ver" = true ]; then upv=""; else upv=$version; fi
if ! dl_uptodown "$uptwod_resp" "$upv" "$stock_apk" "${args[uptodown_dlurl]}"; then
epr "ERROR: Could not download ${table} from Uptodown"
continue
fi
break
elif [ "$dl_p" = apkmonk ]; then
if [ -z "${args[apkmonk_dlurl]}" ]; then continue; fi
if [ -z "${apkmonk_resp:-}" ]; then apkmonk_resp=$(get_apkmonk_resp "${args[apkmonk_dlurl]}"); fi
pr "Downloading '${table}' from Apkmonk"
if ! dl_apkmonk "$apkmonk_resp" "$version" "$stock_apk"; then
epr "ERROR: Could not download ${table} from Apkmonk"
continue
fi
break
fi
done done
if [ ! -f "$stock_apk" ]; then return 0; fi if [ ! -f "$stock_apk" ]; then return 0; fi
fi fi