Skip to content

Instantly share code, notes, and snippets.

@yusefnapora
Last active January 25, 2016 18:49
Show Gist options
  • Save yusefnapora/d989cf9f360ae82cee12 to your computer and use it in GitHub Desktop.
Save yusefnapora/d989cf9f360ae82cee12 to your computer and use it in GitHub Desktop.
#!/bin/bash
type jq >/dev/null 2>&1 || { echo >&2 "Please install jq: https://stedolan.github.io/jq/"; exit 1; }
type curl >/dev/null 2>&1 || { echo >&2 "Please install curl"; exit 1; }
if [ "$2" = "--local-ipfs" ] && (type ipfs >/dev/null 2>&1); then
USE_LOCAL_IPFS=1
else
USE_LOCAL_IPFS=0
fi
#SEARCH_URL="https://mediachain-resolver.herokuapp.com/search"
SEARCH_URL="http://localhost:5050/search"
IPFS_GATEWAY_URL="http://gateway.ipfs.io/ipfs"
function ipfs_lookup {
local ipfs_hash="$1"
local result
type ipfs >/dev/null 2>&1
if [ "$USE_LOCAL_IPFS" = "1" ]; then
echo "Resolving metadata using local ipfs installation..."
ipfs cat $ipfs_hash | jq -C .
else
echo "resolving metadata using IPFS http gateway..."
local url="$IPFS_GATEWAY_URL/$ipfs_hash"
echo "$url"
curl "$url" 2>/dev/null | jq -C .
fi
}
function mchain_resolve {
local img_url="$1"
local header='Content-Type: application/json'
local data="{\"image_url\": \"$img_url\"}"
local search_result=$(curl -X POST -H "$header" -d "$data" "$SEARCH_URL" 2>/dev/null)
local search_err=$(echo $search_result | jq -r .error.message)
if [ "$search_err" != "null" ]; then
echo "Error searching for similar images: "
echo $search_err
exit 1
fi
local metadata_hash=$(echo $search_result | jq -r '.matches | map(.metadata_id?) | map(select(. != null)) | .[0]')
if [ "$metadata_hash" == "null" ]; then
echo "No metadata available for $img_url"
exit
fi
echo "Metadata IPFS locator: $metadata_hash"
ipfs_lookup $metadata_hash
}
mchain_resolve $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment