Hi all,
again a newbie question: How do i "translate" an interwiki link into a URL?
Example: Parsing "[[:mw:Manual:Pywikibot|Manual:Pywikibot]]" using mwparserfromhell yields a WikiLink object with title=":mw:Manual:Pywikibot" and text = "Manual:Pywikibot" (both of type WikiCode). Is there a way to translate the title part ":mw:Manual:Pywikibot" into the URL https://www.mediawiki.org/wiki/Manual:Pywikibot ?
Thank you for any hints!
Florian
First create a Page object, where 'mediawiki' is the site, and 'Manual:Pywikibot' is the title. You will get the desired URL with this method: https://doc.wikimedia.org/pywikibot/master/api_ref/pywikibot.page.html#page....
tohuuu th@tohuuu.de ezt írta (időpont: 2024. aug. 23., P, 11:49):
Hi all,
again a newbie question: How do i "translate" an interwiki link into a URL?
Example: Parsing "[[:mw:Manual:Pywikibot|Manual:Pywikibot]]" using mwparserfromhell yields a WikiLink object with title=":mw:Manual:Pywikibot" and text = "Manual:Pywikibot" (both of type WikiCode). Is there a way to translate the title part ":mw:Manual:Pywikibot" into the URL https://www.mediawiki.org/wiki/Manual:Pywikibot ?
Thank you for any hints!
Florian
pywikibot mailing list -- pywikibot@lists.wikimedia.org Public archives at https://lists.wikimedia.org/hyperkitty/list/pywikibot@lists.wikimedia.org/me... To unsubscribe send an email to pywikibot-leave@lists.wikimedia.org
Bináris,
thanks for your help - but to do so, I suppose have to translate the "mw" prefix into "mediawiki" first. That's the crucial point, since the code should be able to handle any valid interwiki prefix. there a way to do this?
In the meanwhile, I've tried to write a small API query:
deftranslate_interwiki(prefix:str, site:pywikibot.Site) -> str: r =pywikibot.data.api.Request(parameters={'action': 'query', 'meta': 'siteinfo', 'siprop': 'interwikimap'}) data =r.submit() iwm =data['query']['interwikimap'] entry =next((item foritem iniwm ifitem["prefix"] ==prefix), False) returnentry['url']
But I'm not sure if there's already something built into pwb?
Cheers Florian
Am 23.08.2024 um 12:41 schrieb Bináris:
First create a Page object, where 'mediawiki' is the site, and 'Manual:Pywikibot' is the title. You will get the desired URL with this method: https://doc.wikimedia.org/pywikibot/master/api_ref/pywikibot.page.html#page....
Bináris,
great, thanks a lot, exactly what i'm needing! Issue solved.
Florian
Am 23.08.2024 um 13:14 schrieb Bináris:
I think these two will do the job:
https://doc.wikimedia.org/pywikibot/master/api_ref/pywikibot.site.html#pywik... https://doc.wikimedia.org/pywikibot/master/api_ref/pywikibot.site.html#id8
pywikibot mailing list -- pywikibot@lists.wikimedia.org Public archives at https://lists.wikimedia.org/hyperkitty/list/pywikibot@lists.wikimedia.org/me... To unsubscribe send an email to pywikibot-leave@lists.wikimedia.org
Just for the sake of completeness: I've just found that Pywikibot's Link() class makes thinks just easier by parsing site, namespace and page title automatically.
title=":mw:Manual:Pywikibot" l =pywikibot.page.Link(title) url =pywikibot.Page(l.site, l.title, l.namespace.id).full_url()
Florian
You are welcome, and I also learned something. :)
tohuuu th@tohuuu.de ezt írta (időpont: 2024. aug. 23., P, 14:44):
Just for the sake of completeness: I've just found that Pywikibot's Link() class makes thinks just easier by parsing site, namespace and page title automatically.
title=":mw:Manual:Pywikibot" l = pywikibot.page.Link(title) url = pywikibot.Page(l.site, l.title, l.namespace.id
).full_url()
Florian
pywikibot mailing list -- pywikibot@lists.wikimedia.org Public archives at https://lists.wikimedia.org/hyperkitty/list/pywikibot@lists.wikimedia.org/me... To unsubscribe send an email to pywikibot-leave@lists.wikimedia.org