Extract ID in Bash and Python

Recipes to extract the collection id from the web services response received from SeedMe.

1) BASH Shell: extract collectiion_id from response

# Create a test collection and gather the response in output variable
output=$(./seedme.py  -t "Temp test collection")

# Extract collection_id from output variable
id=$(echo  $output | sed -e 's/^.*"collection_id":[ \t]*"\([^"]*\)".*$/\1/')

# Extract status from output variable
status=$(echo $output | sed -e 's/^.*"status":[ \t]*"\([^"]*\)".*$/\1/')

echo "collection_id is $id"
echo "status is $status"

# create collection url
echo "Collection url is: https://www.seedme.org/node/$id" 

# validate whether the web service response indicates success, partial or failure
if [ "$status" = 'success' ] || [ "$status" = 'partial' ] 
    then 
        echo "Successfully created collection with ID = $id"
        echo "Do additional things, like upload files, etc."
    else
        echo "Failed collection creation"
fi

 

2) BASH Shell & Python: extract collectiion_id from response

# Create a test collection and gather the response in output variable
output=$(./seedme.py  -t "Temp test collection")

# Extract collection_id from output variable
id=$(echo $output | sed  's/^.*{/{/' | python -c 'import json, sys; obj=json.load(sys.stdin); print obj["collection_id"]')

# Extract status from output variable using python
status=$(echo $output  | sed  's/^.*{/{/'| python -c 'import json, sys; obj=json.load(sys.stdin); print obj["status"]')

echo "collection_id is $id"
echo "status is $status"

# create collection url
echo "Collection url is: https://www.seedme.org/node/$id" 

# validate whether the web service response indicates success, partial or failure
if [ "$status" = 'success' ] || [ "$status" = 'partial' ] 
    then 
        echo "Successful Upload";
        echo "Do additional things, like upload floes, etc."
    else
        echo "Failed collection creation"
fi

 

3) Python only: extract collectiion_id from response

# seedme module must be installed or in same directory
import seedme

# Create an object from SeedMe class
obj = seedme.SeedMe()
result = obj.create_collection( title="Temp python collection")

# Now extract the collection_id from result which is a string in json format
my_cid = obj.get_id(result) #returns collection_id or null

#alternatively you may extract the collection_id without using the SeedMe class object
my_cid = get_id(result) #returns collection_id or null

# validate if collection_id is valid or not
if not my_cid:
    print(('Collection id was not found in: ' + result))