One of my gripes with Slogger is that when Instagram photos are added, they have no text captions. This makes sense though — the built-in system for adding photos is really as simple as passing the image as an argument.
Still, there had to be a way to make this work! I decided that with a little tweaking of my current IFTTT script, I could get captions included in my Day One entries.
The best way to do this is to setup your IFTTT script to download photos to Dropbox with good file-names. Mine's really simple; it's just CAPTION.jpg. Yep. With the text parsing I'm doing, this works just fine. If I want to sort by date, I can simply sort in Finder by "Date Added". No complaints there. Now for the code.
Let's start with slogger_image.rb
, the file used for importing Instagram photos.
After line 12, we see where the file is added as a variable:
file = ARGV[0]
I decided that after that, taking the file and parsing the string into the caption (remember, the filename is just caption + extension) should be pretty simple.
fileName = File.basename(file)
named = fileName[0..-5]
First, we take the file variable and make a fileName variable of the File
class. Isn't Ruby great? Using File.basename
, everything except the file itself is removed; all directories are removed. After that, we take the fileName and simply cut the last four characters from it — in all of the Instagram photos, this will be ".jpg". Having some solidarity in the way the photos is exported is very nice right about now.
Let's see how this works, step-by-step.
The script is run.
slogger_image.rb ~/Dropbox/instagram/Awesome.jpg
file = ARGV[0]
ARGV[0] = "~/Dropbox/instagram/Awesome.jpg"
We do our parsing.
fileName = "Awesome.jpg"
named = "Awesome"
Now we pass it onto Slogger. Luckily, Brett's left some nice placeholder code in to make this easy. The DayOne
class has a function for passing in "options".
options = { 'content' => named }
The content
variable is, as I learned from my experiments, the caption section of Day One. By passing in the string we've taken from the file name, we can get some sort of caption.
Boom. Instagram captions. And I'm a happy man.
Bonus round: so you're crazy.
And you want Instagram URLs embedded like I do:
We change our IFTTT script to add the URL into the filename, and then divide it with something like "---", then we parse it by hand when it comes onto the computer. Probably the worst code ever, but it'll do.
If our file name is something like "I'll be honest, I'm just testing some code here... I don't care much about this picture---http/instagr.ampOsr4jnmywX", we can try something like this (replace the earlier code with something like this):
caption = minusExtension.slice(0..(minusExtension.index('---')))[0..-2]
url = minusExtension.slice((minusExtension.index('---')..-1))[3..-1]
url.sub!('http:', 'http://')
url.sub!('gr.amp', 'gr.am/p/')
fullCap = caption + " [from [Instagram](#{url})]"
Through that ungodly portion of duct-taped code, we might have something nice looking at the end. In fact, after trying a couple of photos, it seems to work pretty well.
In case you're at all interested, I just added an Instagram plugin to Slogger that has comments and likes. Here's a link to my post