Created
June 29, 2012 04:25
-
-
Save mathieucarbou/3015746 to your computer and use it in GitHub Desktop.
GPX batch upload to sportstracklive.com
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 groovy | |
import groovy.io.FileType | |
import groovyx.net.http.ContentType | |
import groovyx.net.http.HTTPBuilder | |
import groovyx.net.http.Method | |
import org.apache.http.HttpResponse | |
import org.apache.http.client.methods.HttpPost | |
import org.apache.http.client.params.ClientPNames | |
import org.apache.http.client.params.CookiePolicy | |
import org.apache.http.entity.mime.MultipartEntity | |
import org.apache.http.entity.mime.content.FileBody | |
import org.apache.http.entity.mime.content.StringBody | |
import org.apache.http.impl.cookie.BasicClientCookie | |
import org.xml.sax.ErrorHandler | |
import org.xml.sax.SAXParseException | |
import org.apache.http.conn.HttpHostConnectException | |
@Grab(value = 'org.codehaus.groovy.modules.http-builder:http-builder:0.5.2') | |
@Grab(value = 'org.apache.httpcomponents:httpmime:4.1.2') | |
def login = {String email, String password -> | |
def http = new HTTPBuilder("https://www.sportstracklive.com/signin") | |
def success = http.post([ | |
requestContentType: ContentType.URLENC, | |
body: [ | |
'userCredentialsForm.userCredentials.email': email, | |
'userCredentialsForm.userCredentials.password': password | |
] | |
]) {HttpResponse res -> | |
def redirect = res.getFirstHeader('Location')?.value | |
return redirect && redirect.startsWith('https://www.sportstracklive.com/user/') | |
} | |
return success ? http.client.cookieStore.cookies.find {it.name == 'JSESSIONID'}.value : null | |
} | |
def upload = {File file, String category, String session -> | |
def http = new HTTPBuilder("http://www.sportstracklive.com/live/sports/trackupload") | |
http.client.getParams().setParameter ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY | |
http.client.cookieStore.addCookie new BasicClientCookie('JSESSIONID', session).with { | |
it.path = '/' | |
it.domain = 'www.sportstracklive.com' | |
it | |
} | |
return http.request(Method.POST, {HttpPost req -> | |
req.entity = new MultipartEntity().with { | |
it.addPart 'trackProfileForm.trackProfile.units', new StringBody('MET') | |
it.addPart 'trackProfileForm.trackProfile.category', new StringBody(category) | |
it.addPart 'timeZone', new StringBody('-05:00') | |
it.addPart '_target1', new StringBody('Upload') | |
it.addPart 'file1', new FileBody(file) | |
it | |
} | |
response.success = {HttpResponse res -> | |
def redirect = res.getFirstHeader('Location')?.value | |
return redirect && redirect.startsWith('http://www.sportstracklive.com/user/') | |
} | |
}) | |
} | |
if (args.length != 3 || !new File(args[2]).isDirectory()) { | |
println "Usage: ${new File(getClass().protectionDomain.codeSource.location.path).name} <username> <password> <gpx folder>" | |
System.exit(1) | |
} | |
def src = args[2] as File | |
print "Login to 'sportstracklive.com' as ${args[0]}... " | |
def session = login(args[0], args[1]) | |
if (session) { | |
println "Success." | |
src.eachFile FileType.FILES, {File file -> | |
if (file.name.endsWith('.gpx')) { | |
println "Processing ${file.name}..." | |
print " - category: " | |
def category = null | |
try { | |
def slurper = new XmlSlurper(false, false) | |
slurper.errorHandler = [error: {}, warning: {}, fatalError: {}] as ErrorHandler | |
category = slurper.parse(file).trk.name as String | |
category = category.substring 0, category.indexOf(' ') | |
println category | |
} catch (SAXParseException e) { | |
println "ERROR ! ${e.message}" | |
} | |
if (category) { | |
print " - uploading... " | |
try { | |
if(upload(file, category, session)) { | |
file.renameTo(new File(file.parentFile, file.name + '.done')) | |
println 'Done.' | |
} else { | |
println 'Failed.' | |
} | |
} catch (HttpHostConnectException e) { | |
println "ERROR ! ${e.message}" | |
} | |
} | |
} | |
} | |
} else { | |
println 'Failed.' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This groovy script enables you to upload all GPX files contained in a folder to sportstracklive.com. It can be used in example to migrate all your data exported from another website like runkeeper.com to sportstracklive.com.