def test_convert_db_channel_to_json(self): """ Test whether the conversion from a db channel tuple to json works """ input_tuple = (1, 'aaaa'.decode('hex'), 'test', 'desc', 42, 43, 44, 2, 1234, 0.123) output = {'id': 1, 'dispersy_cid': 'aaaa', 'name': 'test', 'description': 'desc', 'torrents': 42, 'votes': 43, 'spam': 44, 'subscribed': True, 'modified': 1234, 'relevance_score': 0.123} self.assertEqual(convert_db_channel_to_json(input_tuple, include_rel_score=True), output)
def test_convert_db_channel_to_json(self): """ Test whether the conversion from a db channel tuple to json works """ input_tuple = (1, 'aaaa'.decode('hex'), 'test', 'desc', 42, 43, 44, 2, 1234, 0.123) output = {'id': 1, 'dispersy_cid': 'aaaa', 'name': 'test', 'description': 'desc', 'torrents': 42, 'votes': 43, 'spam': 44, 'subscribed': True, 'modified': 1234, 'relevance_score': 0.123} self.assertEqual(convert_db_channel_to_json(input_tuple, include_rel_score=True), output)
def render_GET(self, request): """ .. http:get:: /channels/popular?limit=(int:max nr of channels) A GET request to this endpoint will return the most popular discovered channels in Tribler. You can optionally pass a limit parameter to limit the number of results. **Example request**: .. sourcecode:: none curl -X GET http://localhost:8085/channels/popular?limit=1 **Example response**: .. sourcecode:: javascript { "channels": [{ "id": 3, "dispersy_cid": "da69aaad39ccf468aba2ab9177d5f8d8160135e6", "name": "My fancy channel", "description": "A description of this fancy channel", "subscribed": False, "votes": 23, "torrents": 3, "spam": 5, "modified": 14598395, "can_edit": True, }] } """ limit_channels = 10 if 'limit' in request.args and len(request.args['limit']) > 0: limit_channels = int(request.args['limit'][0]) if limit_channels <= 0: request.setResponseCode(http.BAD_REQUEST) return json.dumps( {"error": "the limit parameter must be a positive number"}) popular_channels = self.channel_db_handler.getMostPopularChannels( max_nr=limit_channels) results_json = [] for channel in popular_channels: channel_json = convert_db_channel_to_json(channel) if self.session.config.get_family_filter_enabled() and \ self.session.lm.category.xxx_filter.isXXX(channel_json['name']): continue results_json.append(channel_json) return json.dumps({"channels": results_json})
def render_GET(self, _): """ .. http:get:: /channels/discovered A GET request to this endpoint returns all channels discovered in Tribler. **Example request**: .. sourcecode:: none curl -X GET http://localhost:8085/channels/discovered **Example response**: .. sourcecode:: javascript { "channels": [{ "id": 3, "dispersy_cid": "da69aaad39ccf468aba2ab9177d5f8d8160135e6", "name": "My fancy channel", "description": "A description of this fancy channel", "subscribed": False, "votes": 23, "torrents": 3, "spam": 5, "modified": 14598395, "can_edit": True }, ...] } """ all_channels_db = self.channel_db_handler.getAllChannels() if self.session.config.get_chant_enabled(): chant_channels = list(self.session.lm.mds.ChannelMetadata.select()) for chant_channel in chant_channels: all_channels_db.append( convert_channel_metadata_to_tuple(chant_channel)) results_json = [] for channel in all_channels_db: channel_json = convert_db_channel_to_json(channel) if self.session.config.get_family_filter_enabled() and \ self.session.lm.category.xxx_filter.isXXX(channel_json['name']): continue results_json.append(channel_json) return json.dumps({"channels": results_json})
def render_GET(self, _): """ .. http:get:: /channels/discovered A GET request to this endpoint returns all channels discovered in Tribler. **Example request**: .. sourcecode:: none curl -X GET http://localhost:8085/channels/discovered **Example response**: .. sourcecode:: javascript { "channels": [{ "id": 3, "dispersy_cid": "da69aaad39ccf468aba2ab9177d5f8d8160135e6", "name": "My fancy channel", "description": "A description of this fancy channel", "subscribed": False, "votes": 23, "torrents": 3, "spam": 5, "modified": 14598395, "can_edit": True }, ...] } """ all_channels_db = self.channel_db_handler.getAllChannels() if self.session.config.get_chant_enabled(): chant_channels = list(self.session.lm.mds.ChannelMetadata.select()) for chant_channel in chant_channels: all_channels_db.append(convert_channel_metadata_to_tuple(chant_channel)) results_json = [] for channel in all_channels_db: channel_json = convert_db_channel_to_json(channel) if self.session.config.get_family_filter_enabled() and \ self.session.lm.category.xxx_filter.isXXX(channel_json['name']): continue results_json.append(channel_json) return json.dumps({"channels": results_json})
def on_search_results_channels(self, subject, changetype, objectID, results): """ Returns the channel search results over the events endpoint. """ query = ' '.join(results['keywords']) for channel in results['result_list']: channel_json = convert_db_channel_to_json(channel, include_rel_score=True) if self.session.config.get_family_filter_enabled() and \ self.session.lm.category.xxx_filter.isXXX(channel_json['name']): continue if channel_json['dispersy_cid'] not in self.channel_cids_sent: self.write_data({"type": "search_result_channel", "event": {"query": query, "result": channel_json}}) self.channel_cids_sent.add(channel_json['dispersy_cid'])
def render_GET(self, _): """ .. http:get:: /channels/subscribed Returns all the channels the user is subscribed to. **Example request**: .. sourcecode:: none curl -X GET http://localhost:8085/channels/subscribed **Example response**: .. sourcecode:: javascript { "subscribed": [{ "id": 3, "dispersy_cid": "da69aaad39ccf468aba2ab9177d5f8d8160135e6", "name": "My fancy channel", "description": "A description of this fancy channel", "subscribed": True, "votes": 23, "torrents": 3, "spam": 5, "modified": 14598395, "can_edit": True, }, ...] } """ subscribed_channels_db = self.channel_db_handler.getMySubscribedChannels( include_dispersy=True) results_json = [ convert_db_channel_to_json(channel) for channel in subscribed_channels_db ] if self.session.config.get_chant_enabled(): with db_session: channels_list = list( self.session.lm.mds.ChannelMetadata.select( lambda g: g.subscribed)) results_json.extend([ convert_chant_channel_to_json(channel) for channel in channels_list ]) return json.dumps({"subscribed": results_json})
def render_GET(self, _): """ .. http:get:: /channels/subscribed Returns all the channels the user is subscribed to. **Example request**: .. sourcecode:: none curl -X GET http://localhost:8085/channels/subscribed **Example response**: .. sourcecode:: javascript { "subscribed": [{ "id": 3, "dispersy_cid": "da69aaad39ccf468aba2ab9177d5f8d8160135e6", "name": "My fancy channel", "description": "A description of this fancy channel", "subscribed": True, "votes": 23, "torrents": 3, "spam": 5, "modified": 14598395, "can_edit": True, }, ...] } """ subscribed_channels_db = self.channel_db_handler.getMySubscribedChannels(include_dispersy=True) results_json = [convert_db_channel_to_json(channel) for channel in subscribed_channels_db] if self.session.config.get_chant_enabled(): with db_session: channels_list = list(self.session.lm.mds.ChannelMetadata.select(lambda g: g.subscribed)) results_json.extend([convert_chant_channel_to_json(channel) for channel in channels_list]) return json.dumps({"subscribed": results_json})