From f49a1ea2cafb09b90e43f1ff30cba77423ae41c6 Mon Sep 17 00:00:00 2001 From: kaotisk Date: Sun, 30 Jun 2024 05:42:28 +0300 Subject: mixtapes module: refactoring --- lib/_ak_ipfs | 31 +++++++---------- modules/mixtapes/lib.sh | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ modules/mixtapes/main.sh | 57 ++----------------------------- 3 files changed, 101 insertions(+), 74 deletions(-) create mode 100755 modules/mixtapes/lib.sh diff --git a/lib/_ak_ipfs b/lib/_ak_ipfs index c2ad1b6..f7c2dac 100755 --- a/lib/_ak_ipfs +++ b/lib/_ak_ipfs @@ -22,7 +22,7 @@ _ak_ipfs_swarm_connect(){ _ak_ipfs_get_peers(){ _ak_ipfs swarm peers 1> /dev/null 2>&1 - if [ $? == 0 ] + if [ $? -eq 0 ] then _ak_ipfs swarm peers > $AK_WORKDIR/peers.ipfs fi @@ -40,7 +40,6 @@ _ak_ipfs_scanner(){ fi fi counter=0 - printf '[' > walk.aknet cat $peersIPFSfile \ | cut -d '/' -f 2,3,7 \ @@ -73,7 +72,6 @@ _ak_ipfs_scanner(){ fi done printf ']' >> walk.aknet - mv walk.aknet $ak_peersIPFSfile } @@ -114,12 +112,11 @@ _ak_ipfs_cat(){ exit 1 fi _ak_ipfs --timeout=10s cat $1 - if [ "$?" -ne "0" ] + if [ $? -ne 0 ] then _ak_log_error "Failed to cat $1" exit 1 fi - } _ak_ipfs_files_cp(){ @@ -274,7 +271,6 @@ _ak_ipfs_name_publish(){ _ak_log_error "Failed to get $1" exit 1 fi - } _ak_ipfs_name_resolve(){ @@ -289,7 +285,6 @@ _ak_ipfs_name_resolve(){ _ak_log_error "Failed to resolve $1" exit 1 fi - } _ak_ipfs_swarm_peers(){ @@ -328,26 +323,26 @@ _ak_ipns_resolve(){ _ak_ipfs_check(){ _ak_ipfs_files_ls /zarchive > /dev/null - if [ $? != 0 ] + if [ $? -ne 0 ] then _ak_log_error "/zarchive is missing" else _ak_log_info "/zarchive OK" fi - _ak_ipfs_files_ls /zlatest > /dev/null - if [ $? != 0 ] + if [ $? -ne 0 ] then _ak_log_error "/zlatest is missing" else _ak_log_info "/zlatest is OK" fi - _ak_ipfs_key_list | grep zchain > /dev/null - if [ $? != 0 ]; then + if [ $? -ne 0 ] + then _ak_log_warning "zchain key is missing" _ak_ipfs_key_gen zchain > $ZCHAIN - if [ $? != 0 ]; then + if [ $? -ne 0 ] + then _ak_log_error "zchain fails to create" else _ak_log_info "zchain created" @@ -355,12 +350,13 @@ _ak_ipfs_check(){ else _ak_log_info "zchain is there" fi - _ak_ipfs_key_list | grep ak-config > /dev/null - if [ $? != 0 ]; then + if [ $? -ne 0 ] + then _ak_log_warning "ak-config key is missing" _ak_ipfs_key_gen ak-config - if [ $? != 0 ]; then + if [ $? -ne 0 ] + then _ak_log_error "ak-config fails to create" else _ak_log_info "ak-config created" @@ -375,7 +371,6 @@ _ak_ipfs_init(){ then mkdir $AK_IPFS_REPO _ak_ipfs init - fi } @@ -395,7 +390,6 @@ _ak_ipfs_download(){ exit 1 fi IPFS_TARGET_FILE="kubo_"$IPFS_VERSION"_linux-$ARCH.tar.gz" - _ak_log_info "Downloading ipfs $IPFS_VERSION" if [ ! -f $AK_ARCHIVESDIR/$IPFS_TARGET_FILE ] then @@ -423,7 +417,6 @@ _ak_ipfs_cid_v0_check () { _ak_ipfs_swarm_install() { SWARMSHA512SUM="7001e37412758c43d372a969e977ca11511e034c8c1e233a58dc3ce1c6f3c1aa7d2da8cba9944a5eabaa8885742bfe6cc6794224c146b7129da8f633b53b9cfc" - if [ ! -f $AK_IPFS_REPO/swarm.key ] then _ak_log_info "Downloading swarm key" diff --git a/modules/mixtapes/lib.sh b/modules/mixtapes/lib.sh new file mode 100755 index 0000000..58d7beb --- /dev/null +++ b/modules/mixtapes/lib.sh @@ -0,0 +1,87 @@ +#!/bin/bash + +source $AK_LIBDIR/_ak_log +source $AK_LIBDIR/_ak_ipfs +source $AK_LIBDIR/_ak_gpg +source $AK_LIBDIR/_ak_zblock + +_ak_modules_mixtapes_specs(){ + datetime_mask=$(printf '^[0-9]\{8\}_[0-9]\{6\}$' | xxd -p) + ipfs_mask=$(printf '^Qm[a-zA-Z0-9]\{44\}$' | xxd -p) + text_dash_underscore_space_mask=$(printf '^[a-zA-Z0-9][a-zA-Z0-9[:space:]\_]\{1,128\}$' | xxd -p -c 64) + echo ' + { + "datetime": "'$datetime_mask'", + "artist": "'$text_dash_underscore_space_mask'", + "title": "'$text_dash_underscore_space_mask'", + "ipfs": "'$ipfs_mask'", + "detach": "'$ipfs_mask'" + }' | jq +} + +_ak_modules_mixtapes_add(){ + if [ ! -z $3 ]; + then + echo $1 + PWD="$(pwd)" + MIXTAPE_ARTIST="$1" + MIXTAPE_TITLE="$2" + MIXTAPE_FILE="$PWD/$3" + _ak_modules_mixtapes_main $1 + cat $PWD/data | jq -M + _ak_zblock_pack mixtape/add $PWD/data + fi +} + +_ak_modules_mixtapes_main(){ + echo $MIXTAPE_FILE "by" $MIXTAPE_ARTIST "named as" $MIXTAPE_TITLE + + MIXTAPE_IPFS_HASH=$(_ak_ipfs_add $MIXTAPE_FILE) + + MIXTAPE_SIGN_FILE=$MIXTAPE_FILE".asc" + _ak_gpg_sign_detached $MIXTAPE_SIGN_FILE $MIXTAPE_FILE + + MIXTAPE_SIGNATURE=$(_ak_ipfs_add $MIXTAPE_SIGN_FILE) + + cat > data < aktempzblock + else + ak-enter -l 1 $1 | jq > aktempzblock + fi + curzblock="`cat aktempzblock | jq -r '.[].zblock'`" + curaction="`cat aktempzblock | jq -r '.[].action'`" + curmodule="`cat aktempzblock | jq -r '.[].module'`" + curdata="`cat aktempzblock | jq -r '.[].data'`" + curipfs="$(cat aktempzblock | jq -r ".[].$curdata" | jq -r ".ipfs")" + curprevious="`cat aktempzblock | jq -r '.[].previous'`" + + if [ "$curmodule" == "mixtape" ] && [ "$curaction" == "add" ] + then + artist="$(cat aktempzblock | jq -r ".[].$curdata" | jq -r ".artist")" + title="$(cat aktempzblock | jq -r ".[].$curdata" | jq -r ".title")" + echo "Found zblock: $curzblock" + echo "$title by $artist" + _ak_ipfs_get $curipfs + mpv $curipfs + else + seek $curprevious + fi + rm -rf $tempdir + exit 0 +} diff --git a/modules/mixtapes/main.sh b/modules/mixtapes/main.sh index 712bb54..4456009 100755 --- a/modules/mixtapes/main.sh +++ b/modules/mixtapes/main.sh @@ -12,63 +12,10 @@ ## # We can extend it by calling the _ak_zblock_pack.sh mixtape/add data ## ORIGINAL LINE fullprogrampath="$(realpath $0)" -PROGRAM="$(basename $0)" +PROGRAM="$(realpath $0 | rev | cut -d '/' -f 2 | rev)" descriptionString="AK mixtape block creator" -source $AK_LIBDIR/_ak_log source $AK_LIBDIR/_ak_script -source $AK_LIBDIR/_ak_ipfs -source $AK_LIBDIR/_ak_gpg -source $AK_LIBDIR/_ak_zblock - -_ak_modules_mixtapes_specs(){ - datetime_mask=$(printf '^[0-9]\{8\}_[0-9]\{6\}$' | xxd -p) - ipfs_mask=$(printf '^Qm[a-zA-Z0-9]\{44\}$' | xxd -p) - text_dash_underscore_space_mask=$(printf '^[a-zA-Z0-9][a-zA-Z0-9[:space:]\_]\{1,128\}$' | xxd -p -c 64) - echo ' - { - "datetime": "'$datetime_mask'", - "artist": "'$text_dash_underscore_space_mask'", - "title": "'$text_dash_underscore_space_mask'", - "ipfs": "'$ipfs_mask'", - "detach": "'$ipfs_mask'" - }' | jq -} - -_ak_modules_mixtapes_add(){ - if [ ! -z $3 ]; - then - echo $1 - PWD="$(pwd)" - MIXTAPE_ARTIST="$1" - MIXTAPE_TITLE="$2" - MIXTAPE_FILE="$PWD/$3" - _ak_modules_mixtapes_main $1 - cat $PWD/data | jq -M - _ak_zblock_pack mixtape/add $PWD/data - fi -} - -_ak_modules_mixtapes_main(){ - echo $MIXTAPE_FILE "by" $MIXTAPE_ARTIST "named as" $MIXTAPE_TITLE - - MIXTAPE_IPFS_HASH=$(_ak_ipfs_add $MIXTAPE_FILE) - - MIXTAPE_SIGN_FILE=$MIXTAPE_FILE".asc" - _ak_gpg_sign_detached $MIXTAPE_SIGN_FILE $MIXTAPE_FILE - - MIXTAPE_SIGNATURE=$(_ak_ipfs_add $MIXTAPE_SIGN_FILE) - - cat > data <