Advertisement
Guest User

Garmin Connect Bulk Export JS v2

a guest
Jul 29th, 2020
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. A modified version of another pastebin snippet for bulk exporting Garmin data by an unkown author: https://pastebin.com/YN6Gex5R
  3.  
  4. Here's JavaScript that can be run in any modern browser fairly simply in JavaScript. Might be easier to set up than Python.
  5.  
  6. You'll want to pre-set a download location in your browser settings to some folder, name it TCX or something, and tell your browser to auto-download there, or else you'll get a ton of popup save dialogs.
  7.  
  8. First Navigate to the last (most recent) activity you have in Garmin Connect (as in https://connect.garmin.com/modern/activity/5555555555 ), then hit F12 (should work in Chrome/Edge/Firefox; Safari is a little more complicated) to open dev tools to get to the JavaScript Console. Then paste the below code and hit enter to run it.
  9. If you want a different format, change the variable "fileFormat" to the appropriate format acronym if garmin supports it.
  10.  
  11. It goes from most recent back downloading each one.
  12.  
  13. Added improvments:
  14. -------------------------
  15. * The script no longer stops if you hit a dead download link. This was always the case for manually added activities.
  16. * No more wait time between individual downloads. The next download starts as soon as it gets Garmin's next activityId.
  17. * Status updates are printed to the console.
  18. * You don't have to specify how many files you want to get. It just stops if it has them all.
  19. * Downloads zip archives with the source data by default. Change `fileFormat` if you want something else.
  20. * The script is no longer dependent on JQuery.
  21.  
  22. [CODE]*/
  23. const fileFormat = 'zip' // also possible: tcx, gpx - not really sure what else. Zip is the safest bet, as it just downloads the source format.
  24.  
  25. const activityUrl = activityId => `https://connect.garmin.com/modern/proxy/activity-service/activity/${activityId}`
  26. const nextUrl = activityId => `${activityUrl(activityId)}/relative`
  27. const downloadUrl = activityId => fileFormat === 'zip' ? `https://connect.garmin.com/modern/proxy/download-service/files/activity/${activityId}`
  28.                                                        : `https://connect.garmin.com/modern/proxy/download-service/export/${fileFormat}/activity/${activityId}`
  29.  
  30. const printStatusUpdate = metadataJson => {
  31.     let date = new Date(metadataJson.metadataDTO.uploadedDate);
  32.     console.log(`Downloading ${metadataJson.activityId}... (${metadataJson.activityName}, ${date.toLocaleDateString()})`)
  33. }
  34.  
  35. const downloadFile = (url, filename) => {
  36.     const a = document.createElement('a');
  37.     a.href = url;
  38.     a.download = filename;
  39.     a.click();
  40. }
  41.  
  42. const downloadActivity = activityId =>
  43.     fetch(nextUrl(activityId))
  44.         .then(data => data.json())
  45.         .then(response => {
  46.             fetch(activityUrl(activityId))
  47.                 .then(data => data.json())
  48.                 .then(printStatusUpdate);
  49.            
  50.             downloadFile(downloadUrl(activityId), `activity_${activityId}.${fileFormat}`);
  51.  
  52.             previousActivityId = response.previousActivityId;
  53.             if (previousActivityId !== null) {
  54.                 return downloadActivity(response.previousActivityId);
  55.             }
  56.             console.log("This was your last activity. All done. ")
  57.         })
  58.         .catch(console.error)
  59.  
  60. const urlPathnames = window.location.pathname.split("/")
  61. const initiallId = urlPathnames[urlPathnames.length-1];
  62. downloadActivity(initiallId);
  63. /*[/CODE]*/
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement