| 
									
										
										
										
											2024-05-16 00:12:50 +02:00
										 |  |  | import Script from "./Script" | 
					
						
							|  |  |  | import NameSuggestionIndex, { NSIItem } from "../src/Logic/Web/NameSuggestionIndex" | 
					
						
							|  |  |  | import * as nsiWD from "../node_modules/name-suggestion-index/dist/wikidata.min.json" | 
					
						
							| 
									
										
										
										
											2025-01-09 20:39:21 +01:00
										 |  |  | import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from "fs" | 
					
						
							| 
									
										
										
										
											2024-05-16 00:12:50 +02:00
										 |  |  | import ScriptUtils from "./ScriptUtils" | 
					
						
							|  |  |  | import { Utils } from "../src/Utils" | 
					
						
							| 
									
										
										
										
											2025-01-09 20:39:21 +01:00
										 |  |  | import { LayerConfigJson } from "../src/Models/ThemeConfig/Json/LayerConfigJson" | 
					
						
							| 
									
										
										
										
											2025-01-10 14:09:54 +01:00
										 |  |  | import { FilterConfigOptionJson } from "../src/Models/ThemeConfig/Json/FilterConfigJson" | 
					
						
							| 
									
										
										
										
											2025-01-09 20:39:21 +01:00
										 |  |  | import { TagUtils } from "../src/Logic/Tags/TagUtils" | 
					
						
							| 
									
										
										
										
											2025-01-10 14:09:54 +01:00
										 |  |  | import { TagRenderingConfigJson } from "../src/Models/ThemeConfig/Json/TagRenderingConfigJson" | 
					
						
							| 
									
										
										
										
											2024-05-16 00:12:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-23 11:28:51 +02:00
										 |  |  | class DownloadNsiLogos extends Script { | 
					
						
							| 
									
										
										
										
											2024-05-16 00:12:50 +02:00
										 |  |  |     constructor() { | 
					
						
							|  |  |  |         super("Downloads all images of the NSI") | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private async downloadLogo(nsiItem: NSIItem, type: string, basePath: string) { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             return await this.downloadLogoUnsafe(nsiItem, type, basePath) | 
					
						
							|  |  |  |         } catch (e) { | 
					
						
							|  |  |  |             console.error("Could not download", nsiItem.displayName, "due to", e) | 
					
						
							| 
									
										
										
										
											2024-07-08 23:13:57 +02:00
										 |  |  |             return "error" | 
					
						
							| 
									
										
										
										
											2024-05-16 00:12:50 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private async downloadLogoUnsafe(nsiItem: NSIItem, type: string, basePath: string) { | 
					
						
							|  |  |  |         if (nsiItem === undefined) { | 
					
						
							|  |  |  |             return false | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         let path = basePath + nsiItem.id | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const logos = nsiWD["wikidata"][nsiItem?.tags?.[type + ":wikidata"]]?.logos | 
					
						
							| 
									
										
										
										
											2025-01-08 14:21:07 +01:00
										 |  |  |         const nsi = await NameSuggestionIndex.getNsiIndex() | 
					
						
							|  |  |  |         if (nsi.isSvg(nsiItem, type)) { | 
					
						
							| 
									
										
										
										
											2024-05-16 00:12:50 +02:00
										 |  |  |             path = path + ".svg" | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (existsSync(path)) { | 
					
						
							|  |  |  |             return false | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!logos) { | 
					
						
							|  |  |  |             return false | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (logos.facebook) { | 
					
						
							| 
									
										
										
										
											2024-07-08 23:13:57 +02:00
										 |  |  |             // Facebook's logos are generally better and square
 | 
					
						
							| 
									
										
										
										
											2024-05-16 00:12:50 +02:00
										 |  |  |             await ScriptUtils.DownloadFileTo(logos.facebook, path) | 
					
						
							| 
									
										
										
										
											2024-07-29 03:16:35 +02:00
										 |  |  |             // Validate
 | 
					
						
							|  |  |  |             const content = readFileSync(path, "utf8") | 
					
						
							| 
									
										
										
										
											2025-01-09 20:39:21 +01:00
										 |  |  |             if (content.startsWith("{\"error\"")) { | 
					
						
							| 
									
										
										
										
											2024-07-29 03:16:35 +02:00
										 |  |  |                 unlinkSync(path) | 
					
						
							| 
									
										
										
										
											2024-08-09 16:55:08 +02:00
										 |  |  |                 console.error("Attempted to fetch", logos.facebook, " but this gave an error") | 
					
						
							|  |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2024-07-29 03:16:35 +02:00
										 |  |  |                 return true | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-05-16 00:12:50 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         if (logos.wikidata) { | 
					
						
							|  |  |  |             let url: string = logos.wikidata | 
					
						
							|  |  |  |             console.log("Downloading", url) | 
					
						
							|  |  |  |             let ttl = 10 | 
					
						
							|  |  |  |             do { | 
					
						
							|  |  |  |                 ttl-- | 
					
						
							|  |  |  |                 const dloaded = await Utils.downloadAdvanced(url, { | 
					
						
							| 
									
										
										
										
											2024-06-16 16:06:26 +02:00
										 |  |  |                     "User-Agent": | 
					
						
							|  |  |  |                         "MapComplete NSI scraper/0.1 (https://github.com/pietervdvn/MapComplete; pietervdvn@posteo.net)", | 
					
						
							| 
									
										
										
										
											2024-05-16 00:12:50 +02:00
										 |  |  |                 }) | 
					
						
							|  |  |  |                 const redirect: string | undefined = dloaded["redirect"] | 
					
						
							|  |  |  |                 if (redirect) { | 
					
						
							|  |  |  |                     console.log("Got a redirect from", url, "to", redirect) | 
					
						
							|  |  |  |                     url = redirect | 
					
						
							|  |  |  |                     continue | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if ((<string>logos.wikidata).toLowerCase().endsWith(".svg")) { | 
					
						
							|  |  |  |                     console.log("Written SVG", path) | 
					
						
							| 
									
										
										
										
											2024-05-23 11:28:51 +02:00
										 |  |  |                     if (!path.endsWith(".svg")) { | 
					
						
							|  |  |  |                         throw "Undetected svg path:" + logos.wikidata | 
					
						
							| 
									
										
										
										
											2024-05-16 00:12:50 +02:00
										 |  |  |                     } | 
					
						
							|  |  |  |                     writeFileSync(path, dloaded["content"], "utf8") | 
					
						
							|  |  |  |                     return true | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 console.log("Got data from", url, "-->", path) | 
					
						
							|  |  |  |                 await ScriptUtils.DownloadFileTo(url, path) | 
					
						
							|  |  |  |                 return true | 
					
						
							|  |  |  |             } while (ttl > 0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return false | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return false | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-23 11:28:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 20:39:21 +01:00
										 |  |  |     async downloadFor(type: string): Promise<void> { | 
					
						
							| 
									
										
										
										
											2025-01-08 14:21:07 +01:00
										 |  |  |         const nsi = await NameSuggestionIndex.getNsiIndex() | 
					
						
							|  |  |  |         const items = nsi.allPossible(type) | 
					
						
							| 
									
										
										
										
											2024-05-16 00:12:50 +02:00
										 |  |  |         const basePath = "./public/assets/data/nsi/logos/" | 
					
						
							|  |  |  |         let downloadCount = 0 | 
					
						
							| 
									
										
										
										
											2024-07-08 23:13:57 +02:00
										 |  |  |         const stepcount = 5 | 
					
						
							| 
									
										
										
										
											2024-05-16 00:12:50 +02:00
										 |  |  |         for (let i = 0; i < items.length; i += stepcount) { | 
					
						
							| 
									
										
										
										
											2024-07-08 23:13:57 +02:00
										 |  |  |             if (downloadCount > 0 || i % 200 === 0) { | 
					
						
							| 
									
										
										
										
											2024-05-16 00:12:50 +02:00
										 |  |  |                 console.log(i + "/" + items.length, "downloaded " + downloadCount) | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-07-08 23:13:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             const results = await Promise.all( | 
					
						
							| 
									
										
										
										
											2024-06-16 16:06:26 +02:00
										 |  |  |                 Utils.TimesT(stepcount, (j) => j).map(async (j) => { | 
					
						
							|  |  |  |                     const downloaded = await this.downloadLogo(items[i + j], type, basePath) | 
					
						
							|  |  |  |                     if (downloaded) { | 
					
						
							|  |  |  |                         downloadCount++ | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2024-07-08 23:13:57 +02:00
										 |  |  |                     return downloaded | 
					
						
							| 
									
										
										
										
											2025-01-09 20:39:21 +01:00
										 |  |  |                 }), | 
					
						
							| 
									
										
										
										
											2024-06-16 16:06:26 +02:00
										 |  |  |             ) | 
					
						
							| 
									
										
										
										
											2024-07-08 23:13:57 +02:00
										 |  |  |             for (let j = 0; j < results.length; j++) { | 
					
						
							|  |  |  |                 let didDownload = results[j] | 
					
						
							| 
									
										
										
										
											2024-07-09 13:42:08 +02:00
										 |  |  |                 if (didDownload !== "error") { | 
					
						
							| 
									
										
										
										
											2024-07-08 23:13:57 +02:00
										 |  |  |                     continue | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 console.log("Retrying", items[i + j].id, type) | 
					
						
							|  |  |  |                 didDownload = await this.downloadLogo(items[i + j], type, basePath) | 
					
						
							| 
									
										
										
										
											2024-07-09 13:42:08 +02:00
										 |  |  |                 if (didDownload === "error") { | 
					
						
							| 
									
										
										
										
											2024-07-08 23:13:57 +02:00
										 |  |  |                     console.log("Failed again:", items[i + j].id) | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-05-16 00:12:50 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-09 20:39:21 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private async generateRendering(type: string) { | 
					
						
							|  |  |  |         const nsi = await NameSuggestionIndex.getNsiIndex() | 
					
						
							|  |  |  |         const items = nsi.allPossible(type) | 
					
						
							|  |  |  |         const filterOptions: FilterConfigOptionJson[] = items.map(item => { | 
					
						
							|  |  |  |             return ({ | 
					
						
							|  |  |  |                 question: item.displayName, | 
					
						
							|  |  |  |                 icon: nsi.getIconUrl(item, type), | 
					
						
							| 
									
										
										
										
											2025-01-10 14:09:54 +01:00
										 |  |  |                 osmTags: NameSuggestionIndex.asFilterTags(item), | 
					
						
							| 
									
										
										
										
											2025-01-09 20:39:21 +01:00
										 |  |  |             }) | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2025-01-10 14:09:54 +01:00
										 |  |  |         const mappings = items.map(item => ({ | 
					
						
							|  |  |  |             if: NameSuggestionIndex.asFilterTags(item), | 
					
						
							|  |  |  |             then: nsi.getIconUrl(item, type), | 
					
						
							|  |  |  |         })) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         console.log("Checking for shadow-mappings...") | 
					
						
							| 
									
										
										
										
											2025-01-11 02:38:18 +01:00
										 |  |  |         for (let i = mappings.length - 1; i >= 0; i--) { | 
					
						
							| 
									
										
										
										
											2025-01-10 14:09:54 +01:00
										 |  |  |             const condition = TagUtils.Tag(mappings[i].if) | 
					
						
							| 
									
										
										
										
											2025-01-11 02:38:18 +01:00
										 |  |  |             if (i % 100 === 0) { | 
					
						
							|  |  |  |                 console.log("Checking for shadow-mappings...", i, "/", mappings.length) | 
					
						
							| 
									
										
										
										
											2025-01-10 14:09:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2025-01-11 02:38:18 +01:00
										 |  |  |             const shadowsSomething = mappings.some((m, j) => { | 
					
						
							|  |  |  |                 if (i === j) { | 
					
						
							| 
									
										
										
										
											2025-01-10 14:09:54 +01:00
										 |  |  |                     return false | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 return condition.shadows(TagUtils.Tag(m.if)) | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |             // If this one matches, the other one will match as well
 | 
					
						
							|  |  |  |             // We can thus remove this one in favour of the other one
 | 
					
						
							| 
									
										
										
										
											2025-01-11 02:38:18 +01:00
										 |  |  |             if (shadowsSomething) { | 
					
						
							| 
									
										
										
										
											2025-01-10 14:09:54 +01:00
										 |  |  |                 mappings.splice(i, 1) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const iconsTr: TagRenderingConfigJson = <any>{ | 
					
						
							|  |  |  |             strict: true, | 
					
						
							|  |  |  |             id: "icon", | 
					
						
							|  |  |  |             mappings, | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-01-09 20:39:21 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const config: LayerConfigJson = { | 
					
						
							|  |  |  |             id: "nsi_" + type, | 
					
						
							|  |  |  |             description: { | 
					
						
							|  |  |  |                 en: "Exposes part of the NSI to reuse in other themes, e.g. for rendering", | 
					
						
							|  |  |  |             }, | 
					
						
							| 
									
										
										
										
											2025-01-11 02:37:16 +01:00
										 |  |  |             source: "special:library", | 
					
						
							| 
									
										
										
										
											2025-01-09 20:39:21 +01:00
										 |  |  |             pointRendering: null, | 
					
						
							| 
									
										
										
										
											2025-01-10 14:09:54 +01:00
										 |  |  |             tagRenderings: [ | 
					
						
							|  |  |  |                 iconsTr, | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2025-01-09 20:39:21 +01:00
										 |  |  |             filter: [ | 
					
						
							| 
									
										
										
										
											2025-01-10 14:09:54 +01:00
										 |  |  |                 <any>{ | 
					
						
							| 
									
										
										
										
											2025-01-11 02:38:18 +01:00
										 |  |  |                     "#": "ignore-possible-duplicate", | 
					
						
							| 
									
										
										
										
											2025-01-09 20:39:21 +01:00
										 |  |  |                     id: type, | 
					
						
							|  |  |  |                     strict: true, | 
					
						
							| 
									
										
										
										
											2025-01-10 14:09:54 +01:00
										 |  |  |                     options: [{ question: type }, ...filterOptions], | 
					
						
							| 
									
										
										
										
											2025-01-09 20:39:21 +01:00
										 |  |  |                 }, | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |             allowMove: false, | 
					
						
							| 
									
										
										
										
											2025-01-11 02:37:16 +01:00
										 |  |  |             "#dont-translate": "*", | 
					
						
							| 
									
										
										
										
											2025-01-09 20:39:21 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         const path = "./assets/layers/nsi_" + type | 
					
						
							|  |  |  |         mkdirSync(path, { recursive: true }) | 
					
						
							|  |  |  |         writeFileSync(path + "/nsi_" + type + ".json", JSON.stringify(config, null, "  ")) | 
					
						
							|  |  |  |         console.log("Written", path) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async main(): Promise<void> { | 
					
						
							|  |  |  |         const nsi = await NameSuggestionIndex.getNsiIndex() | 
					
						
							|  |  |  |         const types = ["brand", "operator"] | 
					
						
							| 
									
										
										
										
											2025-01-11 02:38:18 +01:00
										 |  |  |         for (const type of types) { | 
					
						
							|  |  |  |             await this.downloadFor(type) | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-01-09 20:39:21 +01:00
										 |  |  |         for (const type of types) { | 
					
						
							|  |  |  |             await this.generateRendering(type) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-16 00:12:50 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | new DownloadNsiLogos().run() |