Skip to content

Commit

Permalink
Add delete action for torrent in scraper functionalities
Browse files Browse the repository at this point in the history
Enhanced the torrent block feature to support a delete action. Updated HTML dropdown, backend handling, and Telegram notifications to reflect this change. Improved user feedback for block/delete operations and ensured schema validation.
  • Loading branch information
mhdzumair committed Feb 2, 2025
1 parent 4a353a3 commit 52c8a14
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
1 change: 1 addition & 0 deletions db/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ class KodiConfig(BaseModel):

class BlockTorrent(BaseModel):
info_hash: str
action: Literal["block", "delete"]
api_password: str


Expand Down
9 changes: 8 additions & 1 deletion resources/html/scraper.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ <h4 class="section-header">Scraper Selection</h4>
<option value="add_tv_metadata">Add TV Metadata</option>
<option value="add_m3u_playlist">Add M3U Playlist</option>
<option value="update_imdb_data">Update IMDb Data</option>
<option value="block_torrent">Block Torrent</option>
<option value="block_torrent">Block/Delete Torrent</option>
<option value="migrate_id">Migrate MediaFusion ID to IMDb ID</option>
<option value="update_images">Update Images</option>
</select>
Expand Down Expand Up @@ -478,6 +478,13 @@ <h4 class="section-header">TV Metadata Input</h4>
<input type="text" class="form-control" id="blockTorrentInfoHash" name="blockTorrentInfoHash" required
value="{% if prefill_data.info_hash %}{{ prefill_data.info_hash }}{% endif %}">
</div>
<div class="mb-3">
<label for="blockTorrentAction" class="form-label">Action</label>
<select class="form-select" id="blockTorrentAction" name="blockTorrentAction" required>
<option value="block">Block</option>
<option value="delete">Delete</option>
</select>
</div>
</div>
</div>

Expand Down
4 changes: 4 additions & 0 deletions resources/js/scraperControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1707,6 +1707,9 @@ async function handleBlockTorrent(apiPassword, submitBtn, loadingSpinner) {
return;
}

// get the action value from drop down blockTorrentAction
const action = document.getElementById('blockTorrentAction').value.trim();

try {
const response = await fetch('/scraper/block_torrent', {
method: 'POST',
Expand All @@ -1715,6 +1718,7 @@ async function handleBlockTorrent(apiPassword, submitBtn, loadingSpinner) {
},
body: JSON.stringify({
info_hash: infoHash,
action: action,
api_password: apiPassword
})
});
Expand Down
18 changes: 13 additions & 5 deletions scrapers/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,12 +625,17 @@ async def block_torrent(block_data: schemas.BlockTorrent):
validate_api_password(block_data.api_password)
torrent_stream = await TorrentStreams.get(block_data.info_hash)
if not torrent_stream:
raise HTTPException(status_code=404, detail="Torrent not found.")
if torrent_stream.is_blocked:
return {
"status": f"Torrent {block_data.info_hash} is already deleted / not found."
}
if block_data.action == "block" and torrent_stream.is_blocked:
return {"status": f"Torrent {block_data.info_hash} is already blocked."}
torrent_stream.is_blocked = True
try:
await torrent_stream.save()
if block_data.action == "delete":
await torrent_stream.delete()
else:
torrent_stream.is_blocked = True
await torrent_stream.save()
# Send Telegram notification
if settings.telegram_bot_token:
metadata = await MediaFusionMetaData.get_motor_collection().find_one(
Expand All @@ -641,6 +646,7 @@ async def block_torrent(block_data: schemas.BlockTorrent):
poster = f"{settings.poster_host_url}/poster/{meta_type}/{torrent_stream.meta_id}.jpg"
await telegram_notifier.send_block_notification(
info_hash=block_data.info_hash,
action=block_data.action,
meta_id=torrent_stream.meta_id,
title=title,
meta_type=meta_type,
Expand All @@ -653,7 +659,9 @@ async def block_torrent(block_data: schemas.BlockTorrent):
status_code=500, detail=f"Failed to block torrent: {str(e)}"
)

return {"status": f"Torrent {block_data.info_hash} has been successfully blocked."}
return {
"status": f"Torrent {block_data.info_hash} has been successfully {'blocked' if block_data.action == 'block' else 'deleted'}."
}


@router.post("/migrate_id", tags=["scraper"])
Expand Down
10 changes: 7 additions & 3 deletions utils/telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ async def send_contribution_notification(
f"*Torrent Name*: `{torrent_name}`\n"
f"*Info Hash*: `{info_hash}`\n"
f"*Type*: {torrent_type}\n"
f"*Poster*: [View]({poster})"
f"*Poster*: [View]({poster})\n"
f"*Stremio Links*:\n"
f" - *APP*: `stremio://detail/{meta_type}/{meta_id}/{meta_id}`\n"
f" - *WEB*: [View](https://web.stremio.com/#/detail/{meta_type}/{meta_id}/{meta_id})"
)

if catalogs:
Expand All @@ -81,13 +84,14 @@ async def send_contribution_notification(
message += "\n"

# Add block link
message += f"\n\n[🚫 Block Torrent]({block_url})"
message += f"\n\n[🚫 Block/Delete Torrent]({block_url})"

await self._send_photo_message(poster, message)

async def send_block_notification(
self,
info_hash: str,
action: str,
meta_id: str,
title: str,
meta_type: str,
Expand All @@ -106,7 +110,7 @@ async def send_block_notification(
)

message = (
f"🚫 Torrent Blocked\n\n"
f"🚫 Torrent {'Blocked' if action == 'block' else 'Deleted'}\n\n"
f"*Title*: {title}\n"
f"*Type*: {meta_type.title()}\n"
f"{meta_id_data}"
Expand Down

0 comments on commit 52c8a14

Please sign in to comment.