Log Foursquare Locations in Markdown

Posted on 2014-07-06

I've always used Foursquare as a way to remember the places I had visited while traveling. Foursquare isn't really meant to be used in this way, and as a result, they don't make it easy to answer the question, "what was that restaurant I went to last time I was here?" I'm now using IFTTT to log all my checkins to a text file in my Dropbox account.

I like MultiMarkdown tables. So that my Foursquare checkins looked nice, I first created a file in my Dropbox account with a heading

| Date |  VenueName  | VenueUrl | Shout | MapURL |  City | State | Country |
| :---: | :---: | :---: | :---: | :---: | :---: |

In IFTTT, I then created a recipe which matches my table headers

IFTTT Recipe: Share Foursquare checkins in mamarkdown table connects foursquare to dropbox

IFTTT Content

You may have noticed that I added an additional "Address" column that isn't getting filled out. IFTTT doesn't explicitly give the address of the venue you visited. However, the link to the Google Maps image contains GPS coordinates that I can use. Dr. Drang's post gave me the idea to parse out the coordinates and then use them how I'd like. This script, which I'm using with Hazel each time the file is updated, reverse geolocates the coordinates and returns the full address using OpenStreetMap. After that, it appends that address to each line in the markdown file.

#!/bin/bash

FILE="$HOME/Dropbox/IFTTT/foursquare/foursquare.txt"

START=1
index=$START
IFS=$'\n'     # new field separator, the end of line
for line in $(cat $FILE)
do
    mapsurl=$(echo $line | sed -n 's/.*(\(http.*\)).*/\1/p');

    existingaddress=$(echo $line | grep -E '^.*\(http.*\)(.*\|){2,}$');

    if [[ ! $mapsurl || $existingaddress ]]; then
        (( index = index + 1 ))
        continue
    fi

    coords=$(echo $mapsurl | sed -E 's/^.+\?center=([0-9.,-]+).+/\1/');
    lat=$(echo $coords | cut -f1 -d,)
    long=$(echo $coords | cut -f2 -d,)

    address=$(curl -s "http://nominatim.openstreetmap.org/reverse?format=json&lat=${lat}&lon=${long}&zoom=18&addressdetails=1")

    country=$(echo $address | sed -e 's/.*\("country":".*"\),.*/\1/' | awk -F'"' '/country/ {print $4}')
    city=$(echo $address | sed -e 's/.*\("city":".*"\),.*/\1/' | awk -F'"' '/city/ {print $4}')
    state=$(echo $address | grep "\bstate\b" | sed -e 's/.*\("state":".*"\),.*/\1/' | awk -F'"' '/state/ {print $4}')

    # update the line of text
    sed -i '' -e "${index}s/\(.*\)/\1 $city | $state | $country |/" "$FILE";
    (( index = index + 1 ))
done

In the end, the table then ends up looking something like this:

Date VenueName VenueUrl Shout MapURL City State Country
July 06, 2014 at 07:07PM Third Floor Espresso (3FE) http://4sq.com/rtEJWP Map Link Dublin Republic of Ireland
July 06, 2013 at 10:00AM Wooly Pig Cafe http://4sq.com/1n5Scct Map Link San Francisco California United States of America

Tags: markdown automation bash ifttt

log-foursquare-locations-in-markdown

© Ryan M 2023. Built using Pelican.