a.t.
a.t.

Reputation: 2779

Verify magnet link in Python (using regular expressions)?

Context

I tried to verify a magnet link using Python. After checking out this answer, I tried to copy the solution to Python using:

def verify_magnet_link(magnet_link):
    print(f'magnet_link={magnet_link}')
    pattern='/magnet:\?xt=urn:[a-z0-9]+:[a-z0-9]{32}/i'
    print(f'pattern={pattern}')
    if (re.match(pattern,magnet_link) != None):
        print('Link is valid')
    else:
        print("Magnet link invalid.")

I verified the regular expression pattern in Regexr.com on the Ubuntu 22.04 OS magnet link:

magnet_link = 'magnet:?xt=urn:btih:FRVWQWGWDWUVIPKCGGTR3NFRZETEWBUF&dn=ubuntu-22.04-desktop-amd64.iso&xl=3654957056&tr.1=https%3A%2F%2Ftorrent.ubuntu.com%2Fannounce&tr.2=https%3A%2F%2Ftorrent.ubuntu.com%2Fannounce&tr.3=https%3A%2F%2Fipv6.torrent.ubuntu.com%2Fannounce'

# Specify an output dir
output_dir = "/home/"

verify_magnet_link(magnet_link)

However, that returns:

None
Magnet link invalid.

Question

How can one verify a magnet link formatting in Python?

Upvotes: 0

Views: 467

Answers (1)

a.t.
a.t.

Reputation: 2779

The verify_magnet_link(magnet_link) function verifies a Python link. If the link is valid, it prints Magnet link is valid! otherwise it prints Magnet link is invalid.

def verify_magnet_link(magnet_link):
    pattern=re.compile(r"magnet:\?xt=urn:[a-z0-9]+:[a-zA-Z0-9]{32}")
    result = pattern.match(magnet_link)
    if result != None:
        print("Magnet link is valid!")
    else:
        print("Magnet link is invalid.")


magnet_link = 'magnet:?xt=urn:btih:FRVWQWGWDWUVIPKCGGTR3NFRZETEWBUF&dn=ubuntu-22.04-desktop-amd64.iso&xl=3654957056&tr.1=https%3A%2F%2Ftorrent.ubuntu.com%2Fannounce&tr.2=https%3A%2F%2Ftorrent.ubuntu.com%2Fannounce&tr.3=https%3A%2F%2Fipv6.torrent.ubuntu.com%2Fannounce'

# Specify an output dir
output_dir = "/home/name"

verify_magnet_link(magnet_link)

Note that a slight change is implemented w.r.t. to the original answer in Javascript, the last [a-z0-9] is changed to [a-zA-Z0-9] because the FRVWQWGWDWUVIPKCGGTR3NFRZETEWBUF& is capitalised. Python did not give a match on the original answer with a-z0-9, however, regexr does: regexr.com/6nvl1 . I am not quite sure why this difference in behaviour occurs.

Upvotes: 1

Related Questions