Skip to content

Commit

Permalink
Merge pull request #2157 from devos50/rss_feeds_endpoint
Browse files Browse the repository at this point in the history
Implemented the RSS feeds endpoint
  • Loading branch information
whirm committed May 3, 2016
2 parents 89fcee3 + 6f707ee commit b96c597
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 9 deletions.
43 changes: 34 additions & 9 deletions 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, server, resource
from twisted.web import http, resource

from Tribler.Core.simpledefs import NTFY_CHANNELCAST


Expand All @@ -24,16 +25,15 @@ def return_404(request):

class MyChannelEndpoint(MyChannelBaseEndpoint):
"""
This endpoint is reponsible for handing all requests regarding your channel such as getting and updating
This endpoint is responsible for handing all requests regarding your channel such as getting and updating
torrents, playlists and rss-feeds.
"""

def getChild(self, path, request):
child_handler_dict = {"overview": MyChannelOverviewEndpoint, "torrents": MyChannelTorrentsEndpoint}
if path not in child_handler_dict:
return None

return child_handler_dict[path](self.session)
def __init__(self, session):
MyChannelBaseEndpoint.__init__(self, session)
child_handler_dict = {"overview": MyChannelOverviewEndpoint, "torrents": MyChannelTorrentsEndpoint,
"rssfeeds": MyChannelRssFeedsEndpoint}
for path, child_cls in child_handler_dict.iteritems():
self.putChild(path, child_cls(self.session))


class MyChannelOverviewEndpoint(MyChannelBaseEndpoint):
Expand Down Expand Up @@ -90,3 +90,28 @@ def render_GET(self, request):
for torrent in torrents:
torrent_list.append({'name': torrent[0], 'infohash': torrent[1].encode('hex'), 'added': torrent[2]})
return json.dumps({'torrents': torrent_list})


class MyChannelRssFeedsEndpoint(MyChannelBaseEndpoint):
"""
Return the RSS feeds in your channel.
Example response:
{
"rssfeeds": [{
"url": "http://rssprovider.com/feed.xml",
}, ...]
}
"""

def render_GET(self, request):
my_channel_id = self.channel_db_handler.getMyChannelId()
channel_obj = self.session.lm.channel_manager.get_my_channel(my_channel_id)
if my_channel_id is None or channel_obj is None:
return MyChannelBaseEndpoint.return_404(request)

rss_list = channel_obj.get_rss_feed_url_list()
request.setHeader('Content-Type', 'text/json')
feeds_list = [{'url': rss_item} for rss_item in rss_list]

return json.dumps({"rssfeeds": feeds_list})
52 changes: 52 additions & 0 deletions Tribler/Test/Core/Modules/RestApi/test_my_channel_endpoints.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
import json

from Tribler.Core.Modules.channel.channel import ChannelObject
from Tribler.Core.Modules.channel.channel_manager import ChannelManager
from Tribler.Core.Utilities.twisted_thread import deferred
from Tribler.Core.simpledefs import NTFY_CHANNELCAST
from Tribler.Test.Core.Modules.RestApi.base_api_test import AbstractApiTest


class ChannelCommunityMock(object):

def __init__(self, channel_id):
self.name = "Channel name"
self.cid = 'a' * 20
self.channel_id = channel_id

def get_channel_name(self):
return self.name

def get_channel_id(self):
return self.channel_id

class TestMyChannelEndpoints(AbstractApiTest):

def setUp(self, autoload_discovery=True):
super(TestMyChannelEndpoints, self).setUp(autoload_discovery)
self.channel_db_handler = self.session.open_dbhandler(NTFY_CHANNELCAST)

@deferred(timeout=10)
def test_my_channel_unknown_endpoint(self):
"""
Testing whether the API returns an error if an unknown endpoint is queried
"""
self.should_check_equality = False
return self.do_request('mychannel/thisendpointdoesnotexist123', expected_code=404)

def create_my_channel(self, name, description):
"""
Utility method to create your channel
Expand Down Expand Up @@ -62,3 +86,31 @@ def test_torrents_endpoint_with_channel(self):
self.insert_torrents_into_my_channel(torrent_list)

return self.do_request('mychannel/torrents', expected_code=200).addCallback(self.verify_torrents_json)

@deferred(timeout=10)
def test_rss_feeds_endpoint_no_my_channel(self):
"""
Testing whether the API returns the right JSON data if no channel has been created when fetching rss feeds
"""
self.session.lm.channel_manager = ChannelManager(self.session)
return self.do_request('mychannel/rssfeeds', expected_code=404)

@deferred(timeout=10)
def test_rss_feeds_endpoint_with_channel(self):
"""
Testing whether the API returns the right JSON data if a rss feeds from a channel are fetched
"""
expected_json = {u'rssfeeds': [{u'url': u'http://test1.com/feed.xml'}, {u'url': u'http://test2.com/feed.xml'}]}

# Use a fake ChannelCommunity object (we don't actually want to create a Dispersy community)
my_channel_id = self.create_my_channel("my channel", "this is a short description")
self.session.lm.channel_manager = ChannelManager(self.session)

channel_obj = ChannelObject(self.session, ChannelCommunityMock(my_channel_id))
self.session.lm.channel_manager._channel_list.append(channel_obj)

channel_obj = self.session.lm.channel_manager.get_channel("Channel name")
for rss_item in expected_json[u'rssfeeds']:
channel_obj.create_rss_feed(rss_item[u'url'])

return self.do_request('mychannel/rssfeeds', expected_code=200, expected_json=expected_json)
15 changes: 15 additions & 0 deletions doc/Tribler REST API.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The API has been built using [Twisted Web](http://twistedmatrix.com/trac/wiki/Tw
| ---- | --------------- |
| GET /mychannel/overview | Get the name, description and identifier of your channel |
| GET /mychannel/torrents | Get a list of torrents in your channel |
| GET /mychannel/rssfeeds | Get a list of rss feeds used by your channel |

### Settings

Expand Down Expand Up @@ -59,6 +60,20 @@ Returns a list of torrents in your channel. Each torrent item in the list contai
}
```

## `GET /mychannel/rssfeeds`

Returns a list of rss feeds in your channel. Each rss feed items contains the URL of the feed.

### Example response

```json
{
"rssfeeds": [{
"url": "http://rssprovider.com/feed.xml",
}, ...]
}
```

## `GET /settings`

Returns a dictionary with the settings that the current Tribler session is using. Note that the response below is not the complete settings dictionary returned since that would be too large to display here.
Expand Down

0 comments on commit b96c597

Please sign in to comment.