ifl0w / RandomWallpaperGnome3

Random Wallpapers for Gnome 3
MIT License
178 stars 42 forks source link

Wikimedia #21

Open tbo47 opened 7 years ago

tbo47 commented 7 years ago

Wikimedia has a rss/atom feed of a best picture in there catalog every day. It would be nice to use it as a feed of wallpapers! https://commons.wikimedia.org/wiki/Commons:Picture_of_the_day

ifl0w commented 7 years ago

This would be possible quiet easy but I just suggested a Generic JSON API source in #22 where you also could add your opinion if you want to. Since the Wikimedia picture would be an XML response we would have to think of another solution but it would be also possible to add a Generic XML API source.

lens0021 commented 1 year ago

Wikimedia Commons also has its own API.

What I've found * Picture of the day is picked up from [MediaWiki:Ffeed-potd-transcludeme](https://commons.wikimedia.org/wiki/MediaWiki:Ffeed-potd-transcludeme) * It transcludes `Template:Potd/YYYY-MM-DD`, for example [Template:Potd/2023-04-10](https://commons.wikimedia.org/wiki/Template:Potd/2023-04-10) * A wikitext [`{{potd}}`](https://commons.wikimedia.org/wiki/Template:Potd) can be used for fetching potd. The wikitext behind is `[[File:{{Potd/{{#time:Y-m-d||en}}}}]]`

I tried the next sources, but failed:

It seems $.parse.images[0] $.parse.images and $.parse are null, while$ is an object. Any ideas?

Lucki commented 1 year ago

The following works:

References `https://upload.wikimedia.org/wikipedia/commons/a/a0/Geraldine_Ulmar_in_Gilbert_and_Sullivan%27s_The_Mikado.jpg` ~~~ json { "parse": { "title": "API", "pageid": 0, "images": [ "Geraldine_Ulmar_in_Gilbert_and_Sullivan's_The_Mikado.jpg" ] } } ~~~

Edit:

Lucki commented 1 year ago

This is actually a bit more involved.

https://github.com/ifl0w/RandomWallpaperGnome3/blob/02c8bfd549a87f0ecf29af917c636fd273a3b635/randomwallpaper%40iflow.space/sourceAdapter.js#L350

Possible fixes:


@ifl0w Any opinions?

ifl0w commented 1 year ago

@Lucki I'd not remove the encoding entirely since I'd assume that this could break existing setups. If the encodeURIComponent() solution fundamentally works, we could try that.

Another somewhat hacky approach could also be to simply call decodeURI before calling endodeURI. This should not have an effect on decoded URIs and I guess would work around this issue here?

Lucki commented 1 year ago

I'm sorry, we can't use encodeURIComponent() because that'll also encode https:// and path separators.

URL c&p from Firefox:                   https://commons.wikimedia.org/w/api.php?action=parse&format=json&text=%5B%5BFile%3A%7B%7BPotd%2F%7B%7B%23time%3AY-m-d%7C%7Cen%7D%7D%7D%7D%5D%5D&prop=images&contentmodel=wikitext&formatversion=2
decodeURI:                              https://commons.wikimedia.org/w/api.php?action=parse&format=json&text=[[File%3A{{Potd%2F{{%23time%3AY-m-d||en}}}}]]&prop=images&contentmodel=wikitext&formatversion=2
decodeURIComponent:                     https://commons.wikimedia.org/w/api.php?action=parse&format=json&text=[[File:{{Potd/{{#time:Y-m-d||en}}}}]]&prop=images&contentmodel=wikitext&formatversion=2
encodeURI(decodeURI):                   https://commons.wikimedia.org/w/api.php?action=parse&format=json&text=%5B%5BFile%253A%7B%7BPotd%252F%7B%7B%2523time%253AY-m-d%7C%7Cen%7D%7D%7D%7D%5D%5D&prop=images&contentmodel=wikitext&formatversion=2
encodeURI(decodeURIComponent):          https://commons.wikimedia.org/w/api.php?action=parse&format=json&text=%5B%5BFile:%7B%7BPotd/%7B%7B#time:Y-m-d%7C%7Cen%7D%7D%7D%7D%5D%5D&prop=images&contentmodel=wikitext&formatversion=2
encodeURIComponent(decodeURI):          https%3A%2F%2Fcommons.wikimedia.org%2Fw%2Fapi.php%3Faction%3Dparse%26format%3Djson%26text%3D%5B%5BFile%253A%7B%7BPotd%252F%7B%7B%2523time%253AY-m-d%7C%7Cen%7D%7D%7D%7D%5D%5D%26prop%3Dimages%26contentmodel%3Dwikitext%26formatversion%3D2
encodeURIComponent(decodeURIComponent): https%3A%2F%2Fcommons.wikimedia.org%2Fw%2Fapi.php%3Faction%3Dparse%26format%3Djson%26text%3D%5B%5BFile%3A%7B%7BPotd%2F%7B%7B%23time%3AY-m-d%7C%7Cen%7D%7D%7D%7D%5D%5D%26prop%3Dimages%26contentmodel%3Dwikitext%26formatversion%3D2

No matter how we encode and decode - it's never matching the correct URL copied from Firefox.

lens0021 commented 1 year ago

I do not understand the problem exactly, but an api call https://commons.wikimedia.org/w/api.php?action=parse&format=json&pageid=18176574&prop=images&formatversion=2 is also available (18176574 is the page id of MediaWiki:feed-potd-transcludeme) . Does this solve something?

Lucki commented 1 year ago

For this particular case that's a workaround, yes.

The problem is for other sites where it's not this easily possible. A general solution would be better. Also, this prevents pasting a URL from your browser address bar which is quite an inconvenience.

tbo47 commented 1 year ago

Here is an example on how to find the latest picture in plain js:

const https = require('https');
const fs = require('fs');

const resolution = process.argv.at(2) || '1280px' // 1024px 2560px

const url = 'https://commons.wikimedia.org/w/api.php?action=featuredfeed&feed=potd&feedformat=atom';

const match1 = 'href="https://commons.wikimedia.org/wiki/Special'
const match2 = 'typeof="mw:File"'

const getUrl = (url) => {
    return new Promise((resolve) => {
        https.get(url, resp => {
            let data = ''
            resp.on('data', c => data += c)
            resp.on('end', () => {
                resolve(data)
            })
        })
    })
}

const download = (url, dest) => {
    return new Promise((resolve) => {
        const file = fs.createWriteStream(dest);
        https.get(url, resp => {
            resp.pipe(file);
            file.on('finish', () => file.close(resolve))
        })
    })
}

(async () => {
    const data = await getUrl(url)
    const urls = data.split(/\n/).filter(l => l.includes(match1)).map(l => l.split(/"/)[5])
    const lastUrl = urls.slice(-1)[0]
    const data2 = await getUrl(lastUrl)
    const picUrl = data2.split(/\n/).filter(l => l.includes(match2)).map(l => l.split(/"/)[7])
    const picName = picUrl[0].slice(11)
    const picFullUrl = `https://upload.wikimedia.org/wikipedia/commons/thumb/5/50/${picName}/${resolution}-${picName}`
    download(picFullUrl, '/tmp/wikimedia.jpg')
})()
HarrySidhuz0008 commented 1 year ago

<!DOCTYPE html>