diff options
Diffstat (limited to 'lib/_ak_zchain')
-rwxr-xr-x | lib/_ak_zchain | 130 |
1 files changed, 96 insertions, 34 deletions
diff --git a/lib/_ak_zchain b/lib/_ak_zchain index 73b10fb..0d1ba57 100755 --- a/lib/_ak_zchain +++ b/lib/_ak_zchain @@ -1,10 +1,30 @@ -#!/bin/bash -source $AK_LIBDIR/_ak_log -source $AK_LIBDIR/_ak_ipfs -source $AK_LIBDIR/_ak_zblock -source $AK_LIBDIR/_ak_script - -_ak_zchain_reset(){ +#!/usr/bin/env bash +### +### arching-kaos-tools +### Tools to interact and build an Arching Kaos Infochain +### Copyright (C) 2021 - 2025 kaotisk +### +### This program is free software: you can redistribute it and/or modify +### it under the terms of the GNU General Public License as published by +### the Free Software Foundation, either version 3 of the License, or +### (at your option) any later version. +### +### This program is distributed in the hope that it will be useful, +### but WITHOUT ANY WARRANTY; without even the implied warranty of +### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +### GNU General Public License for more details. +### +### You should have received a copy of the GNU General Public License +### along with this program. If not, see <http://www.gnu.org/licenses/>. +### +source $AK_LIBDIR/_ak_lib_load +_ak_lib_load _ak_script +_ak_lib_load _ak_datetime +_ak_lib_load _ak_log +_ak_lib_load _ak_ipfs +_ak_lib_load _ak_zblock + +function _ak_zchain_reset(){ _ak_log_info "Reseting AK_ZLATEST to AK_ZGENESIS" cp $AK_ZGENESIS $AK_ZLATEST if [ $? -ne 0 ] @@ -12,7 +32,6 @@ _ak_zchain_reset(){ _ak_log_error "Failed to reset AK_ZLATEST to AK_ZGENESIS" exit 1 fi - _ak_log_info "Checking if /zarchive directory exists" _ak_ipfs_files_stat /zarchive > /dev/null if [ $? -ne 0 ] @@ -26,13 +45,12 @@ _ak_zchain_reset(){ else _ak_log_info "/zarchive directory exists" fi - _ak_log_info "Checking if /zlatest file exists" _ak_ipfs_files_stat /zlatest > /dev/null 2>&1 if [ $? -eq 0 ] then _ak_log_info "Archive the previous AK_ZLATEST" - _ak_ipfs_files_cp /zlatest /zarchive/$(date -u +%s)-$(_ak_ipfs_files_stat /zlatest | head -n 1) + _ak_ipfs_files_cp /zlatest /zarchive/$(_ak_datetime_unix)-$(_ak_ipfs_files_stat /zlatest | head -n 1) if [ $? -ne 0 ] then _ak_log_error "Failed to copy /zlatest to /zarchive" @@ -48,7 +66,6 @@ _ak_zchain_reset(){ else _ak_log_info "/zlatest not found, skipping backup" fi - _ak_log_info "Copying reset AK_ZLATEST" CZLATEST="$(cat $AK_ZLATEST)" _ak_ipfs_files_cp /ipfs/$CZLATEST /zlatest @@ -57,7 +74,6 @@ _ak_zchain_reset(){ _ak_log_error "Failed to copy AK_ZLATEST to /zlatest" exit 1 fi - _ak_log_info "Publishing new (reset) AK_ZLATEST" _ak_ipfs_name_publish --key=zchain /ipfs/$(cat $AK_ZLATEST) if [ $? -ne 0 ] @@ -65,46 +81,38 @@ _ak_zchain_reset(){ _ak_log_error "Failed to publish updated zchain" exit 1 fi - _ak_config_publish if [ $? -ne 0 ] then _ak_log_error "Could not publish new configuration" exit 1 fi - _ak_log_info "Reset was successful" exit 0 } -_ak_zchain_rebase(){ +function _ak_zchain_rebase(){ if [ ! -n "$1" ]; then exit 1; fi ZTARGET="$1" echo "Reseting ZLATEST to ZTARGET" echo $ZTARGET > $AK_ZLATEST if [ $? != 0 ]; then exit 1; fi - echo "Make sure /zarchive folder exists within IPFS FS" _ak_ipfs_files_mkdir /zarchive if [ $? != 0 ]; then echo "Folder already there"; fi - echo "Archive the previous ZLATEST" - _ak_ipfs_files_cp /zlatest /zarchive/$(date -u +%s)-$(_ak_ipfs_files_stat /zlatest | head -n 1) + _ak_ipfs_files_cp /zlatest /zarchive/$(_ak_datetime_unix)-$(_ak_ipfs_files_stat /zlatest | head -n 1) if [ $? != 0 ]; then exit 1; fi - echo "Removing previous /zlatest entry" _ak_ipfs_files_rm /zlatest if [ $? != 0 ]; then exit 1; fi - echo "Copying rebased ZLATEST" CZLATEST="$(cat $AK_ZLATEST)" _ak_ipfs_files_cp /ipfs/$CZLATEST /zlatest if [ $? != 0 ]; then exit 1; fi - echo "Publishing new (rebased) ZLATEST" _ak_ipfs_name_publish --key=zchain /ipfs/$(cat $AK_ZLATEST) if [ $? != 0 ]; then exit 1; fi - _ak_config_publish if [ $? -ne 0 ] then @@ -115,7 +123,7 @@ _ak_zchain_rebase(){ exit 0 } -_ak_zchain_extract_cids(){ +function _ak_zchain_extract_cids(){ if [ ! -z $1 ] && [ -n "$1" ] then _ak_zchain_crawl $1 | jq -M | grep Qm | sed -e 's/".*"://g; s/ //g; s/[{,"]//g' | sort | uniq @@ -124,9 +132,8 @@ _ak_zchain_extract_cids(){ fi } -_ak_zchain_extract_cids_limit(){ +function _ak_zchain_extract_cids_limit(){ if [ ! -z $1 ] && [ -n "$1" ] && [ ! -z $2 ] && [ -n "$2" ] - then _ak_zchain_crawl -l $2 $1 | jq -M | grep Qm | sed -e 's/".*"://g; s/ //g; s/[{,"]//g' | sort | uniq else @@ -134,7 +141,7 @@ _ak_zchain_extract_cids_limit(){ fi } -_ak_zchain_extract_data_cids(){ +function _ak_zchain_extract_data_cids(){ if [ ! -z $1 ] then _ak_zchain_crawl $1 | jq | grep ipfs | awk '{print $2}' | sed -e 's/"//g;s/,//g' @@ -143,7 +150,7 @@ _ak_zchain_extract_data_cids(){ fi } -_ak_zchain_calculate_size(){ +function _ak_zchain_calculate_size(){ temp="$(_ak_make_temp_directory)" cd $temp if [ ! -z $1 ] && [ -n "$1" ] @@ -157,7 +164,7 @@ _ak_zchain_calculate_size(){ if [ "$p" != "" ] then _ak_ipfs_get $p - num="$(du -bs --apparent-size $p | cut -d $'\t' -f 1)" + num="$( (du -b $p || du -A $p)2>/dev/null | cut -d $'\t' -f 1)" else num=0 fi @@ -168,7 +175,7 @@ _ak_zchain_calculate_size(){ rm -rf $temp } -_ak_zchain_crawl(){ +function _ak_zchain_crawl(){ entrance="$(cat $AK_ZLATEST)" verify=1 limit=0 @@ -264,20 +271,20 @@ variable done } -_ak_zchain_crawl_self(){ +function _ak_zchain_crawl_self(){ _ak_zchain_crawl } -_ak_zchain_crawl_remote_ipfs(){ +function _ak_zchain_crawl_remote_ipfs(){ _ak_zchain_crawl $1 } -_ak_zchain_crawl_remote_ipns(){ +function _ak_zchain_crawl_remote_ipns(){ _ak_zchain_crawl -n $1 } -_ak_zchain_get_latest(){ - _ak_ipfs_files_stat /zlatest | head -n 1| tr -d '\n' +function _ak_zchain_get_latest(){ + cat $AK_ZLATEST | tr -d '\n' } # _ak_zchain_announce(){} @@ -481,3 +488,58 @@ _ak_zchain_get_latest(){ # fi # done # } + +function _ak_zchain_output_types(){ + if [ -z $1 ] || [ ! -n "$1" ] + then + zblock="$(ak zchain --get-latest)" + else + zblock="$1" + fi + seed="$(cat $AK_ZGENESIS)" + previous="$(_ak_zblock_show "$zblock" | jq -r '.previous')" + timestamp="$(_ak_zblock_show "$zblock" | jq -r '.timestamp')" + block="$(_ak_zblock_show "$zblock" | jq -r '.block')" + data="$(_ak_zblock_show "$zblock" | jq -r '.data')" + ak zblock --show $zblock | jq -r '.zblock,.module' | tr $'\n' ' ' | sed -e 's/ $/\n/' | while read zblock module + do + if [ "$module" == "mixtape" ] + then + ipfs="$(_ak_ipfs_cat $data | jq -r '.ipfs')" + file $AK_IPFS_ARTIFACTS/$ipfs | grep 'Ogg' > /dev/null + if [ $? -eq 0 ] + then + fdur="$(ogginfo $AK_IPFS_ARTIFACTS/$ipfs | grep 'Playback length' | cut -d ':' -f 2-)" + mins="$(echo -n $fdur | cut -d 'm' -f 1)" + secs="$(echo -n $fdur | cut -d ':' -f 2|cut -d '.' -f 1)" + msecs="$(echo -n $fdur | cut -d ':' -f 2|cut -d '.' -f 2 | cut -d 's' -f 1)" + smins="$(($mins * 60))" + #echo "$mins => $smins" + #echo "Duration is $mins minutes, $secs seconds and $msecs milliseconds" + if [ $msecs -gt 0 ] + then + sdur="$(( $smins + $secs + 1))" + else + sdur="$(( $smins + $secs))" + fi + #echo "Zblock: $zblock is a $module type added on $timestamp with duration $sdur seconds" + #echo "Output to file would be:" + echo "$timestamp $zblock $block $data $ipfs $sdur" >> $AK_WORKDIR/mixtapes_index + tmpfile="$(_ak_make_temp_file)" + cat $AK_WORKDIR/mixtapes_index | sort -n | uniq > $tmpfile + cat $tmpfile > $AK_WORKDIR/mixtapes_index + rm $tmpfile + fi + else + echo "Zblock: $zblock is a $module type added on $timestamp" + fi + done + if [ "$seed" == "$previous" ] + then + _ak_log_info "Reached genesis, exiting..." + exit 0 + fi + _ak_zchain_output_types $previous +} + +_ak_log_debug "_ak_zchain loaded $(caller)" |