Skip to content

Commit

Permalink
Optimise getting the infohash
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Jan 24, 2024
1 parent bec95d5 commit b63f14c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import random
from binascii import unhexlify
from datetime import datetime
import random
from struct import unpack
from typing import Optional
from typing import Dict, Optional

from lz4.frame import LZ4FrameCompressor
from pony import orm
Expand All @@ -11,8 +11,8 @@
from tribler.core import notifications
from tribler.core.components.database.category_filter.category import Category, default_category_filter
from tribler.core.components.database.category_filter.family_filter import default_xxx_filter
from tribler.core.components.database.db.serialization import EPOCH, REGULAR_TORRENT, TorrentMetadataPayload, \
HealthItemsPayload, time2int
from tribler.core.components.database.db.serialization import EPOCH, HealthItemsPayload, REGULAR_TORRENT, \
TorrentMetadataPayload, time2int
from tribler.core.utilities.notifier import Notifier
from tribler.core.utilities.tracker_utils import get_uniformed_tracker_url
from tribler.core.utilities.unicode import ensure_unicode, hexlify
Expand Down Expand Up @@ -42,7 +42,7 @@ def infohash_to_id(infohash):
return abs(unpack(">q", infohash[:8])[0])


def tdef_to_metadata_dict(tdef, category_filter: Category = None):
def tdef_to_metadata_dict(tdef, category_filter: Category = None) -> Dict:
"""
Helper function to create a TorrentMetadata-compatible dict from TorrentDef
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,11 @@ async def get_torrent_info(self, request):
self._logger.warning("Received metainfo is not a valid dictionary")
return RESTResponse({"error": "invalid response"}, status=HTTP_INTERNAL_SERVER_ERROR)

# Add the torrent to GigaChannel as a free-for-all entry, so others can search it
self.download_manager.notifier[notifications.torrent_metadata_added](
tdef_to_metadata_dict(TorrentDef.load_from_dict(metainfo)))

# TODO(Martijn): store the stuff in a database!!!
# TODO(Vadim): this means cache the downloaded torrent in a binary storage, like LevelDB
infohash = hashlib.sha1(lt.bencode(metainfo[b'info'])).digest()
# Add the torrent to metadata.db
metadata_dict = tdef_to_metadata_dict(TorrentDef.load_from_dict(metainfo))
self.download_manager.notifier[notifications.torrent_metadata_added](metadata_dict)

infohash = metadata_dict['infohash']
download = self.download_manager.downloads.get(infohash)
metainfo_request = self.download_manager.metainfo_requests.get(infohash, [None])[0]
download_is_metainfo_request = download == metainfo_request
Expand Down

0 comments on commit b63f14c

Please sign in to comment.