Search code examples
bashshellunixseddropbox

Dropbox Uploader Modification


Is anyone of you familiar with Dropbox Uploader (https://github.com/andreafabrizi/Dropbox-Uploader) or UNIX sed command.

I would like to get this script to work so it would return an sorted list from dropbox based on modified date.

Command ./droptobox list Test outputs:

Listing "/Test"... DONE
[F] config.bmp
[F] igs.bin

If to do some modifications to script (echoing out $DIR_CONTENT), I can see from the output that there certainly is information I need.

{"revision": 37, "rev": "2514cf1330", "thumb_exists": true, "bytes": 824, "modified": "Thu, 07 Nov 2013 16:14:59 +0000", "client_mtime": "Thu, 07 Nov 2013 16:14:59 +0000", "path": "/Test/config.bmp", "is_dir": false, "icon": "page_white_picture", "root": "dropbox", "mime_type": "image/x-ms-bmp", "size": "824 bytes"}

{"revision": 38, "rev": "2614cf1330", "thumb_exists": false, "bytes": 86, "modified": "Thu, 07 Nov 2013 16:15:18 +0000", "client_mtime": "Thu, 07 Nov 2013 16:15:18 +0000", "path": "/Test/igs.bin", "is_dir": false, "icon": "page_white", "root": "dropbox", "mime_type": "application/octet-stream", "size": "86 bytes"}], "size": "0 bytes"}

I suppose this is the line that responsible for it:

echo "$DIR_CONTENT" | sed -n 's/.*"path": *"\([^"]*\)",.*"is_dir": *\([^"]*\),.*/\1:\2/p' > $RESPONSE_FILE

The problem is that I ain't that familiar with sed, if I try to do some modifications I won't get any output.

Modified line (have no idea if I'm moving in right direction):

echo "$DIR_CONTENT" | sed -n 's/.*"path": *"\([^"]*\)",.*"is_dir": *\([^"]*\),.*"modified": *\([^"]*\),.*/\1:\2\3/p' > $RESPONSE_FILE

But ideally would it be possible to get it to display modified information and sort the results based on it?

Also other part of the db_list function:

        #For each line...
        while read -r line; do

            local FILE=${line%:*}
            FILE=${FILE##*/}
            local TYPE=${line#*:}

            if [[ $TYPE == "false" ]]; then
                echo -ne " [F] $FILE\n"
            else
                echo -ne " [D] $FILE\n"
            fi
        done < $RESPONSE_FILE

Solution

  • Two things I see:

    1. The "is_dir" value is a boolean and does not have quotes, so change

      "is_dir": *\([^"]*\),
      

      to

      "is_dir": *\([^,]*\),
      
    2. "modified" occurs before "path", so you need to write (accounting for quotes as well):

      echo "$DIR_CONTENT" | sed -n 's/.*"modified": *"\([^"]*\)",.*"path": *"\([^"]*\)",.*"is_dir": *\([^,]*\),.*/\2:\3 \1/p' > $RESPONSE_FILE
      

    Also, bash has a handy "here-string" feature, so you don't need to "echo"

    sed -n '...' <<< "$DIR_CONTENT" > "$RESPONSE_FILE"