sportstracker.js (2444B)
1 import BaseService from './baseService'; 2 import urlViaCorsProxy from '~/lib/CORSProxy'; 3 import utf8 from 'utf8'; 4 5 class SportsTrackerBase extends BaseService { 6 isOurUrl() { 7 return this.urlRe.test(this.origUrl); 8 } 9 } 10 11 class SportsTrackerActivity extends SportsTrackerBase { 12 urlRe = /^https?:\/\/(www\.)?sports-tracker\.com\/workout\/([^/]+)\/([a-z0-9]+)/u; 13 14 requestOptions() { 15 const m = this.urlRe.exec(this.origUrl); 16 const activityId = m[3]; 17 return [ 18 { 19 url: urlViaCorsProxy( 20 `https://api.sports-tracker.com/apiserver/v1/workouts/${activityId}/data?samples=100000` 21 ), 22 options: { 23 responseType: 'binarystring', 24 isResponseSuccess: (xhr) => xhr.status === 200 || xhr.status === 403 25 } 26 }, 27 { 28 url: urlViaCorsProxy(`https://api.sports-tracker.com/apiserver/v1/workouts/${activityId}/combined`), 29 options: { 30 responseType: 'binarystring', 31 isResponseSuccess: (xhr) => [200, 403, 404].includes(xhr.status) 32 } 33 } 34 ]; 35 } 36 37 parseResponse(responses) { 38 const [dataResponse, metadataResponse] = responses; 39 if (metadataResponse.status === 404) { 40 return [{error: 'Sports Tracker activity not found'}]; 41 } 42 if (dataResponse.status === 403) { 43 return [{error: 'Sports Tracker user disabled viewing this activity'}]; 44 } 45 let data, metadata; 46 let name = `Sports Tracker activity`; 47 try { 48 data = JSON.parse(utf8.decode(dataResponse.responseBinaryText)); 49 metadata = JSON.parse(utf8.decode(metadataResponse.responseBinaryText)); 50 } catch (e) { 51 return [{name, error: 'UNSUPPORTED'}]; 52 } 53 const track = data.payload.locations.map((location) => ({ 54 lat: location.la, 55 lng: location.ln 56 }) 57 ); 58 if (data.payload.description) { 59 name = data.payload.description; 60 } else { 61 const startTime = new Date(metadata.payload.startTime).toDateString(); 62 name = `${metadata.payload.fullname} on ${startTime}`; 63 } 64 65 return [{ 66 name, 67 tracks: [track] 68 }]; 69 } 70 } 71 72 export {SportsTrackerActivity};