forked from MapComplete/MapComplete
Add support for mapillary api v4, fixes #364
This commit is contained in:
parent
5d81e7d792
commit
c8eacaa409
9 changed files with 117 additions and 63 deletions
|
@ -8,39 +8,92 @@ import {Utils} from "../../Utils";
|
|||
export class Mapillary extends ImageAttributionSource {
|
||||
|
||||
public static readonly singleton = new Mapillary();
|
||||
|
||||
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"
|
||||
|
||||
private constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
private static ExtractKeyFromURL(value: string) {
|
||||
private static ExtractKeyFromURL(value: string): {
|
||||
key: string,
|
||||
isApiv4?: boolean
|
||||
} {
|
||||
if (value.startsWith("https://a.mapillary.com")) {
|
||||
return value.substring(0, value.lastIndexOf("?")).substring(value.lastIndexOf("/") + 1);
|
||||
const key = value.substring(0, value.lastIndexOf("?")).substring(value.lastIndexOf("/") + 1)
|
||||
return {key:key, isApiv4: !isNaN(Number(key))};
|
||||
}
|
||||
const matchApi = value.match(/https?:\/\/images.mapillary.com\/([^/]*)/)
|
||||
if (matchApi !== null) {
|
||||
return matchApi[1];
|
||||
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))};
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
return value;
|
||||
|
||||
const matchApi = value.match(/https?:\/\/images.mapillary.com\/([^/]*)(&.*)?/)
|
||||
if (matchApi !== null) {
|
||||
return {key: matchApi[1]};
|
||||
}
|
||||
|
||||
|
||||
return {key: value, isApiv4: !isNaN(Number(value))};
|
||||
}
|
||||
|
||||
SourceIcon(backlinkSource?: string): BaseUIElement {
|
||||
return Svg.mapillary_svg();
|
||||
}
|
||||
|
||||
PrepareUrl(value: string): string {
|
||||
const key = Mapillary.ExtractKeyFromURL(value)
|
||||
return `https://images.mapillary.com/${key}/thumb-640.jpg?client_id=TXhLaWthQ1d4RUg0czVxaTVoRjFJZzowNDczNjUzNmIyNTQyYzI2`
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
protected DownloadAttribution(url: string): UIEventSource<LicenseInfo> {
|
||||
|
||||
const key = Mapillary.ExtractKeyFromURL(url)
|
||||
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
|
||||
|
||||
const metadataURL = `https://a.mapillary.com/v3/images/${key}?client_id=TXhLaWthQ1d4RUg0czVxaTVoRjFJZzowNDczNjUzNmIyNTQyYzI2`
|
||||
const source = new UIEventSource<LicenseInfo>(undefined)
|
||||
Utils.downloadJson(metadataURL).then(data => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue