Esempio n. 1
0
    def render_GET(self, request):
        """
        .. http:get:: /torrents/random?limit=(int: max nr of torrents)

        A GET request to this endpoint returns random (channel) torrents.
        You can optionally specify a limit parameter to limit the maximum number of results. By default, this is 10.

            **Example request**:

            .. sourcecode:: none

                curl -X GET http://localhost:8085/torrents/random?limit=1

            **Example response**:

            .. sourcecode:: javascript

                {
                    "torrents": [{
                        "id": 4,
                        "infohash": "97d2d8f5d37e56cfaeaae151d55f05b077074779",
                        "name": "Ubuntu-16.04-desktop-amd64",
                        "size": 8592385,
                        "category": "other",
                        "num_seeders": 42,
                        "num_leechers": 184,
                        "last_tracker_check": 1463176959
                    }]
                }
        """
        limit_torrents = 10

        if 'limit' in request.args and len(request.args['limit']) > 0:
            limit_torrents = int(request.args['limit'][0])

            if limit_torrents <= 0:
                request.setResponseCode(http.BAD_REQUEST)
                return json.dumps(
                    {"error": "the limit parameter must be a positive number"})

        torrent_db_columns = [
            'Torrent.torrent_id', 'infohash', 'Torrent.name', 'length',
            'Torrent.category', 'num_seeders', 'num_leechers',
            'last_tracker_check', 'ChannelTorrents.inserted'
        ]

        popular_torrents = self.channel_db_handler.get_random_channel_torrents(
            torrent_db_columns, limit=limit_torrents)

        results_json = []
        for popular_torrent in popular_torrents:
            torrent_json = convert_db_torrent_to_json(popular_torrent)
            if (self.session.tribler_config.get_family_filter_enabled() and
                    self.session.lm.category.xxx_filter.isXXX(torrent_json['category'])) \
                    or torrent_json['name'] is None:
                continue

            results_json.append(torrent_json)

        return json.dumps({"torrents": results_json})
