Created
June 13, 2017 20:08
-
-
Save huberf/e24cb02f81180a3491297bc970e04636 to your computer and use it in GitHub Desktop.
Bulk Import of Events to Nomie via API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
06-12-2017 | |
#1-10:00 | |
Coffee-10:15-1 | |
#1-10:40 | |
Soylent-15:00 | |
Eat-15:00 | |
#2-16:20 | |
Water-16:25-1 | |
#1-17:50 | |
Water-18:29-1 | |
Eat-19:00 | |
#1-20:20 | |
Water-20:50-1 | |
#1-21:50 | |
#split | |
06-13-2017 | |
#1-1:35 | |
#1-6:30 | |
Wash Underarms/Face-6:40 | |
#2-7:40 | |
Water-7:55-0.5 | |
Eat-8:10 | |
Brush Teeth-8:45 | |
Fingernails-8:50 | |
#1-9:50 | |
#1-12:50 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests as r | |
import urllib | |
from datetime import datetime, timezone, timedelta | |
from calendar import timegm | |
# Global assumptions | |
# Modify with your own coordinates | |
geo = '0,0' | |
# Replace with your personal API key | |
apiKey = 'YOUR_NOMIE_2_API_KEY' | |
def getUTC(year, month, day, hour, minutes): | |
year = int(year) | |
month = int(month) | |
day = int(day) | |
hour = int(hour) | |
minutes = int(minutes) | |
dt = datetime(year, month, day, hour, minutes) | |
tz = timezone(timedelta(hours=-6)) | |
return timegm(dt.replace(tzinfo=tz).utctimetuple()) | |
eventFile = open('events.txt') | |
eventsRaw = eventFile.read() | |
blobs = eventsRaw.split('#split') | |
splitBlobs = [] | |
for i in blobs: | |
splitBlobs += [i.split('\n')] | |
formatted = [] | |
for i in splitBlobs: | |
cleaned = [] | |
for a in i: | |
if not a == '': | |
cleaned += [a.split('-')] | |
toPut = {'day': cleaned[0], 'events': cleaned[1:]} | |
formatted += [toPut] | |
print(formatted) | |
for i in formatted: | |
day = i['day'] | |
for a in i['events']: | |
hours = a[1].split(':') | |
timestamp = getUTC(day[2], day[0], day[1], hours[0], hours[1]) * 1000 | |
print(urllib.parse.quote_plus(a[0])) | |
a[0] = urllib.parse.quote_plus(a[0]) | |
if len(a) == 2: | |
r.get('https://api.nomie.io/v2/push/' + apiKey + '/action=track/label=' + a[0] + '/time=' + str(timestamp) + '/geo=' + geo) | |
elif len(a) == 3: | |
r.get('https://api.nomie.io/v2/push/' + apiKey + '/action=track/label=' + a[0] + '/time=' + str(timestamp) + '/value=' + a[2] + '/geo=' + geo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment