Geoguessr Map-Making Auto-Tag

tag by date and address

目前為 2023-09-24 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Geoguessr Map-Making Auto-Tag
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  tag by date and address
// @author       KaKa
// @match        https://map-making.app/*
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_setClipboard
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    async function runScript(tags) {

        var api_key = GM_getValue("api_key");
        if (!api_key) {
            api_key = prompt("Please enter your Google API key");
            GM_setValue("api_key", api_key);
        }
        var text = await navigator.clipboard.readText();
        var data = JSON.parse(text);
        var newData = [];

        function get_Meta(id) {
            var url = "https://maps.googleapis.com/maps/api/streetview/metadata?pano=" + id + "&key=" + api_key;
            return fetch(url)
                .then(function(response) {
                    return response.json();
                })
                .then(function(data) {
                    console.log(data);
                    if (data.status == "OK") {
                        var date = data.date;
                        var cr = data.copyright;
                        var year = 'nodate';
                        var match = date.match(/\d{4}/);
                        if (match) {
                            year = match[0];
                        }

                        var panoType = 'unofficial';
                        if (cr.includes('Google')) {
                            panoType = 'official';
                        }

                        return [year, panoType];
                    } else {
                        return ["nodata","nodata"];
                    }
                })
                .catch(function(error) {
                    console.log(error);
                });
        }

        function search_Meta(lat, lng) {
            var url = "https://maps.googleapis.com/maps/api/streetview/metadata?location=" + lat + "," + lng + "&key=" + api_key;
            return fetch(url)
                .then(function(response) {
                    return response.json();
                })
                .then(function(data) {
                    console.log(data);
                    if (data.status == "OK") {
                        var date = data.date;
                        var cr = data.copyright;
                        var year = 'nodate';
                        var match = date.match(/\d{4}/);
                        if (match) {
                            year = match[0];
                        }

                        var panoType = 'unofficial';
                        if (cr.includes('Google')) {
                            panoType = 'official';
                        }

                        return [year, panoType, cr];
                    } else {
                        return ["nodata","nodata"];
                    }
                })
                .catch(function(error) {
                    console.log(error);
                });
        }

        let last_token = null;
        let last_token_expiry = 0;

        async function get_Token(api_key) {
            let current_time = Date.now() / 1000;
            if (last_token && last_token_expiry > current_time) {
                return last_token;
            }
            let url = `https://tile.googleapis.com/v1/createSession?key=${api_key}`;
            let headers = {'Content-Type': 'application/json'};
            let data = { "mapType": "streetview",
                        "language": "en-US",
                        "region": "US"};
            let response = await fetch(url, {method: 'POST', headers: headers, body: JSON.stringify(data)});
            if (response.status == 200) {
                let token = (await response.json()).session;
                last_token_expiry = current_time + 5 * 60;
                last_token = token;
                return token;
            } else {
                console.log(`Error: ${response.status}, ${await response.text()}`);
            }
        }

        async function get_add(id) {
            let tk = await get_Token(api_key);
            let url = `https://tile.googleapis.com/v1/streetview/metadata?session=${tk}&key=${api_key}&panoId=${id}`;
            let response = await fetch(url);
            let country = 'nodata';
            let subdivision = 'nodata';
            let locality = 'nodata';
            if (response.status == 200) {
                let data = await response.json();
                for (let add of data.addressComponents) {
                    if (add.types.includes('country')) {
                        country = add.longName;
                    }
                    if (add.types.includes('administrative_area_level_1')) {
                        subdivision = add.longName;
                    }
                    if (add.types.includes('locality')) {
                        locality = add.longName;
                    }
                }
            }
            return [country, subdivision, locality];
        }

        async function search_add(lat,lng) {
            let tk = await get_Token(api_key);
            let url = `https://tile.googleapis.com/v1/streetview/metadata?session=${tk}&key=${api_key}&lat=${lat}&lng=${lng}&radius=50`;
            let response = await fetch(url);
            let country = 'nodata';
            let subdivision = 'nodata';
            let locality = 'nodata';
            if (response.status == 200) {
                let data = await response.json();
                for (let add of data.addressComponents) {
                    if (add.types.includes('country')) {
                        country = add.longName;
                    }
                    if (add.types.includes('administrative_area_level_1')) {
                        subdivision = add.longName;
                    }
                    if (add.types.includes('locality')) {
                        locality = add.longName;
                    }
                }
            }
            return [country, subdivision, locality];
        }
        var CHUNK_SIZE = 1000;
        var promises = [];

        async function processCoord(coord, tags) {
            if (!coord.extra) {
                coord.extra = {};
            }
            if (!coord.extra.tags) {
                coord.extra.tags = [];
            }
            var meta;
            var address;
            if (coord.panoId) {
                meta = await get_Meta(coord.panoId);
                address= await get_add(coord.panoId);
            } else {
                meta = await search_Meta(coord.lat, coord.lng);
                address= await search_add(coord.lat, coord.lng);
            }
            if (meta && meta.length >= 2) {
                var year_tag = meta[0];
                var type_tag = meta[1];
                var country_tag=address[0]
                var subdivision_tag=address[1]
                var locality_tag=address[2]
                if (tags.includes('year')) coord.extra.tags.push(year_tag);
                if (tags.includes('type')) coord.extra.tags.push(type_tag);
                if (tags.includes('country')) coord.extra.tags.push(country_tag);
                if (tags.includes('subdivision')) coord.extra.tags.push(subdivision_tag);
                if (tags.includes('locality')) coord.extra.tags.push(locality_tag);
                newData.push(coord);
            }
}

        async function processChunk(chunk) {
            var promises = chunk.map(async coord => await processCoord(coord, tags));
            await Promise.all(promises);
        }

        async function processData(tags) {
            for (let i = 0; i < data.customCoordinates.length; i += CHUNK_SIZE) {
                let chunk = data.customCoordinates.slice(i, i + CHUNK_SIZE);
                await processChunk(chunk);
            }

            GM_setClipboard(JSON.stringify(newData));
            alert("New JSON data has been copied to the clipboard!");
        }
        await processData(tags);
    }

    function createButton(text, tags) {
        var button = document.createElement('button');
        button.textContent = text;
        button.addEventListener('click', async function() { await runScript(tags); });
        document.body.appendChild(button);
    }

    createButton('Tag by Year', ['year']);
    createButton('Tag by Type',[ 'type']);
    createButton('Tag by Country', ['country']);
    createButton('Tag by Subdivision', ['subdivision']);
    createButton('Tag by Locality', ['locality']);
})();