Last active
August 29, 2015 14:05
-
-
Save mariuswatz/e9d7b64eae293c09301a to your computer and use it in GitHub Desktop.
Tool to download all your Moves data in JSON format, from a provided starting date until the current day.
This file contains 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
/* | |
UMovesDownloader.pde | |
Marius Watz, August 2014 | |
http://workshop.evolutionzone.com | |
Retrieves JSON data from Moves (http://www.moves-app.com/), | |
from a provided start date until the current date. Days for | |
which a data file already exists are skipped. | |
Requires a Moves access token, which can be obtained using the | |
web interface set up by Nicholas Felton for his MovesMapper | |
project. See instructions below. | |
NOTE: Try to get the starting date as close as possible to a | |
valid date for which you have Moves data. The code will give | |
nasty-looking errors for any date that has no data. | |
API code based on MovesMapper by Nicholas Felton: | |
https://github.com/feltron/MMapper | |
*/ | |
// Instructions from original code by Nicholas Felton. We'll | |
// get a valid Moves access token by using his web service interface. | |
// ------------------------------------------------------------------ | |
// SETUP | |
// 1. Open Moves app and make sure that your activity is up to date. | |
// 2. In the settings, go to the Apps section and click the add a PIN button. | |
// 3. Visit http://shielded-sierra-8807.herokuapp.com to get the PIN and enter this in the app. | |
// 4. Once you've given the app permission to access your data, the web page will refresh and dislay your access token. | |
// 5. Set the accessToken string | |
// 6. Run the sketch. | |
import java.util.*; | |
import java.text.SimpleDateFormat; | |
String accessToken = "YOUR_TOKEN"; | |
int startYear=2014; | |
int startMonth=1; // January | |
int startDay=20; // First day of month == 1 | |
void setup() { | |
if(accessToken.compareTo("YOUR_TOKEN")==0) { | |
println("Access token must be provided."); | |
println("Read the comments for instructions."); | |
} | |
else { | |
movesDownload(startYear, startMonth, startDay); | |
} | |
} | |
void draw() { | |
exit(); | |
} | |
/* | |
Iterate through days from provided start date until the | |
current date, fetching Moves JSON data for each day. | |
*/ | |
void movesDownload(int startY, int startM, int startD) { | |
Calendar today=Calendar.getInstance(); | |
Calendar cal=Calendar.getInstance(); | |
cal.clear(); | |
cal.set(Calendar.YEAR, startY); | |
cal.set(Calendar.MONTH, startM-(1-Calendar.JANUARY)); | |
cal.set(Calendar.DAY_OF_MONTH, startD-(1-Calendar.MONDAY)); | |
SimpleDateFormat df=new SimpleDateFormat("yyyyMMdd"); | |
while (cal.before (today)) { | |
String s=df.format(cal.getTime()); | |
movesQuery(s); | |
cal.add(Calendar.DAY_OF_YEAR, 1); | |
} | |
} | |
/* | |
Checks if JSON file exists for provided date, executing a call to | |
the Moves API if none is found. The result is saved as a JSON file in | |
the "data" folder. | |
*/ | |
void movesQuery(String fetchDate) { | |
if (new File(dataPath(fetchDate + ".json")).exists()) { | |
println("Data exists: " + fetchDate); | |
return; | |
} | |
println("Calling Moves API for date: " + fetchDate); | |
String apiCall = "https://api.moves-app.com/api/v1/user/storyline/daily/" + | |
fetchDate + "?trackPoints=true&access_token=" + accessToken; | |
try { | |
JSONArray result = loadJSONArray( apiCall ); | |
if (result != null) { // unsure if this call works | |
saveJSONArray(result, dataPath(fetchDate + ".json")); | |
} | |
} | |
catch(Exception e) { | |
println("Fail: "+fetchDate); | |
} | |
long t=System.currentTimeMillis(); | |
while (System.currentTimeMillis ()-t<1*1000) { | |
// do nothing - just wait a moment between API calls | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment