New super-fast api script

pull/559/head
Botspot 3 years ago
parent fbe5f49ef2
commit 4f9c1f5fd0

125
api

@ -0,0 +1,125 @@
#!/bin/bash
DIRECTORY="$(readlink -f "$(dirname "$0")")"
echo "API directory is $DIRECTORY" 1>&2
repo_url="$(cat "${DIRECTORY}/etc/git_url" || echo 'https://github.com/Botspot/pi-apps')"
#determine if host system is 64 bit arm64 or 32 bit armhf
if [ ! -z "$(file "$(readlink -f "/sbin/init")" | grep 64)" ];then
arch=64
elif [ ! -z "$(file "$(readlink -f "/sbin/init")" | grep 32)" ];then
arch=32
else
error "Failed to detect OS CPU architecture! Something is very wrong."
fi
list_apps() { #installed, uninstalled, corrupted, cpu_installable, online, online_only, local, local_only
if [ -z "$1" ] || [ "$1" == local ];then
#list all apps
ls "${DIRECTORY}/apps"
elif [ "$1" == installed ];then
#list installed apps
#list apps|only show ( list of installed apps | remove match string | basename | change \n to \| | remove last "\|" )
list_apps | grep -x "$(grep -rx 'installed' "${DIRECTORY}/data/status" | awk -F: '{print $1}' | sed 's!.*/!!' | sed -z 's/\n/\\|/g' | sed -z 's/\\|$/\n/g')"
elif [ "$1" == corrupted ];then
#list corrupted apps
#list apps|only show ( list of corrupted apps | remove match string | basename | change \n to \| | remove last "\|" )
list_apps | grep -x "$(grep -rx 'corrupted' "${DIRECTORY}/data/status" | awk -F: '{print $1}' | sed 's!.*/!!' | sed -z 's/\n/\\|/g' | sed -z 's/\\|$/\n/g')"
elif [ "$1" == uninstalled ];then
#list uninstalled apps
#list apps that have a status file containing "uninstalled"
list_apps | grep -x "$(grep -rx 'uninstalled' "${DIRECTORY}/data/status" | awk -F: '{print $1}' | sed 's!.*/!!' | sed -z 's/\n/\\|/g' | sed -z 's/\\|$/\n/g')"
#also list apps that don't have a status file
list_apps | grep -vx "$(ls "${DIRECTORY}/data/status" | sed -z 's/\n/\\|/g' | sed -z 's/\\|$/\n/g')"
elif [ "$1" == cpu_installable ];then
#list apps that can be installed on the device's OS architecture (32-bit or 64-bit)
#find all apps that have install-XX script or an install script
find "${DIRECTORY}/apps" -type f \( -name "install-$arch" -o -name "install" \) | sed "s+/install-$arch++g" | sed "s+/install++g" | sed "s+${DIRECTORY}/apps/++g" | sort | uniq
elif [ "$1" == online ];then
#list apps that exist on the online git repo
if [ -d "${DIRECTORY}/update/pi-apps/apps" ];then
#if update folder exists, just use that
ls "${DIRECTORY}/update/pi-apps/apps"
else
#if update folder doesn't exist, then parse github HTML to get a lost of online apps. Horrible idea, but it works!
wget -qO- "${repo_url}/tree/master/apps" | grep 'title=".*" data-pjax=' -o | sed 's/title="//g' | sed 's/" data-pjax=//g'
fi
elif [ "$1" == online_only ];then
#list apps that exist only on the git repo, and not locally
# change \n to \| | remove last "\|"
list_apps online | grep -vx "$(list_apps | sed -z 's/\n/\\|/g' | sed -z 's/\\|$/\n/g')"
elif [ "$1" == local_only ];then
#list apps that exist only locally, and not on the git repo
# change \n to \| | remove last "\|"
list_apps | grep -vx "$(list_apps online | sed -z 's/\n/\\|/g' | sed -z 's/\\|$/\n/g')"
fi
}
app_categories() { #lists all apps in a virtual filesystem based on categories file
#cat "${DIRECTORY}/data/categories/structure" | awk -F'|' '{print $2"/"$1}'
#find apps not in categories file
{
missingapps="$(list_apps | grep -vx "$(cat "${DIRECTORY}/data/categories/structure" | awk -F'|' '{print $1}' | sed -z 's/\n/\\|/g' | sed -z 's/\\|$/\n/g')")"
if [ ! -z "$missingapps" ];then
PREIFS="$IFS"
IFS=$'\n'
for app in $missingapps ;do
echo "WARNING: $app not found in categories file." 1>&2
if list_apps online | grep -qx "$app" ;then
#if app found online, then use online category line
if [ -z "$onlinestructurefile" ];then
onlinestructurefile="$(wget -qO- 'https://raw.githubusercontent.com/Botspot/pi-apps/master/data/categories/structure')"
fi
if echo "$onlinestructurefile" | grep -q '^'"$app|" ;then
#if line found in online structure file
echo "Putting $app in the $(echo "$onlinestructurefile" | grep '^'"$app|" | awk -F'|' '{print $2}') category." 1>&2
echo "$(echo "$onlinestructurefile" | grep '^'"$app|")" >> "${DIRECTORY}/data/categories/structure"
else
#app exists online, but no structure line found
echo -e "\e[33mHUGE WARNING: the $app exists on github, but no category was found for it on github!\nPlease report this to Botspot.\e[39m" 1>&2
echo "Putting $app in the / category." 1>&2
#put the app in root directory - no category
echo "$app|" >> "${DIRECTORY}/data/categories/structure"
fi
else
#app not found online
echo "Putting $app in the / category." 1>&2
#put the app in root directory - no category
echo "$app|" >> "${DIRECTORY}/data/categories/structure"
fi
done
IFS="$PREIFS"
fi
}
#category file cleaned up past this point
#show normal categories
cat "${DIRECTORY}/data/categories/structure" | grep . | awk -F'|' '{print $2"/"$1}'
#show special Installed category
list_apps installed | sed 's+^+Installed/+g'
#show special All Apps category
list_apps cpu_installable | sed 's+^+All Apps/+g'
}
Loading…
Cancel
Save