Esempio n. 2
0
    def render_GET(self, request):
        """
        .. http:get:: /channels/discovered/(string: channelid)/playlists

        Returns the playlists in your channel. Returns error 404 if you have not created a channel.

            **Example request**:

            .. sourcecode:: none

                curl -X GET http://localhost:8085/channels/discovered/abcd/playlists

            **Example response**:

            .. sourcecode:: javascript

                {
                    "playlists": [{
                        "id": 1,
                        "name": "My first playlist",
                        "description": "Funny movies",
                        "torrents": [{
                            "id": 4,
                            "infohash": "97d2d8f5d37e56cfaeaae151d55f05b077074779",
                            "name": "Ubuntu-16.04-desktop-amd64",
                            "size": 8592385,
                            "category": "other",
                            "num_seeders": 42,
                            "num_leechers": 184,
                            "last_tracker_check": 1463176959
                        }, ... ]
                    }, ...]
                }

            :statuscode 404: if you have not created a channel.
        """

        channel = self.get_channel_from_db(self.cid)
        if channel is None:
            return ChannelsPlaylistsEndpoint.return_404(request)

        playlists = []
        req_columns = ['Playlists.id', 'Playlists.name', 'Playlists.description']
        req_columns_torrents = ['Torrent.torrent_id', 'infohash', 'Torrent.name', 'length', 'Torrent.category',
                                'num_seeders', 'num_leechers', 'last_tracker_check', 'ChannelTorrents.inserted']
        for playlist in self.channel_db_handler.getPlaylistsFromChannelId(channel[0], req_columns):
            # Fetch torrents in the playlist
            playlist_torrents = self.channel_db_handler.getTorrentsFromPlaylist(playlist[0], req_columns_torrents)
            torrents = []
            for torrent_result in playlist_torrents:
                torrent = convert_db_torrent_to_json(torrent_result)
                if (self.session.tribler_config.get_family_filter_enabled() and torrent['category'] == 'xxx') or \
                        torrent['name'] is None:
                    continue
                torrents.append(torrent)

            playlists.append({"id": playlist[0], "name": playlist[1], "description": playlist[2], "torrents": torrents})

        return json.dumps({"playlists": playlists})
    def render_GET(self, request):
        """
        .. http:get:: /channels/discovered/(string: channelid)/torrents

        A GET request to this endpoint returns all discovered torrents in a specific channel. The size of the torrent is
        in number of bytes. The last_tracker_check value will be 0 if we did not check the tracker state of the torrent
        yet. Optionally, we can disable the family filter for this particular request by passing the following flag:
        - disable_filter: whether the family filter should be disabled for this request (1 = disabled)

            **Example request**:

            .. sourcecode:: none

                curl -X GET http://localhost:8085/channels/discovered/da69aaad39ccf468aba2ab9177d5f8d8160135e6/torrents

            **Example response**:

            .. sourcecode:: javascript

                {
                    "torrents": [{
                        "id": 4,
                        "infohash": "97d2d8f5d37e56cfaeaae151d55f05b077074779",
                        "name": "Ubuntu-16.04-desktop-amd64",
                        "size": 8592385,
                        "category": "other",
                        "num_seeders": 42,
                        "num_leechers": 184,
                        "last_tracker_check": 1463176959
                    }, ...]
                }

            :statuscode 404: if the specified channel cannot be found.
        """
        channel_info = self.get_channel_from_db(self.cid)
        if channel_info is None:
            return ChannelsTorrentsEndpoint.return_404(request)

        torrent_db_columns = ['Torrent.torrent_id', 'infohash', 'Torrent.name', 'length', 'Torrent.category',
                              'num_seeders', 'num_leechers', 'last_tracker_check', 'ChannelTorrents.inserted']
        results_local_torrents_channel = self.channel_db_handler\
            .getTorrentsFromChannelId(channel_info[0], True, torrent_db_columns)

        should_filter = self.session.config.get_family_filter_enabled()
        if 'disable_filter' in request.args and len(request.args['disable_filter']) > 0 \
                and request.args['disable_filter'][0] == "1":
            should_filter = False

        results_json = []
        for torrent_result in results_local_torrents_channel:
            torrent_json = convert_db_torrent_to_json(torrent_result)
            if torrent_json['name'] is None or (should_filter and torrent_json['category'] == 'xxx'):
                continue

            results_json.append(torrent_json)

        return json.dumps({"torrents": results_json})
    def render_GET(self, request):
        """
        .. http:get:: /channels/discovered/(string: channelid)/torrents

        A GET request to this endpoint returns all discovered torrents in a specific channel. The size of the torrent is
        in number of bytes. The last_tracker_check value will be 0 if we did not check the tracker state of the torrent
        yet. Optionally, we can disable the family filter for this particular request by passing the following flag:
        - disable_filter: whether the family filter should be disabled for this request (1 = disabled)

            **Example request**:

            .. sourcecode:: none

                curl -X GET http://localhost:8085/channels/discovered/da69aaad39ccf468aba2ab9177d5f8d8160135e6/torrents

            **Example response**:

            .. sourcecode:: javascript

                {
                    "torrents": [{
                        "id": 4,
                        "infohash": "97d2d8f5d37e56cfaeaae151d55f05b077074779",
                        "name": "Ubuntu-16.04-desktop-amd64",
                        "size": 8592385,
                        "category": "other",
                        "num_seeders": 42,
                        "num_leechers": 184,
                        "last_tracker_check": 1463176959
                    }, ...]
                }

            :statuscode 404: if the specified channel cannot be found.
        """
        chant_dirty = False
        if self.is_chant_channel:
            with db_session:
                channel = self.session.lm.mds.ChannelMetadata.get(public_key=self.cid)
                if channel:
                    if channel == self.session.lm.mds.get_my_channel():
                        # That's our channel, it gets special treatment
                        uncommitted = [convert_torrent_metadata_to_tuple(x, UNCOMMITTED) for x in
                                       list(channel.uncommitted_contents)]
                        deleted = [convert_torrent_metadata_to_tuple(x, TODELETE) for x in
                                   list(channel.deleted_contents)]
                        committed = [convert_torrent_metadata_to_tuple(x, COMMITTED) for x in
                                     list(channel.committed_contents)]
                        results_local_torrents_channel = uncommitted + deleted + committed
                        chant_dirty = bool(uncommitted + deleted)
                    else:
                        results_local_torrents_channel = map(convert_torrent_metadata_to_tuple,
                                                             list(channel.contents))
                else:
                    return ChannelsTorrentsEndpoint.return_404(request)
        else:
            channel_info = self.get_channel_from_db(self.cid)
            if channel_info is None:
                return ChannelsTorrentsEndpoint.return_404(request)

            torrent_db_columns = ['Torrent.torrent_id', 'infohash', 'Torrent.name', 'length', 'Torrent.category',
                                  'num_seeders', 'num_leechers', 'last_tracker_check', 'ChannelTorrents.inserted']
            results_local_torrents_channel = self.channel_db_handler\
                .getTorrentsFromChannelId(channel_info[0], True, torrent_db_columns)

        should_filter = self.session.config.get_family_filter_enabled()
        if 'disable_filter' in request.args and len(request.args['disable_filter']) > 0 \
                and request.args['disable_filter'][0] == "1":
            should_filter = False

        results_json = []
        for torrent_result in results_local_torrents_channel:
            torrent_json = convert_db_torrent_to_json(torrent_result)
            if torrent_json['name'] is None or (should_filter and torrent_json['category'] == 'xxx'):
                continue

            results_json.append(torrent_json)

        return json.dumps({"torrents": results_json, "chant_dirty": chant_dirty})
    def render_GET(self, request):
        """
        .. http:get:: /channels/discovered/(string: channelid)/playlists

        Returns the playlists in your channel. Returns error 404 if you have not created a channel.
        - disable_filter: whether the family filter should be disabled for this request (1 = disabled)

            **Example request**:

            .. sourcecode:: none

                curl -X GET http://localhost:8085/channels/discovered/abcd/playlists

            **Example response**:

            .. sourcecode:: javascript

                {
                    "playlists": [{
                        "id": 1,
                        "name": "My first playlist",
                        "description": "Funny movies",
                        "torrents": [{
                            "id": 4,
                            "infohash": "97d2d8f5d37e56cfaeaae151d55f05b077074779",
                            "name": "Ubuntu-16.04-desktop-amd64",
                            "size": 8592385,
                            "category": "other",
                            "num_seeders": 42,
                            "num_leechers": 184,
                            "last_tracker_check": 1463176959
                        }, ... ]
                    }, ...]
                }

            :statuscode 404: if you have not created a channel.
        """

        channel = self.get_channel_from_db(self.cid)
        if channel is None:
            return ChannelsPlaylistsEndpoint.return_404(request)

        playlists = []
        req_columns = ['Playlists.id', 'Playlists.name', 'Playlists.description']
        req_columns_torrents = ['Torrent.torrent_id', 'infohash', 'Torrent.name', 'length', 'Torrent.category',
                                'num_seeders', 'num_leechers', 'last_tracker_check', 'ChannelTorrents.inserted']

        should_filter = self.session.config.get_family_filter_enabled()
        if 'disable_filter' in request.args and len(request.args['disable_filter']) > 0 \
                and request.args['disable_filter'][0] == "1":
            should_filter = False

        for playlist in self.channel_db_handler.getPlaylistsFromChannelId(channel[0], req_columns):
            # Fetch torrents in the playlist
            playlist_torrents = self.channel_db_handler.getTorrentsFromPlaylist(playlist[0], req_columns_torrents)
            torrents = []
            for torrent_result in playlist_torrents:
                torrent = convert_db_torrent_to_json(torrent_result)
                if (should_filter and torrent['category'] == 'xxx') or torrent['name'] is None:
                    continue
                torrents.append(torrent)

            playlists.append({"id": playlist[0], "name": playlist[1], "description": playlist[2], "torrents": torrents})

        return json.dumps({"playlists": playlists})