View Single Post
Old 01-15-2021, 11:29 AM   #1
jtgrenz
Junior Member
jtgrenz began at the beginning.
 
Posts: 2
Karma: 10
Join Date: Jan 2021
Device: Kobo LibraH2O
Question Need help cross-compiling jq lib for kobo

Oh hello MobileReaders, long time reader, first time poster

I noticed the other day that the KoboCloud plugin seems to have stopped working for me. It only syncs files up to a certain number and then just stops.

So, I started poking around the source code and thought to myself, wouldn't this be better if we use the dropbox api instead of relying on a parsing urls from a shared folder url?

Couple hours of playing around with the dropbox api and I had something that seemed pretty usable and also supported subfolders. Popped it over to my kobo and no dice. I totally forgot that the Kobo runs arm and one of the dependencies I added was jq, a unix json parser. I added the linux_x86_32 version of the binary to the kobo which wont work and there isn't an official arm binary available currently.

I'm a bit clueless when it comes to compiled languages and doubly so when dealing with a limited niche environment like the kobo. Would anyone be able to lend a hand in cross-compiling the Jq library over to work with the kobo? Also open to other ways to deal with JSON from the shell on the kobo.

If anyone wanted to see the rough hacky code I was working on, thats in the spoiler below. Basically I hooked into how kobo_cloud works but when it reaches a "url" of `dropbox_token:<some_token>` it runs the dropbox script below. The idea here being you have a dropbox app and all the contents of that app's dropbox folder being synced.

Spoiler:


$CURL is the path to the included kobo_cloud curl binary
$JQ is the path to the included jq binary (which alas doesn't yet work)
$AllowDropboxDelete is false by default.

Code:
#!/bin/sh
token=${1#"dropbox_token:"}

#load config
. $(dirname $0)/config.sh

response=$($CURL -X POST https://api.dropboxapi.com/2/files/list_folder \
    --header "Authorization: Bearer $token" \
    --header "Content-Type: application/json" \
    --silent \
    --data "{
      \"path\": \"\",
      \"recursive\": true,
      \"limit\": 2000,
      \"include_non_downloadable_files\": false,
      \"include_deleted\": $AllowDropboxDelete
    }"
)

download() {
  $CURL -X POST https://content.dropboxapi.com/2/files/download \
  --header "Authorization: Bearer $token" \
  --header "Dropbox-API-Arg: {\"path\":\"$1\"}" \
  --silent \
  --output "$Lib$1"
}

process_entry() {
  local name=$(echo $1 | $JQ -r '.name' )
  local url="$(echo $1 | $JQ -r '.path_display')"
  local full_path="$Lib$url"
  local tag=$(echo $1| $JQ -r '.[".tag"]')
  local content_hash=$(echo $1 | $JQ -r '.content_hash')

  case "$tag" in
    "file")
      if [ ! -f "$full_path" ]; then
        echo "$(date): [Downloading] $full_path"
        download $url $name
      else
        echo "$(date): [Skip::Downloaded] $full_path"
      fi
      ;;

    "folder")
      if [ -d "$full_path" ]; then
        echo "$(date): [Directory::Found] $full_path"
      else
        echo "$(date): [Directory::NotFound] Creating $full_path"
        mkdir "$full_path"
      fi
      ;;

    "deleted")
       if [ -d "$full_path" ] || [ -f "$full_path" ]; then
       if [ "$AllowDropboxDelete" = "true" ]; then
          echo "$(date): [Delete] Removing $full_path"
          rm -rf "$full_path"
        fi
      fi
      ;;

    *)
      echo "$(date): [Error] Unknown tag: $tag"
      ;;
  esac
}

echo $response | $JQ -c '.entries[]' | while read el
do
  process_entry $el
done

Last edited by jtgrenz; 01-15-2021 at 11:33 AM.
jtgrenz is offline   Reply With Quote