Example #1
0
    def add_torrent_def_to_channel(self, channel_id, torrent_def, extra_info={}, forward=True):
        """
        Adds a TorrentDef to a Channel.

        :param channel_id: id of the Channel to add the Torrent to
        :param torrent_def: definition of the Torrent to add
        :param extra_info: description of the Torrent to add
        :param forward: when True the messages are forwarded (as defined by their message
         destination policy) to other nodes in the community. This parameter should (almost always)
         be True, its inclusion is mostly to allow certain debugging scenarios
        """
        # Make sure that this new torrent_def is also in collected torrents
        self.lm.rtorrent_handler.save_torrent(torrent_def)

        channelcast_db = self.open_dbhandler(NTFY_CHANNELCAST)
        if channelcast_db.hasTorrent(channel_id, torrent_def.infohash):
            raise DuplicateTorrentFileError("This torrent file already exists in your channel.")

        dispersy_cid = str(channelcast_db.getDispersyCIDFromChannelId(channel_id))
        community = self.get_dispersy_instance().get_community(dispersy_cid)

        community._disp_create_torrent(
            torrent_def.infohash,
            long(time.time()),
            torrent_def.get_name_as_unicode(),
            tuple(torrent_def.get_files_with_length()),
            torrent_def.get_trackers_as_single_tuple(),
            forward=forward)

        if 'description' in extra_info:
            desc = extra_info['description'].strip()
            if desc != '':
                data = channelcast_db.getTorrentFromChannelId(channel_id, torrent_def.infohash, ['ChannelTorrents.id'])
                community.modifyTorrent(data, {'description': desc}, forward=forward)
Example #2
0
        def add_torrent_to_channel(self, tdef, extra_info=None):
            """
            Add a torrent to your channel.
            :param tdef: The torrent definition file of the torrent to add
            :param extra_info: Optional extra info to add to the torrent
            """
            if extra_info:
                tags = extra_info.get('description', '')
            else:
                # We only want to determine the type of the data. XXX filtering is done by the receiving side
                tags = default_category_filter.calculateCategory(tdef.metainfo, tdef.get_name_as_unicode())

            new_entry_dict = {
                "infohash": tdef.get_infohash(),
                "title": tdef.get_name_as_unicode()[:300],  # TODO: do proper size checking based on bytes
                "tags": tags[:200],  # TODO: do proper size checking based on bytes
                "size": tdef.get_length(),
                "torrent_date": datetime.fromtimestamp(tdef.get_creation_date()),
                "tracker_info": get_uniformed_tracker_url(tdef.get_tracker() or '') or '',
                "status": NEW}

            # See if the torrent is already in the channel
            old_torrent = self.get_torrent(tdef.get_infohash())
            if old_torrent:
                # If it is there, check if we were going to delete it
                if old_torrent.status == TODELETE:
                    if old_torrent.metadata_conflicting(new_entry_dict):
                        # Metadata from torrent we're trying to add is conflicting with the
                        # deleted old torrent's metadata. We will replace the old metadata.
                        new_timestamp = self._clock.tick()
                        old_torrent.set(timestamp=new_timestamp, **new_entry_dict)
                        old_torrent.sign()
                    else:
                        # No conflict. This means the user is trying to replace the deleted torrent
                        # with the same one. Just recover the old one.
                        old_torrent.status = COMMITTED
                    torrent_metadata = old_torrent
                else:
                    raise DuplicateTorrentFileError()
            else:
                torrent_metadata = db.TorrentMetadata.from_dict(new_entry_dict)
            return torrent_metadata