| 
									
										
										
										
											2020-10-17 00:37:45 +02:00
										 |  |  | import {LicenseInfo} from "./Wikimedia"; | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  | import ImageAttributionSource from "./ImageAttributionSource"; | 
					
						
							|  |  |  | import BaseUIElement from "../../UI/BaseUIElement"; | 
					
						
							|  |  |  | import {UIEventSource} from "../UIEventSource"; | 
					
						
							|  |  |  | import Svg from "../../Svg"; | 
					
						
							| 
									
										
										
										
											2021-07-03 22:24:12 +02:00
										 |  |  | import {Utils} from "../../Utils"; | 
					
						
							| 
									
										
										
										
											2020-10-17 00:37:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  | export class Mapillary extends ImageAttributionSource { | 
					
						
							| 
									
										
										
										
											2020-10-17 00:37:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  |     public static readonly singleton = new Mapillary(); | 
					
						
							| 
									
										
										
										
											2021-09-15 01:33:52 +02:00
										 |  |  |      | 
					
						
							|  |  |  |     private static readonly v4_cached_urls = new Map<string, UIEventSource<string>>(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private static readonly client_token_v3 = 'TXhLaWthQ1d4RUg0czVxaTVoRjFJZzowNDczNjUzNmIyNTQyYzI2' | 
					
						
							|  |  |  |     private static readonly client_token_v4 = "MLY|4441509239301885|b40ad2d3ea105435bd40c7e76993ae85" | 
					
						
							| 
									
										
										
										
											2020-10-17 00:37:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  |     private constructor() { | 
					
						
							|  |  |  |         super(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-15 01:33:52 +02:00
										 |  |  |     private static ExtractKeyFromURL(value: string): { | 
					
						
							|  |  |  |         key: string, | 
					
						
							|  |  |  |         isApiv4?: boolean | 
					
						
							|  |  |  |     } { | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  |         if (value.startsWith("https://a.mapillary.com")) { | 
					
						
							| 
									
										
										
										
											2021-09-15 01:33:52 +02:00
										 |  |  |             const key =  value.substring(0, value.lastIndexOf("?")).substring(value.lastIndexOf("/") + 1) | 
					
						
							|  |  |  |             return {key:key, isApiv4: !isNaN(Number(key))}; | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-15 01:33:52 +02:00
										 |  |  |         const newApiFormat = value.match(/https?:\/\/www.mapillary.com\/app\/\?pKey=([0-9]*)/) | 
					
						
							|  |  |  |         if (newApiFormat !== null) { | 
					
						
							|  |  |  |             return {key: newApiFormat[1], isApiv4: true} | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const mapview = value.match(/https?:\/\/www.mapillary.com\/map\/im\/(.*)/) | 
					
						
							|  |  |  |         console.log("Mapview matched ", value, mapview) | 
					
						
							|  |  |  |         if(mapview !== null){ | 
					
						
							|  |  |  |             const key = mapview[1] | 
					
						
							|  |  |  |             return {key:key, isApiv4: !isNaN(Number(key))}; | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-15 01:33:52 +02:00
										 |  |  |          | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (value.toLowerCase().startsWith("https://www.mapillary.com/map/im/")) { | 
					
						
							|  |  |  |             // Extract the key of the image
 | 
					
						
							|  |  |  |             value = value.substring("https://www.mapillary.com/map/im/".length); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-15 01:33:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const matchApi = value.match(/https?:\/\/images.mapillary.com\/([^/]*)(&.*)?/) | 
					
						
							|  |  |  |         if (matchApi !== null) { | 
					
						
							|  |  |  |             return {key: matchApi[1]}; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return {key: value, isApiv4: !isNaN(Number(value))}; | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     SourceIcon(backlinkSource?: string): BaseUIElement { | 
					
						
							|  |  |  |         return Svg.mapillary_svg(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-15 01:33:52 +02:00
										 |  |  |     PrepareUrl(value: string): string | UIEventSource<string> { | 
					
						
							|  |  |  |         const keyV = Mapillary.ExtractKeyFromURL(value) | 
					
						
							|  |  |  |         if (!keyV.isApiv4) { | 
					
						
							|  |  |  |             return `https://images.mapillary.com/${keyV.key}/thumb-640.jpg?client_id=${Mapillary.client_token_v3}` | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             const key = keyV.key; | 
					
						
							|  |  |  |             if(Mapillary.v4_cached_urls.has(key)){ | 
					
						
							|  |  |  |                 return Mapillary.v4_cached_urls.get(key) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const metadataUrl ='https://graph.mapillary.com/' + key + '?fields=thumb_1024_url&&access_token=' + Mapillary.client_token_v4; | 
					
						
							|  |  |  |             const source = new UIEventSource<string>(undefined) | 
					
						
							|  |  |  |             Mapillary.v4_cached_urls.set(key, source) | 
					
						
							|  |  |  |             Utils.downloadJson(metadataUrl).then( | 
					
						
							|  |  |  |                json => { | 
					
						
							|  |  |  |                    console.warn("Got response on mapillary image", json, json["thumb_1024_url"]) | 
					
						
							|  |  |  |                    return source.setData(json["thumb_1024_url"]); | 
					
						
							|  |  |  |                } | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             return source | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected DownloadAttribution(url: string): UIEventSource<LicenseInfo> { | 
					
						
							| 
									
										
										
										
											2020-10-17 00:37:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-15 01:33:52 +02:00
										 |  |  |         const keyV = Mapillary.ExtractKeyFromURL(url) | 
					
						
							|  |  |  |         if(keyV.isApiv4){ | 
					
						
							|  |  |  |             const license = new LicenseInfo() | 
					
						
							|  |  |  |             license.artist = "Contributor name unavailable"; | 
					
						
							|  |  |  |             license.license = "CC BY-SA 4.0"; | 
					
						
							|  |  |  |             // license.license = "Creative Commons Attribution-ShareAlike 4.0 International License";
 | 
					
						
							|  |  |  |             license.attributionRequired = true; | 
					
						
							|  |  |  |             return new UIEventSource<LicenseInfo>(license) | 
					
						
							|  |  |  |              | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         const key = keyV.key | 
					
						
							|  |  |  |          | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  |         const metadataURL = `https://a.mapillary.com/v3/images/${key}?client_id=TXhLaWthQ1d4RUg0czVxaTVoRjFJZzowNDczNjUzNmIyNTQyYzI2` | 
					
						
							|  |  |  |         const source = new UIEventSource<LicenseInfo>(undefined) | 
					
						
							| 
									
										
										
										
											2021-07-03 22:24:12 +02:00
										 |  |  |         Utils.downloadJson(metadataURL).then(data => { | 
					
						
							| 
									
										
										
										
											2020-10-17 00:37:45 +02:00
										 |  |  |             const license = new LicenseInfo(); | 
					
						
							|  |  |  |             license.artist = data.properties?.username; | 
					
						
							|  |  |  |             license.licenseShortName = "CC BY-SA 4.0"; | 
					
						
							|  |  |  |             license.license = "Creative Commons Attribution-ShareAlike 4.0 International License"; | 
					
						
							|  |  |  |             license.attributionRequired = true; | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  |             source.setData(license); | 
					
						
							| 
									
										
										
										
											2020-10-17 00:37:45 +02:00
										 |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  |         return source | 
					
						
							| 
									
										
										
										
											2020-10-17 00:37:45 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } |