I had three photo blogs on WordPress for photos of my daughters through the years, kind of a diary of pictures. I wanted to move them off Wordpress and on to the grave. At the time of doing the migration there was upwards of 1k posts, which included mostly photos, plus some tweets. There wasn't a simple import/export option available, so I needed to think of another way.

When I first created the blogs I used instagrams RSS feed, coupled with IFTTT. Of course, this was pre OAuth. Using this method downloaded the pictures and stored them on my server, something that I was keen to have. Once out was the only option I continued to use IFTTT, adding instagram and WordPress as authenticated services. This however only added links to the images hosted on instagram. I was lazy and left it as it was, with the view to change it. Several years later I was 1k+ posts deep.

I decided to bite the bullet and move to grab. After many failed attempts trying to work with the RSS and XML feeds provided by WordPress, I decided to download my instagram data and write a bash script to farm the data and create the graph folders, MD file and include the linked image, this is my script:

sample=$(<insta-export.json)
i=0
z=0
for row in $(echo "${sample}" | jq -r '.[] | @base64'); do
    _loop() {
      caption=$(echo ${row} | base64 --decode | jq -r '.caption')
      if echo $caption | grep -iqF \#hashtag; then

        # Remove ' from caption
        caption=${caption//\'/}

        # Get path of the image
        path=$(echo ${row} | base64 --decode | jq -r '.path')

        # Make a directory, using the image name
        mkdir ~/Documents/insta-export/jinsta-${path##*/}

        # Find the image on the local machine and store its full path in 'imagepath'
        imagepath=$(find ~/Downloads -path "*${path##*/}")

        # Copy the image to the newly created folder
        cp $imagepath ~/Documents/insta-export/jinsta-${path##*/}

        # Get the published date - in fortmat - 2014-01-18T13:54:51
        taken_at=$(echo ${row} | base64 --decode | jq -r '.taken_at')
        taken_at=$(date -j -f '%Y-%m-%d %H:%M:%S' "$taken_at" +'%s')
        taken_at=$(date -j -r "$taken_at" +"%d-%m-%Y %I:%M%p")

        # Create the markdown file
        printf -- "---\ntitle: '$caption'\ndate: $taken_at\nmedia_order: ${path##*/}\nvisible: true\n---\n\n![](${path##*/})" > ~/Documents/insta-export/jinsta-${path##*/}/item.md
        i=$[$i +1]
      fi
    }
    z=$[$z +1]
   _loop

done
echo "DOOOONE! ($z  files process / $i folders created)"

Heres an explination of each line:

Itterate over each object in the json file specified on line 1 sample=$(<jt.json):

for row in $(echo "${sample}" | jq -r '.[] | @base64'); do
  ...
done

Store the post caption in $caption:

caption=$(echo ${row} | base64 --decode | jq -r '.caption')

As my instagram isn't exclusivly peictures of my daughetrs, I give thos pictures a hastag, We now check the caption insludes a specific hastag:

if echo $caption | grep -iqF \#hashtag; then
  ...
fi

' Can cuase issues, therefore lets remove these from the caption:

caption=${caption//\'/}

Include in the json is the local path to the associate image. We'll store this a a variable:

path=$(echo ${row} | base64 --decode | jq -r '.path')

Lets make a directory using the image name. The path is expressed as photos/201812/8bb29a83fa4ad1109ad912b7f2b77281.jpg int he JSON. using ${path##*/} gets just the file name minus the extemsion type.

mkdir ~/Documents/insta-export/jinsta-${path##*/}

Next we find the full path to the image and store this in $imagepath:

imagepath=$(find ~/Downloads -path "*${path##*/}")

Copy the image into the folder we created earlier:

cp $imagepath ~/Documents/insta-export/jinsta-${path##*/}

The instagram export may not be in the same order as the pictures taken, therfore we need o set the date:. Befor that we need to make the date format correct in the JSON. This can be done with a simple regex find and replace on the JSON - Find: (\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2}) | Replace: $1 $2:

taken_at=$(echo ${row} | base64 --decode | jq -r '.taken_at')
taken_at=$(date -j -f '%Y-%m-%d %H:%M:%S' "$taken_at" +'%s')
taken_at=$(date -j -r "$taken_at" +"%d-%m-%Y %I:%M%p")

Finally create markdown file:

printf -- "---\ntitle: '$caption'\ndate: $taken_at\nmedia_order: ${path##*/}\nvisible: true\n---\n\n![](${path##*/})" > ~/Documents/insta-export/jinsta-${path##*/}/item.md

Resulting in all instagram pics being added to grav in a chronological order.

As mentioned there was tweets inclued on the blog too. This uses a modified version of the above script.

It's my intention to modify the above script and make it more generic, then uplaod it to github for public use.

Watch this space, and thanks for reading...