Scripts: more fixes to the uploadPanoramaxScript

This commit is contained in:
Pieter Vander Vennet 2024-11-10 23:51:41 +01:00
parent 664ddc686c
commit 6e335c91d4
5 changed files with 275 additions and 93 deletions

View file

@ -11,6 +11,7 @@ export class Imgur extends ImageProvider {
public readonly defaultKeyPrefixes: string[] = ["image"]
public static readonly apiUrl = "https://api.imgur.com/3/image"
public static readonly supportingUrls = ["https://i.imgur.com"]
private constructor() {
super()
}
@ -30,13 +31,44 @@ export class Imgur extends ImageProvider {
url: value,
key: key,
provider: this,
id: value,
},
id: value
}
]
}
return undefined
}
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
}
/**
* Download the attribution and license info for the picture at the given URL
*
@ -56,7 +88,9 @@ export class Imgur extends ImageProvider {
*
*
*/
public async DownloadAttribution(providedImage: { url: string }): Promise<LicenseInfo> {
public async DownloadAttribution(providedImage: {
url: string
}, withResponse?: (obj) => void): Promise<LicenseInfo> {
const url = providedImage.url
const hash = url.substr("https://i.imgur.com/".length).split(/\.jpe?g/i)[0]
@ -64,26 +98,17 @@ export class Imgur extends ImageProvider {
const response = await Utils.downloadJsonCached<{
data: { description: string; datetime: string; views: number }
}>(apiUrl, 365 * 24 * 60 * 60, {
Authorization: "Client-ID " + Constants.ImgurApiKey,
Authorization: "Client-ID " + Constants.ImgurApiKey
})
const descr = response.data.description ?? ""
const data: any = {}
const imgurData = response.data
for (const tag of descr.split("\n")) {
const kv = tag.split(":")
const k = kv[0]
data[k] = kv[1]?.replace(/\r/g, "")
if (withResponse) {
withResponse(response)
}
const licenseInfo = new LicenseInfo()
const imgurData = response.data
const license= Imgur.parseLicense(imgurData.description ?? "")
license.views = imgurData.views
license.date = new Date(Number(imgurData.datetime) * 1000)
licenseInfo.licenseShortName = data.license
licenseInfo.artist = data.author
licenseInfo.date = new Date(Number(imgurData.datetime) * 1000)
licenseInfo.views = imgurData.views
return licenseInfo
return license
}
}

View file

@ -145,12 +145,7 @@ export default class PanoramaxImageProvider extends ImageProvider {
)
}
Stores.Chronic(1500, () => hasLoading(source.data)).addCallback((_) => {
console.log(
"Testing panoramax URLS again as some were loading",
source.data,
hasLoading(source.data)
)
Stores.Chronic(1500, () => hasLoading(source.data)).addCallback(() => {
super.getRelevantUrlsFor(tags, prefixes).then((data) => {
source.set(data)
return !hasLoading(data)
@ -192,9 +187,9 @@ export default class PanoramaxImageProvider extends ImageProvider {
export class PanoramaxUploader implements ImageUploader {
public readonly panoramax: AuthorizedPanoramax
maxFileSizeInMegabytes = 100 * 1000 * 1000 // 100MB
private readonly _targetSequence: Store<string>
private readonly _targetSequence?: Store<string>
constructor(url: string, token: string, targetSequence: Store<string>) {
constructor(url: string, token: string, targetSequence?: Store<string>) {
this._targetSequence = targetSequence
this.panoramax = new AuthorizedPanoramax(url, token)
}
@ -204,7 +199,8 @@ export class PanoramaxUploader implements ImageUploader {
currentGps: [number, number],
author: string,
noblur: boolean = false,
sequenceId?: string
sequenceId?: string,
datetime?: string
): Promise<{
key: string
value: string
@ -213,7 +209,7 @@ export class PanoramaxUploader implements ImageUploader {
// https://panoramax.openstreetmap.fr/api/docs/swagger#/
let [lon, lat] = currentGps
let datetime = new Date().toISOString()
datetime ??= new Date().toISOString()
try {
const tags = await ExifReader.load(blob)
const [[latD], [latM], [latS, latSDenom]] = <