From 4cdf7f4ab30ad5a7b45e6990a915e14ca1c1f63f Mon Sep 17 00:00:00 2001 From: TheAMM Date: Tue, 14 Nov 2017 21:27:15 +0200 Subject: [PATCH] Support searching for base32 info hash (BTIH) "BitTorrent info hashes" are generally found in magnet uris. An info hash is 40 characters in hex and 32 in base32 so the searches won't clash. --- nyaa/views/main.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/nyaa/views/main.py b/nyaa/views/main.py index 21ed75e..8dfe38f 100644 --- a/nyaa/views/main.py +++ b/nyaa/views/main.py @@ -1,3 +1,4 @@ +import base64 import math import re from datetime import datetime, timedelta @@ -107,12 +108,18 @@ def home(rss): special_results['first_word_user'] = models.User.by_username(user_word_match.group(1)) special_results['query_sans_user'] = user_word_match.group(2) - # Check if search is a 40-char torrent hash + # Check if search is a 40-char torrent hash (or 32-char base32 hash) infohash_match = re.match(r'(?i)^([a-f0-9]{40})$', search_term) + base32_infohash_match = re.match(r'(?i)^([a-z0-9]{32})$', search_term) if infohash_match: # Check for info hash in database matched_torrent = models.Torrent.by_info_hash_hex(infohash_match.group(1)) special_results['infohash_torrent'] = matched_torrent + elif base32_infohash_match: + # Convert base32 to info_hash + info_hash = base64.b32decode(base32_infohash_match.group(1)) + matched_torrent = models.Torrent.by_info_hash(info_hash) + special_results['infohash_torrent'] = matched_torrent query_args = { 'user': user_id,