2022-10-09 17:37:34 -05:00
#!/usr/bin/env bash
declare -A apks
2022-10-09 21:33:06 -05:00
apks[ "com.google.android.youtube" ] = dl_yt
apks[ "com.google.android.apps.youtube.music" ] = dl_ytm
2022-10-09 17:37:34 -05:00
2022-11-01 20:22:31 -05:00
flag = $2
2023-03-22 18:28:43 -05:00
# Read the settings
source " $1 /build_settings "
2022-10-09 17:37:34 -05:00
## Functions
# Wget user agent
WGET_HEADER = "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0"
# Wget function
req( ) { wget -nv -O " $2 " --header= " $WGET_HEADER " " $1 " ; }
2022-10-12 23:29:22 -05:00
# # Wget apk verions
# get_apk_vers() { req "$1" - | sed -n 's;.*Version:</span><span class="infoSlide-value">\(.*\) </span>.*;\1;p'; }
# # Wget apk verions(largest)
# get_largest_ver() {
# local max=0
# while read -r v || [ -n "$v" ]; do
# if [[ ${v//[!0-9]/} -gt ${max//[!0-9]/} ]]; then max=$v; fi
# done
# if [[ $max = 0 ]]; then echo ""; else echo "$max"; fi
# }
2022-10-09 17:37:34 -05:00
2022-10-13 01:27:01 -05:00
# Returns if $1 is less than $2
ver_less_than( ) {
2023-07-27 15:26:08 -05:00
[ [ ${ 1 : 0 : 1 } = = "v" ] ] && var1 = ${ 1 : 1 } || var1 = $1
[ [ ${ 2 : 0 : 1 } = = "v" ] ] && var2 = ${ 2 : 1 } || var2 = $2
[ [ $( echo $var1 $'\n' $var2 | sort -V | tail -n1) != $var1 ] ] && echo true || echo false
2022-10-13 01:27:01 -05:00
}
2022-10-09 17:37:34 -05:00
# Wget download apk
dl_apk( ) {
local url = $1 regexp = $2 output = $3
url = " https://www.apkmirror.com $( req " $url " - | tr '\n' ' ' | sed -n " s/href=\"/@/g; s;.* ${ regexp } .*;\1;p " ) "
2022-11-01 20:22:31 -05:00
echo " $url "
2022-10-09 17:37:34 -05:00
url = " https://www.apkmirror.com $( req " $url " - | tr '\n' ' ' | sed -n 's;.*href="\(.*key=[^"]*\)">.*;\1;p' ) "
url = " https://www.apkmirror.com $( req " $url " - | tr '\n' ' ' | sed -n 's;.*href="\(.*key=[^"]*\)">.*;\1;p' ) "
req " $url " " $output "
}
# Downloading youtube
dl_yt( ) {
2022-11-01 20:22:31 -05:00
if [ [ $flag = = checkonly ] ] ; then
echo " [checkonly] YouTube has an update ( $version_present -> $version ) "
return
2022-10-13 23:34:05 -05:00
fi
2022-11-01 20:22:31 -05:00
echo "Downloading YouTube"
2022-10-09 17:37:34 -05:00
local last_ver
last_ver = " $version "
2022-10-12 23:29:22 -05:00
# last_ver="${last_ver:-$(get_apk_vers "https://www.apkmirror.com/uploads/?appcategory=youtube" | get_largest_ver)}"
2022-10-09 17:37:34 -05:00
2022-11-01 20:22:31 -05:00
echo " Choosing version ' ${ last_ver } ' "
2022-10-09 17:37:34 -05:00
local base_apk = "com.google.android.youtube.apk"
2022-10-11 18:29:50 -05:00
declare -r dl_url = $( dl_apk " https://www.apkmirror.com/apk/google-inc/youtube/youtube- ${ last_ver //./- } -release/ " \
"APK</span>[^@]*@\([^#]*\)" \
" $base_apk " )
2022-11-01 20:22:31 -05:00
echo " YouTube version: ${ last_ver } "
echo " downloaded from: [APKMirror - YouTube]( $dl_url ) "
2023-03-22 18:28:43 -05:00
jq " .\" $apk \" = \" $last_ver \" " versions.json >versions.json.tmp && mv versions.json.tmp versions.json
2022-10-09 17:37:34 -05:00
}
# Architectures
ARM64_V8A = "arm64-v8a"
ARM_V7A = "arm-v7a"
# Downloading youtube music
dl_ytm( ) {
2022-11-01 20:22:31 -05:00
if [ [ $flag = = checkonly ] ] ; then
echo " [checkonly] YouTube Music has an update ( $version_present -> $version ) "
return
2022-10-13 23:34:05 -05:00
fi
2022-10-09 17:37:34 -05:00
local arch = $ARM64_V8A
2022-11-01 20:22:31 -05:00
echo " Downloading YouTube Music ( ${ arch } ) "
2022-10-09 17:37:34 -05:00
local last_ver
last_ver = " $version "
2022-10-12 23:29:22 -05:00
# last_ver="${last_ver:-$(get_apk_vers "https://www.apkmirror.com/uploads/?appcategory=youtube-music" | get_largest_ver)}"
2022-10-09 17:37:34 -05:00
2022-11-01 20:22:31 -05:00
echo " Choosing version ' ${ last_ver } ' "
2022-10-09 17:37:34 -05:00
local base_apk = "com.google.android.apps.youtube.music.apk"
2022-10-11 18:29:50 -05:00
if [ " $arch " = " $ARM64_V8A " ] ; then
local regexp_arch = 'arm64-v8a</div>[^@]*@\([^"]*\)'
elif [ " $arch " = " $ARM_V7A " ] ; then
local regexp_arch = 'armeabi-v7a</div>[^@]*@\([^"]*\)'
2022-10-09 17:37:34 -05:00
fi
2022-10-11 18:29:50 -05:00
declare -r dl_url = $( dl_apk " https://www.apkmirror.com/apk/google-inc/youtube-music/youtube-music- ${ last_ver //./- } -release/ " \
" $regexp_arch " \
" $base_apk " )
2022-11-01 20:22:31 -05:00
echo " \nYouTube Music ( ${ arch } ) version: ${ last_ver } "
echo " downloaded from: [APKMirror - YouTube Music ${ arch } ]( $dl_url ) "
2023-03-22 18:28:43 -05:00
jq " .\" $apk \" = \" $last_ver \" " versions.json >versions.json.tmp && mv versions.json.tmp versions.json
2022-10-09 17:37:34 -05:00
}
2022-10-11 18:29:50 -05:00
# Get into the build directory
2022-10-11 20:33:46 -05:00
if [ -d " $1 " ] ; then
2023-03-22 18:28:43 -05:00
cd " $1 "
2022-10-11 20:25:44 -05:00
else
2023-03-22 18:28:43 -05:00
echo "Working directory not provided"
exit -1
2022-10-11 20:25:44 -05:00
fi
2022-10-11 18:29:50 -05:00
2022-10-09 17:37:34 -05:00
## Main
2023-01-28 12:59:25 -06:00
try = 0
2023-03-22 18:28:43 -05:00
while :; do
try = $(( $try + 1 ))
2023-01-28 12:59:25 -06:00
[ $try -gt 10 ] && echo "API error!" && exit 3
curl -X 'GET' 'https://releases.revanced.app/patches' -H 'accept: application/json' -o patches.json
cat patches.json | jq -e '.error' >/dev/null 2>& 1 || break
2023-03-22 18:28:43 -05:00
echo " API failure, trying again. $(( 10 - $try )) tries left... "
2023-01-28 12:59:25 -06:00
sleep 10
done
2022-10-09 17:37:34 -05:00
for apk in " ${ !apks[@] } " ; do
2023-03-22 18:28:43 -05:00
# Skip if app not specified for build
[ [ " $apk " = = "com.google.android.youtube" && " $YT_NONROOT " = = false && " $YT_ROOT " = = false ] ] && continue
[ [ " $apk " = = "com.google.android.apps.youtube.music" && " $YTM_NONROOT " = = false && " $YTM_ROOT " = = false ] ] && continue
2022-11-01 20:22:31 -05:00
echo " Checking $apk "
2022-10-13 00:03:26 -05:00
supported_vers = " $( jq -r '.[].compatiblePackages[] | select(.name == "' $apk '") | .versions | last' patches.json) "
version = 0
for vers in $supported_vers ; do
2022-10-13 01:27:01 -05:00
[ $vers != "null" ] && [ [ $( ver_less_than $vers $version ) = = true || $version = = 0 ] ] && version = $vers
2022-10-13 00:03:26 -05:00
done
version_present = $( jq -r " .\" $apk \" " versions.json)
2022-11-01 20:22:31 -05:00
[ [ $( ver_less_than $version_present $version ) = = true || ! -f $apk .apk || $2 = = force ] ] && ${ apks [ $apk ] } || echo " Recommended version ( $version_present ) of " $apk " is already present"
2022-10-09 17:37:34 -05:00
done