Fork 0
mirror of https://gitlab.com/SIGBUS/nyaa.git synced 2024-07-21 19:18:09 +00:00

Optimize magnet generation (#594)

with our trusty tool: caching!
This commit is contained in:
Anna-Maria Meriniemi 2020-07-12 10:14:35 +03:00 committed by GitHub
parent e545f8ae19
commit 4fe0ff5b1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 8 deletions

View file

@ -17,7 +17,7 @@ from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy_fulltext import FullText
from sqlalchemy_utils import ChoiceType, EmailType, PasswordType
from nyaa.extensions import cache, config, db
from nyaa.extensions import config, db
from nyaa.torrents import create_magnet
app = flask.current_app
@ -250,7 +250,6 @@ class TorrentBase(DeclarativeHelperBase):
return self.info_hash.hex()
def magnet_uri(self):
return create_magnet(self)

View file

@ -1,3 +1,4 @@
import functools
import os
from urllib.parse import quote, urlencode
@ -75,29 +76,34 @@ def get_default_trackers():
return list(trackers)
def create_magnet(torrent, max_trackers=5, trackers=None):
def _create_magnet(display_name, info_hash, max_trackers=5, trackers=None):
# Unless specified, we just use default trackers
if trackers is None:
trackers = get_default_trackers()
magnet_parts = [
('dn', torrent.display_name)
('dn', display_name)
('tr', tracker_url)
for tracker_url in trackers[:max_trackers]
return ''.join([
'magnet:?xt=urn:btih:', info_hash,
'&', urlencode(magnet_parts, quote_via=quote)
def create_magnet(torrent):
# Since we accept both models.Torrents and ES objects,
# we need to make sure the info_hash is a hex string
info_hash = torrent.info_hash
if isinstance(info_hash, (bytes, bytearray)):
info_hash = info_hash.hex()
return ''.join([
'magnet:?xt=urn:btih:', info_hash,
'&', urlencode(magnet_parts, quote_via=quote)
return _create_magnet(torrent.display_name, info_hash)
def create_default_metadata_base(torrent, trackers=None, webseeds=None):