Skip to content

Commit

Permalink
Implemented endpoint to remove torrent from channel
Browse files Browse the repository at this point in the history
  • Loading branch information
devos50 committed Jun 30, 2016
1 parent 29b37ec commit 5f5b333
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
13 changes: 12 additions & 1 deletion Tribler/Core/Modules/restapi/channels/base_channels_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from twisted.web import http, resource
from Tribler.Core.simpledefs import NTFY_CHANNELCAST
from Tribler.community.allchannel.community import AllChannelCommunity

from Tribler.dispersy.exception import CommunityNotFoundException

UNKNOWN_CHANNEL_RESPONSE_MSG = "the channel with the provided cid is not known"
UNAUTHORIZED_RESPONSE_MSG = "you are not authorized to perform this request"
Expand Down Expand Up @@ -72,3 +72,14 @@ def vote_for_channel(self, cid, vote):
if isinstance(community, AllChannelCommunity):
community.disp_create_votecast(cid, vote, int(time.time()))
break

def get_community_for_channel_id(self, channel_id):
"""
Returns a Dispersy community from the given channel id. The Community object can be used to delete/add torrents
or modify playlists in a specific channel.
"""
dispersy_cid = str(self.channel_db_handler.getDispersyCIDFromChannelId(channel_id))
try:
return self.session.get_dispersy_instance().get_community(dispersy_cid)
except CommunityNotFoundException:
return None
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
from Tribler.Core.exceptions import DuplicateTorrentFileError


UNKNOWN_TORRENT_MSG = "this torrent is not found in the specified channel"
UNKNOWN_COMMUNITY_MSG = "the community for the specified channel cannot be found"


class ChannelsTorrentsEndpoint(BaseChannelsEndpoint):
"""
This class is responsible for managing requests regarding torrents in a channel.
Expand Down Expand Up @@ -182,3 +186,46 @@ def on_receive_magnet_meta_info(meta_info):
return BaseChannelsEndpoint.return_500(self, request, ex)

return json.dumps({"added": self.infohash})

def render_DELETE(self, request):
"""
.. http:delete:: /channels/discovered/(string: channelid)/torrents/(string: torrent infohash)
Remove a torrent with a given infohash from a given channel.
**Example request**:
.. sourcecode:: none
curl -X DELETE http://localhost:8085/channels/discovered/abcdefg/torrents/
97d2d8f5d37e56cfaeaae151d55f05b077074779
**Example response**:
.. sourcecode:: javascript
{
"removed": True
}
:statuscode 404: if the channel is not found or if the torrent is not found in the specified channel
"""
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.dispersy_id']
torrent_info = self.channel_db_handler.getTorrentFromChannelId(channel_info[0], self.infohash.decode('hex'),
torrent_db_columns)

if torrent_info is None:
return BaseChannelsEndpoint.return_404(request, message=UNKNOWN_TORRENT_MSG)

channel_community = self.get_community_for_channel_id(channel_info[0])
if channel_community is None:
return BaseChannelsEndpoint.return_404(request, message=UNKNOWN_COMMUNITY_MSG)

channel_community.remove_torrents([torrent_info[8]]) # the 8th index is the dispersy id of the channel torrent

return json.dumps({"removed": True})

0 comments on commit 5f5b333

Please sign in to comment.