commit f068c4f72261f279cad3a8d90f554b324c31347a
parent fdb2318067abc08d3fddb3d4db193e40c09ef4b3
Author: Sergej Orlov <wladimirych@gmail.com>
Date: Sun, 25 Nov 2018 23:48:55 +0100
[tracks] refactored movescount service
Diffstat:
3 files changed, 112 insertions(+), 147 deletions(-)
diff --git a/src/lib/leaflet.control.track-list/lib/movescount.js b/src/lib/leaflet.control.track-list/lib/movescount.js
@@ -1,146 +0,0 @@
-import urlViaCorsProxy from 'lib/CORSProxy';
-import {decode as utf8_decode} from 'utf8';
-
-const urlTypes = {
- route: {
- type: 'route',
- url: 'http://www.movescount.com/Move/Route/{id}',
- re: /^https?:\/\/www.movescount.com\/([a-z]{2}\/)?map\/?.*[?&]route=(\d+)/
- },
- move: {
- type: 'move',
- url: 'http://www.movescount.com/Move/Track2/{id}',
- re: /^https?:\/\/www.movescount.com\/([a-z]{2}\/)?moves\/move(\d+)/
- }
-};
-
-function getTrackParser(type) {
- const parsers = {
- route(data, type, id, name) {
- const track = data.points.latitudes.map((lat, i) => ({
- lat: data.points.latitudes[i],
- lng: data.points.longitudes[i]
- }));
-
- name = name || data.routeName;
-
- return [{
- name: getTrackName(type, name, id),
- tracks: [track]
- }];
- },
-
- move(data, type, id, name) {
- const track = data.TrackPoints.map(trackPoint => ({
- lat: trackPoint.Latitude,
- lng: trackPoint.Longitude
- }));
-
- return [{
- name: getTrackName(type, name, id),
- tracks: [track]
- }];
- }
- };
-
- return parsers[type];
-}
-
-function getMovescountUrlType(url, id) {
- let urlType = Object.values(urlTypes).filter(urlType => {
- if (id === undefined) {
- return urlType.re.test(url);
- } else {
- return url === urlViaCorsProxy(urlType.url.replace('{id}', id));
- }
- })[0];
-
- return urlType && urlType.type;
-}
-
-function getTrackName(type, name, id) {
- return `${name} (${type}${id})`;
-}
-
-function isMovescountUrl(url) {
- return Object.values(urlTypes).some(urlType => {
- return urlType.re.test(url);
- });
-}
-
-function movescountXhrOptions(url) {
- let type = getMovescountUrlType(url);
-
- let urlType = urlTypes[type];
-
- let match = urlType.re.exec(url);
-
- let id = match[2];
-
- const options = [{
- url: urlViaCorsProxy(urlType.url.replace('{id}', id)),
- options: {
- responseType: 'binarystring',
- isResponseSuccess: (xhr) => xhr.status === 200 || xhr.status === 403 || xhr.status === 404
- },
- }];
-
- if (type === 'move') {
- options.push({
- url: urlViaCorsProxy(url),
- options: {
- responseType: 'binarystring',
- isResponseSuccess: (xhr) => xhr.status === 200 || xhr.status === 403 || xhr.status === 404
- },
- });
- }
-
- return options;
-}
-
-function movescountParser(id, responses) {
- let data;
-
- let name;
-
- let response = responses[0];
-
- let type = getMovescountUrlType(response.responseURL, id);
-
- if (response.status === 403) {
- return [{error: `Movescount user disabled viewing this ${type}`}];
- } else {
- try {
- data = JSON.parse(response.responseBinaryText)
- } catch (e) {
- return [{name: id, error: 'UNSUPPORTED'}];
- }
- }
-
- if (responses[1]) {
- let s = responses[1].responseBinaryText;
- s = utf8_decode(s);
-
- let m = s.match(/<title>([^<]+)<\/title>/);
-
- if (m) {
- name = unescapeHtml(m[1]);
- }
- }
-
- let parseTrack = getTrackParser(type);
-
- return parseTrack(data, type, id, name);
-
- function unescapeHtml(html) {
- const element = document.createElement('div');
-
- return html.replace(/&[0-9a-z#]+;/gi, s => {
- element.innerHTML = s;
-
- return element.innerText;
- });
- }
-}
-
-export {isMovescountUrl, movescountXhrOptions, movescountParser}
-\ No newline at end of file
diff --git a/src/lib/leaflet.control.track-list/lib/services/index.js b/src/lib/leaflet.control.track-list/lib/services/index.js
@@ -5,6 +5,7 @@ import Gpslib from './gpslib';
import Strava from './strava';
import {YandexRuler} from './yandex';
import {NakarteTrack, NakarteNktk, NakarteNktl} from './nakarte';
+import {MovescountMove, MovescountRoute} from './movescount';
export default [
YandexRuler,
@@ -15,5 +16,7 @@ export default [
Gpsies,
Gpslib,
Strava,
+ MovescountMove,
+ MovescountRoute,
SimpleService
]
\ No newline at end of file
diff --git a/src/lib/leaflet.control.track-list/lib/services/movescount.js b/src/lib/leaflet.control.track-list/lib/services/movescount.js
@@ -0,0 +1,108 @@
+import BaseService from './baseService';
+import urlViaCorsProxy from 'lib/CORSProxy';
+
+class MovescountBase extends BaseService {
+ // urlRe = null;
+
+ isOurUrl() {
+ return this.urlRe.test(this.origUrl);
+ }
+}
+
+class MovescountRoute extends MovescountBase {
+ urlRe = /^https?:\/\/www.movescount.com\/([a-z]{2}\/)?map.*[?&]route=(\d+)/;
+
+ requestOptions() {
+ const m = this.urlRe.exec(this.origUrl);
+ const trackId = this.trackId = m[2];
+ return [{
+ url: urlViaCorsProxy(`http://www.movescount.com/Move/Route/${trackId}`),
+ options: {
+ responseType: 'binarystring',
+ isResponseSuccess: (xhr) => xhr.status === 200 || xhr.status === 403
+ },
+ }]
+ }
+
+ parseResponse(responses) {
+ const response = responses[0];
+ if (response.status === 403) {
+ return [{error: 'Movescount user disabled viewing this route'}];
+ }
+ let data;
+ let name = `Movescount route ${this.trackId}`;
+ try {
+ data = JSON.parse(response.responseBinaryText)
+ } catch (e) {
+ return [{name, error: 'UNSUPPORTED'}];
+ }
+ const track = data.points.latitudes.map((lat, i) => ({
+ lat,
+ lng: data.points.longitudes[i]
+ })
+ );
+
+ name = data.routeName ? data.routeName : name;
+
+ return [{
+ name,
+ tracks: [track]
+ }];
+ }
+}
+
+class MovescountMove extends MovescountBase {
+ urlRe = /^https?:\/\/www.movescount.com\/([a-z]{2}\/)?moves\/move(\d+)/;
+
+ requestOptions() {
+ const m = this.urlRe.exec(this.origUrl);
+ const trackId = m[2];
+ return [
+ {
+ url: urlViaCorsProxy(`http://www.movescount.com/Move/Track2/${trackId}`),
+ options: {
+ responseType: 'binarystring',
+ isResponseSuccess: (xhr) => xhr.status === 200 || xhr.status === 403
+ }
+ },
+ {
+ url: urlViaCorsProxy(`https://www.movescount.com/moves/move${trackId}`),
+ options: {
+ responseType: 'binarystring',
+ isResponseSuccess: (xhr) => xhr.status === 200 || xhr.status === 403 || xhr.status === 404
+ }
+ }
+ ]
+ }
+
+ parseResponse(responses) {
+ const [trackResponse, pageResponse] = responses;
+ if (trackResponse.status === 403) {
+ return [{error: 'Movescount user disabled viewing this activity'}];
+ }
+ let data;
+ let name = `Movescount move ${this.trackId}`;
+ try {
+ data = JSON.parse(trackResponse.responseBinaryText)
+ } catch (e) {
+ return [{name, error: 'UNSUPPORTED'}];
+ }
+ const track = data.TrackPoints.map(trackPoint => ({
+ lat: trackPoint.Latitude,
+ lng: trackPoint.Longitude
+ })
+ );
+
+ const dom = (new DOMParser()).parseFromString(pageResponse.responseBinaryText, "text/html");
+ const title = dom.querySelector('title').text.trim();
+ name = title ? title : name;
+
+ return [{
+ name,
+ tracks: [track]
+ }];
+
+ }
+}
+
+export {MovescountRoute, MovescountMove};
+\ No newline at end of file