MapComplete/src/Logic/ImageProviders/Imgur.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

125 lines
4.7 KiB
TypeScript
Raw Normal View History

2023-09-28 23:50:27 +02:00
import ImageProvider, { ProvidedImage } from "./ImageProvider"
import BaseUIElement from "../../UI/BaseUIElement"
import { Utils } from "../../Utils"
import Constants from "../../Models/Constants"
import { LicenseInfo } from "./LicenseInfo"
export class Imgur extends ImageProvider {
2021-11-07 16:34:51 +01:00
public static readonly defaultValuePrefix = ["https://i.imgur.com"]
public static readonly singleton = new Imgur()
public readonly name = "Imgur"
2021-11-07 16:34:51 +01:00
public readonly defaultKeyPrefixes: string[] = ["image"]
2023-09-27 22:21:35 +02:00
public static readonly apiUrl = "https://api.imgur.com/3/image"
public static readonly supportingUrls = ["https://i.imgur.com"]
private constructor() {
super()
}
2023-09-27 22:21:35 +02:00
apiUrls(): string[] {
return [Imgur.apiUrl]
}
SourceIcon(): BaseUIElement {
return undefined
}
public ExtractUrls(key: string, value: string): undefined | ProvidedImage[] {
2021-11-07 16:34:51 +01:00
if (Imgur.defaultValuePrefix.some((prefix) => value.startsWith(prefix))) {
return [
{
2021-11-07 16:34:51 +01:00
url: value,
key: key,
provider: this,
2024-11-14 02:21:10 +01:00
id: value,
isSpherical: false
2024-11-14 02:21:10 +01:00
},
2021-11-07 16:34:51 +01:00
]
}
return undefined
2021-11-07 16:34:51 +01:00
}
public static parseLicense(descr: string) {
const data: Record<string, string> = {}
if (!descr) {
return undefined
}
if (descr.toLowerCase() === "cc0") {
data.author = "Unknown"
data.license = "CC0"
} else {
for (const tag of descr.split("\n")) {
const kv = tag.split(":")
if (kv.length < 2) {
continue
}
const k = kv[0]
data[k] = kv[1]?.replace(/\r/g, "")
}
}
if (Object.keys(data).length === 0) {
return undefined
}
const licenseInfo = new LicenseInfo()
licenseInfo.licenseShortName = data.license
licenseInfo.artist = data.author
return licenseInfo
}
2022-06-13 00:51:53 +02:00
/**
2023-01-13 03:45:02 +01:00
* Download the attribution and license info for the picture at the given URL
2022-09-03 20:53:06 +02:00
*
2022-06-13 00:51:53 +02:00
* const data = {"data":{"id":"I9t6B7B","title":"Station Knokke","description":"author:Pieter Vander Vennet\r\nlicense:CC-BY 4.0\r\nosmid:node\/9812712386","datetime":1655052078,"type":"image\/jpeg","animated":false,"width":2400,"height":1795,"size":910872,"views":2,"bandwidth":1821744,"vote":null,"favorite":false,"nsfw":false,"section":null,"account_url":null,"account_id":null,"is_ad":false,"in_most_viral":false,"has_sound":false,"tags":[],"ad_type":0,"ad_url":"","edited":"0","in_gallery":false,"link":"https:\/\/i.imgur.com\/I9t6B7B.jpg","ad_config":{"safeFlags":["not_in_gallery","share"],"highRiskFlags":[],"unsafeFlags":["sixth_mod_unsafe"],"wallUnsafeFlags":[],"showsAds":false,"showAdLevel":1}},"success":true,"status":200}
* Utils.injectJsonDownloadForTests("https://api.imgur.com/3/image/E0RuAK3", data)
2024-04-01 18:20:30 +02:00
* const licenseInfo = await Imgur.singleton.DownloadAttribution({url: "https://i.imgur.com/E0RuAK3.jpg"})
2022-06-13 00:51:53 +02:00
* const expected = new LicenseInfo()
* expected.licenseShortName = "CC-BY 4.0"
* expected.artist = "Pieter Vander Vennet"
2023-12-06 01:29:32 +01:00
* expected.date = new Date(1655052078000)
* expected.views = 2
2022-06-13 00:51:53 +02:00
* licenseInfo // => expected
2024-04-01 18:20:30 +02:00
* const licenseInfoJpeg = await Imgur.singleton.DownloadAttribution({url:"https://i.imgur.com/E0RuAK3.jpeg"})
* licenseInfoJpeg // => expected
2024-04-01 18:20:30 +02:00
* const licenseInfoUpperCase = await Imgur.singleton.DownloadAttribution({url: "https://i.imgur.com/E0RuAK3.JPEG"})
* licenseInfoUpperCase // => expected
*
*
2022-06-13 00:51:53 +02:00
*/
2024-11-14 02:21:10 +01:00
public async DownloadAttribution(
providedImage: {
url: string
},
withResponse?: (obj) => void
): Promise<LicenseInfo> {
2024-04-01 02:00:48 +02:00
const url = providedImage.url
const hash = url.substr("https://i.imgur.com/".length).split(/(\.jpe?g)|(\.png)/i)[0]
const apiUrl = "https://api.imgur.com/3/image/" + hash
2024-07-21 10:52:51 +02:00
const response = await Utils.downloadJsonCached<{
data: { description: string; datetime: string; views: number }
}>(apiUrl, 365 * 24 * 60 * 60, {
2024-11-14 02:21:10 +01:00
Authorization: "Client-ID " + Constants.ImgurApiKey,
2022-06-13 00:51:53 +02:00
})
if (withResponse) {
withResponse(response)
}
const imgurData = response.data
2024-11-14 02:21:10 +01:00
const license = Imgur.parseLicense(imgurData.description ?? "")
if (license) {
license.views = imgurData.views
license.date = new Date(Number(imgurData.datetime) * 1000)
}
return license
}
getPanoramaInfo(image: { id: string }): undefined {
return undefined
}
}