Created
April 3, 2015 00:24
-
-
Save n8henrie/408b673f9dd4c5c5994f to your computer and use it in GitHub Desktop.
Uses the Everytrail API to back up trip info as a .csv file and all the .gpx files. Files will populate into whatever directory the script is in, so I recommend you first make an "everytrail_backup" folder, move the script there, then run.
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
#! /usr/bin/env python3 | |
"""everytrail_backup.py | |
Uses the Everytrail API to back up trip info as a .csv file and all the | |
.gpx files. Files will populate into whatever directory the script is in, | |
so I recommend you first make an "everytrail_backup" folder, move the script | |
there, then run.""" | |
import csv | |
import requests | |
import urllib.parse | |
import xml.etree.ElementTree | |
# Your Everytrail login info -- required to work with private trips. | |
USERNAME = '' | |
PASSWORD = '' | |
# http://www.everytrail.com/developer/requestkey | |
key = '' | |
secret = '' | |
# Get this by viewing your profile and looking at the URL. | |
USER_ID = '' | |
api_base_url = 'http://www.everytrail.com/api/' | |
my_trips_url = urllib.parse.urljoin(api_base_url, 'user/trips') | |
payload = { | |
'user_id': USER_ID, | |
'username': USERNAME, | |
'password': PASSWORD, | |
'version': 3, | |
'limit': 1000 | |
} | |
response = requests.get(my_trips_url, auth=(key, secret), params=payload) | |
root = xml.etree.ElementTree.fromstring(response.content) | |
with open('everytrail.csv', 'w') as csv_file: | |
writer = csv.writer(csv_file) | |
for trip in root[0].findall('trip'): | |
name = trip.find('name').text | |
date = trip.find('date').text | |
activity = trip.find('activity').text | |
length = trip.find('length').text | |
gpx_url = trip.find('gpx').text | |
writer.writerow([name, date, activity, length, gpx_url]) | |
trip_id = trip.get('id') | |
gpx_data = requests.get(gpx_url).content | |
with open(trip_id + '.gpx', 'wb') as gpxfile: | |
gpxfile.write(gpx_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment