1
0
Fork 0
mirror of https://gitlab.com/SIGBUS/nyaa.git synced 2024-12-22 15:50:00 +00:00

Optimize magnet generation

with our trusty tool: caching!
This commit is contained in:
TheAMM 2019-10-31 21:30:40 +02:00
parent 611f0c5706
commit 95fc828107
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_fulltext import FullText
from sqlalchemy_utils import ChoiceType, EmailType, PasswordType 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 from nyaa.torrents import create_magnet
app = flask.current_app app = flask.current_app
@ -250,7 +250,6 @@ class TorrentBase(DeclarativeHelperBase):
return self.info_hash.hex() return self.info_hash.hex()
@property @property
@cache.memoize(timeout=3600)
def magnet_uri(self): def magnet_uri(self):
return create_magnet(self) return create_magnet(self)

View file

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