Skip to content

Commit

Permalink
Merge pull request #2207 from devos50/my_channel_playlists_endpoint
Browse files Browse the repository at this point in the history
Implemented endpoint to fetch playlists from your channel
  • Loading branch information
whirm committed May 17, 2016
2 parents 30035c5 + 2fa8dfc commit d7cc3d8
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 4 deletions.
45 changes: 44 additions & 1 deletion Tribler/Core/Modules/restapi/my_channel_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from twisted.web import http, resource
from Tribler.Core.CacheDB.sqlitecachedb import str2bin

from Tribler.Core.simpledefs import NTFY_CHANNELCAST

Expand Down Expand Up @@ -38,7 +39,7 @@ class MyChannelEndpoint(MyChannelBaseEndpoint):
def __init__(self, session):
MyChannelBaseEndpoint.__init__(self, session)
child_handler_dict = {"overview": MyChannelOverviewEndpoint, "torrents": MyChannelTorrentsEndpoint,
"rssfeeds": MyChannelRssFeedsEndpoint}
"rssfeeds": MyChannelRssFeedsEndpoint, "playlists": MyChannelPlaylistsEndpoint}
for path, child_cls in child_handler_dict.iteritems():
self.putChild(path, child_cls(self.session))

Expand Down Expand Up @@ -170,3 +171,45 @@ def render_DELETE(self, request):

channel_obj.remove_rss_feed(self.feed_url)
return json.dumps({"removed": True})


class MyChannelPlaylistsEndpoint(MyChannelBaseEndpoint):
"""
This class is responsible for handling requests regarding playlists in your channel.
"""

def render_GET(self, request):
"""
Returns the playlists in your channel. Returns error 404 if you have not created a channel.
Example response:
{
"playlists": [{
"id": 1,
"name": "My first playlist",
"description": "Funny movies",
"torrents": [{
"name": "movie_1",
"infohash": "e940a7a57294e4c98f62514b32611e38181b6cae"
}, ... ]
}, ...]
}
"""
request.setHeader('Content-Type', 'text/json')

my_channel_id = self.channel_db_handler.getMyChannelId()
if my_channel_id is None:
return MyChannelBaseEndpoint.return_404(request)

playlists = []
req_columns = ['Playlists.id', 'Playlists.name', 'Playlists.description']
req_columns_torrents = ['ChannelTorrents.name', 'Torrent.infohash']
for playlist in self.channel_db_handler.getPlaylistsFromChannelId(my_channel_id, req_columns):
# Fetch torrents in the playlist
torrents = []
for torrent in self.channel_db_handler.getTorrentsFromPlaylist(playlist[0], req_columns_torrents):
torrents.append({"name": torrent[0], "infohash": str2bin(torrent[1]).encode('hex')})

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

return json.dumps({"playlists": playlists})
47 changes: 44 additions & 3 deletions Tribler/Test/Core/Modules/RestApi/test_my_channel_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ def create_fake_channel(self, channel_name, channel_description):
self.session.lm.channel_manager._channel_list.append(channel_obj)
return my_channel_id

def insert_torrents_into_my_channel(self, torrent_list):
self.channel_db_handler.on_torrents_from_dispersy(torrent_list)


class TestMyChannelEndpoints(AbstractTestMyChannelEndpoints):

Expand All @@ -55,9 +58,6 @@ def test_my_channel_unknown_endpoint(self):
self.should_check_equality = False
return self.do_request('mychannel/thisendpointdoesnotexist123', expected_code=404)

def insert_torrents_into_my_channel(self, torrent_list):
self.channel_db_handler.on_torrents_from_dispersy(torrent_list)

@deferred(timeout=10)
def test_my_channel_overview_endpoint_no_my_channel(self):
"""
Expand Down Expand Up @@ -196,3 +196,44 @@ def verify_rss_removed(_):

return self.do_request('mychannel/rssfeeds/http%3A%2F%2Frssfeed.com%2Frss.xml', expected_code=200,
expected_json=expected_json, request_type='DELETE').addCallback(verify_rss_removed)


class TestMyChannelPlaylistEndpoints(AbstractTestMyChannelEndpoints):

def create_playlist(self, channel_id, dispersy_id, peer_id, name, description):
self.channel_db_handler.on_playlist_from_dispersy(channel_id, dispersy_id, peer_id, name, description)

def insert_torrent_into_playlist(self, playlist_disp_id, infohash):
self.channel_db_handler.on_playlist_torrent(42, playlist_disp_id, 42, infohash)

@deferred(timeout=10)
def test_get_playlists_endpoint_without_channel(self):
"""
Testing whether the API returns error 404 if no channel has been created when fetching playlists
"""
self.should_check_equality = False
return self.do_request('mychannel/playlists', expected_code=404)

@deferred(timeout=10)
def test_playlists_endpoint_no_playlists(self):
"""
Testing whether the API returns the right JSON data if no playlists have been added to your channel
"""
self.create_my_channel("my channel", "this is a short description")
return self.do_request('mychannel/playlists', expected_code=200, expected_json={"playlists": []})

@deferred(timeout=10)
def test_playlists_endpoint(self):
"""
Testing whether the API returns the right JSON data if playlists are fetched
"""
my_channel_id = self.create_my_channel("my channel", "this is a short description")
self.create_playlist(my_channel_id, 1234, 42, "test playlist", "test description")
torrent_list = [[my_channel_id, 1, 1, 'a' * 20, 1460000000, "ubuntu-torrent.iso", [], []]]
self.insert_torrents_into_my_channel(torrent_list)
self.insert_torrent_into_playlist(1234, 'a' * 20)

expected_json = {u"playlists": [{u"id": 1, u"name": u"test playlist", u"description": u"test description",
u"torrents": [{u"infohash": bytes(('a' * 20).encode('hex')),
u"name": u"ubuntu-torrent.iso"}]}]}
return self.do_request('mychannel/playlists', expected_code=200, expected_json=expected_json)
21 changes: 21 additions & 0 deletions doc/Tribler REST API.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ curl -X PUT http://localhost:8085/mychannel/rssfeeds/http%3A%2F%2Frssfeed.com%2F
| GET /mychannel/rssfeeds | Get a list of rss feeds used by your channel |
| PUT /mychannel/rssfeeds/{feedurl} | Add a rss feed to your channel |
| DELETE /mychannel/rssfeeds/{feedurl} | Remove a rss feed from your channel |
| GET /mychannel/playlists | Get a list of playlists in your channel |

### Search

Expand Down Expand Up @@ -179,6 +180,26 @@ Add a RSS feed to your channel. Returns error 409 (Conflict) if the supplied RSS

Delete a RSS feed from your channel. Returns error 404 if the RSS feed that is being removed does not exist. Note that the rss feed url should be URL-encoded.

## `GET /mychannel/playlists`

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

### Example response

```json
{
"playlists": [{
"id": 1,
"name": "My first playlist",
"description": "Funny movies",
"torrents": [{
"name": "movie_1",
"infohash": "e940a7a57294e4c98f62514b32611e38181b6cae"
}, ... ]
}, ...]
}
```

## `GET /search`

Search for channels and torrents present in the local Tribler database according to a query. The query is passed using the url, i.e. /search?q=pioneer and results are pushed over the events endpoint.
Expand Down

0 comments on commit d7cc3d8

Please sign in to comment.