Skip to main content

wmlinksfromhell

CI PyPI Python License Latest Release

wmlinksfromhell is a Python package for working with links on Wikimedia and other MediaWiki sites. It is mainly focused on Wikimedia interwiki links, language and project prefixes, and Wikimedia URLs, with support for local links and external URLs too.

It can resolve links to their destination wiki and page, convert them between interwiki and URL forms, and give you the details you need to work with them in your code.

Developed by Max.

Full documentation is available on Read the Docs. Development takes place on GitHub, and bugs and feature requests can be reported in the issue tracker.

Project links

Install

python3 -m pip install wmlinksfromhell

Pywikibot support is optional:

python3 -m pip install 'wmlinksfromhell[pywikibot]'

Supports Python 3.9 and newer.

Quick start

Get information about a wiki:

import wmlinksfromhell

wiki = wmlinksfromhell.wiki("enwiki")

print(wiki.language)       # en
print(wiki.hostname)       # en.wikipedia.org
print(wiki.page_url("Apple"))
print(wiki.interwiki("Apple"))

Resolve a Wikimedia link:

result = wmlinksfromhell.resolve("w:en:Apple")

print(result.dbname)        # enwiki
print(result.title)         # Apple
print(result.url)           # https://en.wikipedia.org/wiki/Apple

Parse links from MediaWiki text:

import wmlinksfromhell

code = wmlinksfromhell.parse(
    "[[w:en:Apple]] https://de.wikipedia.org/wiki/Berlin https://example.org/",
    source="metawiki",
)

for link in code.filter_links():
    print(link.original)
    print(link.dbname, link.title, link.url)

The parser keeps the original way a link was written separate from the destination it resolves to. This lets you work with the same destination whether it came from an interwiki link, a wiki URL, or another supported form.

Wikimedia links

The resolver understands Wikimedia language and project prefixes, including prefix chains:

w:en:Apple
wikt:simple:uppercase
b:de:Main Page
:de:q:Hauptseite
:m:en:About
:w:it:b:Wiskunde

It also handles Wikimedia URL forms such as article paths and index.php, including fragments, revisions, diffs, actions, and page IDs.

For example:

result = wmlinksfromhell.resolve(
    "https://en.wikipedia.org/w/index.php?title=Apple&oldid=123456",
)

print(result.dbname)       # enwiki
print(result.revision)     # 123456
print(result.interwiki)    # w:en:Special:PermanentLink/123456

A leading colon changes MediaWiki's special handling for some links, but it does not necessarily change the destination. For example, [[en:Apple]] and [[:en:Apple]] both point to English Wikipedia's Apple page; the leading-colon form is used when the link should be shown as a normal link in the page.

Local and external links

Local targets are resolved when a source wiki is available:

result = wmlinksfromhell.resolve(
    "Apple",
    source="enwiki",
)

print(result.dbname)       # enwiki
print(result.title)        # Apple

Ordinary external URLs are kept as external destinations:

result = wmlinksfromhell.resolve("https://example.org/page")

print(result.status)             # ResolutionStatus.EXTERNAL
print(result.destination_type)   # DestinationType.EXTERNAL
print(result.url)

Finding links

filter_links() can select links by the things you care about:

code.filter_links(dbname="enwiki")
code.filter_links(family="wikipedia", language="en")
code.filter_links(namespace="MediaWiki")
code.filter_links(hostname="en.wikipedia.org")

code.filter_links(wiki_only=True)
code.filter_links(interwiki_only=True)
code.filter_links(local_only=True)
code.filter_links(external_only=True)

Links inside HTML comments are ignored by default. Include them when needed:

code.filter_links(comment_links=True)

Working with a link

Links expose both the original link and its resolved destination:

for link in code.filter_links():
    print(link.original)
    print(link.dbname)
    print(link.title)
    print(link.interwiki)
    print(link.url)
    print(link.destination)

Links can also be matched and converted:

link.matches(dbname="enwiki", title="Apple")

link.set_url()
link.set_interwiki()
link.set_local(source="enwiki")

Pywikibot

When Pywikibot is installed, a Page can be passed directly to parse_page():

import pywikibot
import wmlinksfromhell

site = pywikibot.Site("en", "wikipedia")
page = pywikibot.Page(site, "Apple")

code = wmlinksfromhell.parse_page(page)

for link in code.filter_links():
    print(link.original, link.url)

Metadata

Wikimedia SiteMatrix and interwiki metadata can be loaded and cached locally. The lower-level MetadataStore, Resolver, and Destination APIs are available when you need more control.

See the API Reference for the full API.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wmlinksfromhell-0.1.1.tar.gz (65.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wmlinksfromhell-0.1.1-py3-none-any.whl (45.4 kB view details)

Uploaded Python 3

File details

Details for the file wmlinksfromhell-0.1.1.tar.gz.

File metadata

  • Download URL: wmlinksfromhell-0.1.1.tar.gz
  • Upload date:
  • Size: 65.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.3

File hashes

Hashes for wmlinksfromhell-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d4b28e6aab3b5dd040d5d20b9b532405a5728098d2c638fa6ff52f468f8cf87f
MD5 900a8e25fe668079298c4075af2fc055
BLAKE2b-256 e23766a6ce5a25a9cb8c6dbb6dd042fe1ca4c3f8213791662c3392308e75a3fa

See more details on using hashes here.

File details

Details for the file wmlinksfromhell-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for wmlinksfromhell-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7265da7f3623bcdce47c42b09f46e29e30cd244c38edfbb0407f1ef6e0235b53
MD5 f14526a471c70d2c63984158542d2b5d
BLAKE2b-256 e4203052e403a39eb79f4792d5b1e2a1690d882aae4c10f680895195791dde21

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.3

2 files

0.1.2

2 files

This release

0.1.1 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page