Upload Image / Make Post Request via curl + scrot + xclip

342 Views Asked by At

I am attempting to write a simple bash script that works similar to jomo/imgur-screenshot, but uses the ziggi/zimg-host source.

Essentially what imgur-screenshot does is when the script is initialized, it opens up scrot -select (the -select option interactively choose a window or rectangle with the mouse). Then once you take the screenshot of the defined area, it uploads the saved screenshot to imgur (or in my case, my self-hosted site using ziggi/zimg-host) and then with xclip it copies the needed result data (ultimately the URL link) to your clipboard.

This is my (very base) bash script that I have now to perform this wanted purpose:

#!/bin/bash

function uploadImage {
        curl -s -F files[]="@$1" http://ziggisite.com/upload.php
}

if [ "$#" -ne 1 ]; then
        notify-send "Upload error"
else
        uploadImage $1 | xclip -selection c
        notify-send "Upload complete"
fi

When I initiate this script using the following command: scrot -e './img.sh $f', it uploads a screenshot successfully but xclip does not copy the correct data to my clipboard - instead it copies everything that gets outputted.

And for reference, ziggi/zimg-host response looks like this:

[
    {
        "name":"image1.jpg",
        "type":2,
        "size":
        {
            "width":420,
            "height":336,
            "filesize":26834
        },
        "error":
        {
            "upload":0,
            "type":0,
            "size":0
        },
        "url":"ccc762c11f336cfa9fdbcc1b7ea4c1a3.jpg"
    }
]

Basically once the script is ran, I need it to copy the "url" portion of the response, and throwhttp://ziggisite.com at the beginning of it, so the final data copied to my clipboard would look something likehttp://ziggisite.com/ccc762c11f336cfa9fdbcc1b7ea4c1a3.jpg.

If anyone could throw out some resources or assistance, that would be great!

1

There are 1 best solutions below

0
On

From the OP's comment I assume he/she is okay with using a JSON parser like jq. Then here's a solution in jq. Say we put the example response in response.json, then

<response.json jq -r '.[0].url'

yields

ccc762c11f336cfa9fdbcc1b7ea4c1a3.jpg

while

<response.json jq -r '"http://ziggisite.com/" + .[0].url'

yields

http://ziggisite.com/ccc762c11f336cfa9fdbcc1b7ea4c1a3.jpg

Here the -r option is for raw output, i.e., without JSON's quotes.