Skip to content

Commit

Permalink
Added endpoint to fetch subscribed channels
Browse files Browse the repository at this point in the history
  • Loading branch information
devos50 committed May 3, 2016
1 parent 89fcee3 commit fe7b2f3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
66 changes: 66 additions & 0 deletions Tribler/Core/Modules/restapi/channels_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import json

from twisted.web import resource
from twisted.web.resource import NoResource

from Tribler.Core.simpledefs import NTFY_CHANNELCAST


class BaseChannelsEndpoint(resource.Resource):
"""
This class contains some utility methods to work with raw channels from the database.
All endpoints that are using the database, should derive from this class.
"""

def convert_db_channel_to_json(self, channel):
return {"id": channel[0], "dispersy_cid": channel[1].encode('hex'), "name": channel[2], "description": channel[3],
"votes": channel[5], "torrents": channel[4], "spam": channel[6], "modified": channel[8],
"subscribed": (channel[7] == 2)}


class ChannelsEndpoint(BaseChannelsEndpoint):
"""
This endpoint is responsible for handing all requests regarding channels in Tribler.
"""

def __init__(self, session):
resource.Resource.__init__(self)
self.session = session

child_handler_dict = {"subscribed": ChannelsSubscribedEndpoint}
for path, child_cls in child_handler_dict.iteritems():
self.putChild(path, child_cls(self.session))


class ChannelsSubscribedEndpoint(BaseChannelsEndpoint):
"""
A GET request to this endpoint returns all the channels the user is subscribed to.
Example GET response:
{
"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,
}, ...]
}
"""

def __init__(self, session):
resource.Resource.__init__(self)
self.session = session
self.channel_db_handler = self.session.open_dbhandler(NTFY_CHANNELCAST)

def render_GET(self, request):
subscribed_channels_db = self.channel_db_handler.getMySubscribedChannels(includeDispsersy=True)
results_json = []
for channel in subscribed_channels_db:
results_json.append(self.convert_db_channel_to_json(channel))

return json.dumps({"subscribed": results_json})
5 changes: 5 additions & 0 deletions Tribler/Core/Modules/restapi/root_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from twisted.web import resource

from Tribler.Core.Modules.restapi.channels_endpoint import ChannelsEndpoint
from Tribler.Core.Modules.restapi.my_channel_endpoint import MyChannelEndpoint
from Tribler.Core.Modules.restapi.settings_endpoint import SettingsEndpoint
from Tribler.Core.Modules.restapi.variables_endpoint import VariablesEndpoint
Expand All @@ -14,6 +16,9 @@ def __init__(self, session):
resource.Resource.__init__(self)
self.session = session

self.channels_endpoint = ChannelsEndpoint(self.session)
self.putChild("channels", self.channels_endpoint)

self.my_channel_endpoint = MyChannelEndpoint(self.session)
self.putChild("mychannel", self.my_channel_endpoint)

Expand Down

0 comments on commit fe7b2f3

Please sign in to comment.