nakarte

Source code of https://map.sikmir.ru (fork)
git clone git://git.sikmir.ru/nakarte
Log | Files | Refs | LICENSE

garmin.js (3353B)


      1 import BaseService from './baseService';
      2 import urlViaCorsProxy from '~/lib/CORSProxy';
      3 
      4 class GarminBase extends BaseService {
      5     isOurUrl() {
      6         return this.urlRe.test(this.origUrl);
      7     }
      8 }
      9 
     10 class GarminRoute extends GarminBase {
     11     urlRe = /^https?:\/\/connect\.garmin\.com\/modern\/course\/(\d+)/u;
     12 
     13     requestOptions() {
     14         const m = this.urlRe.exec(this.origUrl);
     15         const trackId = this.trackId = m[1];
     16         return [{
     17             url: urlViaCorsProxy(`https://connect.garmin.com/modern/proxy/course-service/course/${trackId}`),
     18             options: {
     19                 responseType: 'json',
     20                 isResponseSuccess: (xhr) => xhr.status === 200 || xhr.status === 403 || xhr.status === 404
     21             },
     22         }];
     23     }
     24 
     25     parseResponse(responses) {
     26         const response = responses[0];
     27         if (response.status === 403) {
     28             return [{error: 'Garmin Connect user disabled viewing this route'}];
     29         }
     30         if (response.status === 404) {
     31             return [{error: 'Garmin Connect route does not exist'}];
     32         }
     33         let name = `Garmin Connect route ${this.trackId}`;
     34         const data = response.responseJSON;
     35         if (!data) {
     36             return [{name, error: 'UNSUPPORTED'}];
     37         }
     38         let points = null;
     39         let tracks = [];
     40         try {
     41             if (data.coursePoints) {
     42                 points = data.coursePoints.map((pt) => ({name: pt.name, lat: pt.lat, lng: pt.lon}));
     43             }
     44             if (data.geoPoints) {
     45                 tracks = [data.geoPoints.map((obj) => ({lat: obj.latitude, lng: obj.longitude}))];
     46             }
     47         } catch {
     48             return [{name, error: 'UNSUPPORTED'}];
     49         }
     50         name = data.courseName ? data.courseName : name;
     51         return [{
     52             name,
     53             points,
     54             tracks: tracks,
     55         }];
     56     }
     57 }
     58 
     59 class GarminActivity extends GarminBase {
     60     urlRe = /^https?:\/\/connect\.garmin\.com\/modern\/activity\/(\d+)/u;
     61 
     62     requestOptions() {
     63         const m = this.urlRe.exec(this.origUrl);
     64         const trackId = this.trackId = m[1];
     65         return [
     66             {
     67                 url: urlViaCorsProxy(
     68                     `https://connect.garmin.com/modern/proxy/activity-service/activity/${trackId}/details`),
     69                 options: {
     70                     responseType: 'json',
     71                     isResponseSuccess: (xhr) => xhr.status === 200 || xhr.status === 403 || xhr.status === 404
     72                 }
     73             }
     74         ];
     75     }
     76 
     77     parseResponse(responses) {
     78         const response = responses[0];
     79         if (response.status === 403) {
     80             return [{error: 'Garmin Connect user disabled viewing this activity'}];
     81         }
     82         if (response.status === 404) {
     83             return [{error: 'Garmin Connect activity does not exist'}];
     84         }
     85         let name = `Garmin Connect activity ${this.trackId}`;
     86         const data = response.responseJSON;
     87         if (!data) {
     88             return [{name, error: 'UNSUPPORTED'}];
     89         }
     90         let track;
     91         try {
     92             track = data.geoPolylineDTO.polyline.map((obj) => ({lat: obj.lat, lng: obj.lon}));
     93         } catch {
     94             return [{name, error: 'UNSUPPORTED'}];
     95         }
     96 
     97         return [{
     98             name,
     99             tracks: [track]
    100         }];
    101     }
    102 }
    103 
    104 export {GarminRoute, GarminActivity};