add ability to export iptv channels

This commit is contained in:
2024-11-24 16:09:03 +11:00
parent ad98c05c2f
commit ecbbc1da20
5 changed files with 390 additions and 31 deletions
BIN
View File
Binary file not shown.
+3
View File
@@ -4,11 +4,14 @@ from .iptv_views import (
IPTVChannelCreateView,
IPTVChannelUpdateView,
IPTVChannelDeleteView,
export_m3u_playlist,
)
urlpatterns = [
# UI routes
path('ui/iptv/', IPTVChannelListView.as_view(), name='iptv-list'),
path('ui/iptv/create/', IPTVChannelCreateView.as_view(), name='iptv-create'),
path('ui/iptv/<int:pk>/edit/', IPTVChannelUpdateView.as_view(), name='iptv-update'),
path('ui/iptv/<int:pk>/delete/', IPTVChannelDeleteView.as_view(), name='iptv-delete'),
path('api/iptv/export/', export_m3u_playlist, name='iptv-export'),
]
+23
View File
@@ -1,6 +1,7 @@
from django.urls import reverse_lazy
from django.views.generic import ListView, CreateView, UpdateView, DeleteView
from django.shortcuts import render
from django.http import HttpResponse
from .models import IPTVChannel
from .forms import IPTVChannelForm
@@ -31,3 +32,25 @@ class IPTVChannelDeleteView(DeleteView):
model = IPTVChannel
template_name = 'links/iptv/confirm_delete.html'
success_url = reverse_lazy('iptv-list')
def export_m3u_playlist(request):
"""Export all channels as M3U playlist"""
channels = IPTVChannel.objects.all()
# Start the M3U file content
content = ['#EXTM3U']
for channel in channels:
# Add channel info line
content.append(f'#EXTINF:-1 tvg-id="{channel.id}",{channel.title}')
# Add URL line
content.append(channel.url)
# Join all lines with newlines
playlist_content = '\n'.join(content)
# Create the response with appropriate headers
response = HttpResponse(playlist_content, content_type='audio/x-mpegurl')
response['Content-Disposition'] = 'attachment; filename="iptv_channels.m3u"'
return response
+234 -31
View File
@@ -2,43 +2,51 @@
{% load static %}
{% block content %}
<div class="flex h-[calc(100vh-4rem)]">
<!-- Left sidebar -->
<div class="w-80 border-r border-gray-200 bg-white overflow-y-auto">
<div class="p-4 border-b border-gray-200 flex justify-between items-center">
<h2 class="text-lg font-medium text-gray-900">IPTV Channels</h2>
<a href="{% url 'iptv-create' %}" class="inline-flex items-center px-3 py-1.5 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
<div class="flex flex-col h-screen md:h-[calc(100vh-4rem)] md:flex-row">
<!-- Sidebar - full width on mobile, w-80 on desktop -->
<div class="w-full md:w-80 border-b md:border-r border-gray-200 bg-white overflow-y-auto md:h-full">
<div class="p-4 border-b border-gray-200 flex justify-between items-center sticky top-0 bg-white z-10">
<div class="flex items-center space-x-4">
<h2 class="text-lg font-medium text-gray-900">IPTV Channels</h2>
<a href="{% url 'iptv-export' %}" class="text-gray-400 hover:text-gray-600" title="Export M3U Playlist">
<svg class="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"/>
</svg>
</a>
</div>
<button onclick="showAddModal()" class="inline-flex items-center px-4 py-2 border border-transparent text-base font-medium rounded-md shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
Add Channel
</a>
</button>
</div>
<nav class="flex-1 min-h-0 overflow-y-auto">
<div class="px-2 py-2 space-y-1">
<div class="p-2 space-y-2">
{% for channel in channels %}
<div class="channel-item p-3 rounded-md hover:bg-gray-50 {% if channel.id == active_channel.id %}bg-gray-100{% endif %}"
<div class="channel-item p-4 rounded-md hover:bg-gray-50 {% if channel.id == active_channel.id %}bg-gray-100{% endif %} touch-manipulation"
data-url="{{ channel.url }}"
data-title="{{ channel.title }}"
data-id="{{ channel.id }}"
onclick="selectChannel('{{ channel.url }}', '{{ channel.title }}', this)">
<div class="flex justify-between items-start">
<div>
<h3 class="text-sm font-medium text-gray-900">{{ channel.title }}</h3>
<p class="text-sm text-gray-500">{{ channel.description|truncatechars:50 }}</p>
<div class="flex justify-between items-start gap-4">
<div class="min-w-0 flex-1">
<h3 class="text-base font-medium text-gray-900 truncate">{{ channel.title }}</h3>
<p class="text-sm text-gray-500 mt-1">{{ channel.description|truncatechars:50 }}</p>
</div>
<div class="flex space-x-2">
<a href="{% url 'iptv-update' channel.id %}" class="text-gray-400 hover:text-gray-500">
<svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<div class="flex space-x-3">
<button onclick="event.stopPropagation(); showEditModal('{{ channel.id }}', '{{ channel.title }}', '{{ channel.url }}', '{{ channel.description }}')" class="p-2 text-gray-400 hover:text-gray-500">
<svg class="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
</svg>
</a>
<a href="{% url 'iptv-delete' channel.id %}" class="text-gray-400 hover:text-gray-500">
<svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
</button>
<button onclick="event.stopPropagation(); showDeleteModal('{{ channel.id }}', '{{ channel.title }}')" class="p-2 text-gray-400 hover:text-gray-500">
<svg class="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
</svg>
</a>
</button>
</div>
</div>
</div>
{% empty %}
<div class="text-center py-4">
<div class="text-center py-6">
<p class="text-sm text-gray-500">No channels added yet.</p>
</div>
{% endfor %}
@@ -46,30 +54,102 @@
</nav>
</div>
<!-- Main content -->
<div class="flex-1 flex flex-col bg-gray-50">
<div class="flex-1 flex items-center justify-center relative" id="player-container">
<div class="text-center" id="empty-state">
<!-- Main content - full height on mobile -->
<div class="flex-1 flex flex-col bg-gray-50 min-h-[50vh] md:min-h-0">
<div class="flex-1 flex items-center justify-center relative w-full h-full" id="player-container">
<div class="text-center p-4" id="empty-state">
<svg class="mx-auto h-12 w-12 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 10l4.553-2.276A1 1 0 0121 8.618v6.764a1 1 0 01-1.447.894L15 14M5 18h8a2 2 0 002-2V8a2 2 0 00-2-2H5a2 2 0 00-2 2v8a2 2 0 002 2z"/>
</svg>
<h3 class="mt-2 text-sm font-medium text-gray-900">No channel selected</h3>
<h3 class="mt-2 text-base font-medium text-gray-900">No channel selected</h3>
<p class="mt-1 text-sm text-gray-500">Select a channel from the list to start watching</p>
</div>
<!-- Play overlay -->
<div id="play-overlay" class="hidden absolute inset-0 bg-black bg-opacity-50 flex items-center justify-center cursor-pointer">
<div class="text-center text-white">
<button onclick="playVideo()" class="bg-white bg-opacity-20 rounded-full p-4 hover:bg-opacity-30 transition-all">
<svg class="h-16 w-16" fill="currentColor" viewBox="0 0 24 24">
<div class="text-center text-white p-4">
<button onclick="playVideo()" class="bg-white bg-opacity-20 rounded-full p-6 hover:bg-opacity-30 transition-all touch-manipulation">
<svg class="h-16 w-16 md:h-20 md:w-20" fill="currentColor" viewBox="0 0 24 24">
<path d="M8 5v14l11-7z"/>
</svg>
</button>
<h3 class="mt-4 text-xl font-medium" id="channel-title"></h3>
<h3 class="mt-4 text-xl md:text-2xl font-medium" id="channel-title"></h3>
</div>
</div>
<video id="video-player" class="hidden w-full h-full" controls></video>
<video id="video-player" class="hidden w-full h-full object-contain" controls playsinline></video>
</div>
</div>
</div>
<!-- Add/Edit Channel Modal -->
<div id="channelModal" class="fixed inset-0 bg-gray-500 bg-opacity-75 hidden" role="dialog" aria-modal="true">
<div class="fixed inset-0 overflow-y-auto">
<div class="flex min-h-full items-end justify-center p-4 text-center sm:items-center sm:p-0">
<div class="relative transform overflow-hidden rounded-lg bg-white px-4 pb-4 pt-5 text-left shadow-xl transition-all sm:my-8 sm:w-full sm:max-w-lg sm:p-6">
<div class="absolute right-0 top-0 hidden pr-4 pt-4 sm:block">
<button type="button" onclick="hideChannelModal()" class="rounded-md bg-white text-gray-400 hover:text-gray-500">
<span class="sr-only">Close</span>
<svg class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
<div class="sm:flex sm:items-start">
<div class="mt-3 text-center sm:ml-4 sm:mt-0 sm:text-left w-full">
<h3 class="text-base font-semibold leading-6 text-gray-900" id="modalTitle">Add Channel</h3>
<div class="mt-4">
<form id="channelForm" class="space-y-4" method="POST" onsubmit="event.preventDefault(); submitChannelForm();">
{% csrf_token %}
<input type="hidden" id="channelId" name="id">
<div>
<label for="title" class="block text-sm font-medium text-gray-700">Title</label>
<input type="text" name="title" id="title" required class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm">
</div>
<div>
<label for="url" class="block text-sm font-medium text-gray-700">URL</label>
<input type="url" name="url" id="url" required class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm">
</div>
<div>
<label for="description" class="block text-sm font-medium text-gray-700">Description</label>
<textarea name="description" id="description" rows="3" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm"></textarea>
</div>
</form>
</div>
</div>
</div>
<div class="mt-5 sm:mt-4 sm:flex sm:flex-row-reverse">
<button type="button" onclick="submitChannelForm()" class="inline-flex w-full justify-center rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 sm:ml-3 sm:w-auto">Save</button>
<button type="button" onclick="hideChannelModal()" class="mt-3 inline-flex w-full justify-center rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50 sm:mt-0 sm:w-auto">Cancel</button>
</div>
</div>
</div>
</div>
</div>
<!-- Delete Confirmation Modal -->
<div id="deleteModal" class="fixed inset-0 bg-gray-500 bg-opacity-75 hidden" role="dialog" aria-modal="true">
<div class="fixed inset-0 overflow-y-auto">
<div class="flex min-h-full items-end justify-center p-4 text-center sm:items-center sm:p-0">
<div class="relative transform overflow-hidden rounded-lg bg-white px-4 pb-4 pt-5 text-left shadow-xl transition-all sm:my-8 sm:w-full sm:max-w-lg sm:p-6">
<div class="sm:flex sm:items-start">
<div class="mx-auto flex h-12 w-12 flex-shrink-0 items-center justify-center rounded-full bg-red-100 sm:mx-0 sm:h-10 sm:w-10">
<svg class="h-6 w-6 text-red-600" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126zM12 15.75h.007v.008H12v-.008z" />
</svg>
</div>
<div class="mt-3 text-center sm:ml-4 sm:mt-0 sm:text-left">
<h3 class="text-base font-semibold leading-6 text-gray-900">Delete Channel</h3>
<div class="mt-2">
<p class="text-sm text-gray-500">Are you sure you want to delete "<span id="deleteChannelTitle"></span>"? This action cannot be undone.</p>
</div>
</div>
</div>
<div class="mt-5 sm:mt-4 sm:flex sm:flex-row-reverse">
<button type="button" onclick="confirmDelete()" class="inline-flex w-full justify-center rounded-md bg-red-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-red-500 sm:ml-3 sm:w-auto">Delete</button>
<button type="button" onclick="hideDeleteModal()" class="mt-3 inline-flex w-full justify-center rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50 sm:mt-0 sm:w-auto">Cancel</button>
</div>
</div>
</div>
</div>
</div>
@@ -254,5 +334,128 @@ function cleanupPlayer() {
emptyState.classList.remove('hidden');
playOverlay.classList.add('hidden');
}
// Modal handling functions
let currentDeleteId = null;
let lastFocusedElement = null;
function showAddModal() {
lastFocusedElement = document.activeElement;
document.getElementById('modalTitle').textContent = 'Add Channel';
document.getElementById('channelId').value = '';
document.getElementById('channelForm').reset();
const modal = document.getElementById('channelModal');
modal.classList.remove('hidden');
document.getElementById('title').focus();
}
function showEditModal(id, title, url, description) {
lastFocusedElement = document.activeElement;
document.getElementById('modalTitle').textContent = 'Edit Channel';
document.getElementById('channelId').value = id;
document.getElementById('title').value = title;
document.getElementById('url').value = url;
document.getElementById('description').value = description;
const modal = document.getElementById('channelModal');
modal.classList.remove('hidden');
document.getElementById('title').focus();
}
function hideChannelModal() {
document.getElementById('channelModal').classList.add('hidden');
if (lastFocusedElement) {
lastFocusedElement.focus();
}
}
function showDeleteModal(id, title) {
lastFocusedElement = document.activeElement;
currentDeleteId = id;
document.getElementById('deleteChannelTitle').textContent = title;
document.getElementById('deleteModal').classList.remove('hidden');
}
function hideDeleteModal() {
currentDeleteId = null;
document.getElementById('deleteModal').classList.add('hidden');
if (lastFocusedElement) {
lastFocusedElement.focus();
}
}
async function submitChannelForm() {
const form = document.getElementById('channelForm');
const formData = new FormData(form);
const id = formData.get('id');
const url = id ? `/ui/iptv/${id}/edit/` : '/ui/iptv/create/';
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'X-CSRFToken': getCookie('csrftoken'),
},
body: formData,
credentials: 'same-origin'
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
hideChannelModal();
window.location.reload();
} catch (error) {
console.error('Error:', error);
alert('Error saving channel. Please try again.');
}
}
async function confirmDelete() {
if (!currentDeleteId) return;
try {
const response = await fetch(`/ui/iptv/${currentDeleteId}/delete/`, {
method: 'POST',
headers: {
'X-CSRFToken': getCookie('csrftoken'),
},
credentials: 'same-origin'
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
hideDeleteModal();
window.location.reload();
} catch (error) {
console.error('Error:', error);
alert('Error deleting channel. Please try again.');
}
}
// Add keyboard event listeners for modal accessibility
document.addEventListener('keydown', function(e) {
if (e.key === 'Escape') {
hideChannelModal();
hideDeleteModal();
}
});
function getCookie(name) {
let cookieValue = null;
if (document.cookie && document.cookie !== '') {
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
</script>
{% endblock %}
+130
View File
@@ -588,6 +588,18 @@ video {
}
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border-width: 0;
}
.pointer-events-none {
pointer-events: none;
}
@@ -608,6 +620,10 @@ video {
position: relative;
}
.sticky {
position: sticky;
}
.inset-0 {
inset: 0px;
}
@@ -888,6 +904,14 @@ video {
min-height: 0px;
}
.min-h-full {
min-height: 100%;
}
.min-h-\[50vh\] {
min-height: 50vh;
}
.w-12 {
width: 3rem;
}
@@ -1008,6 +1032,10 @@ video {
cursor: pointer;
}
.touch-manipulation {
touch-action: manipulation;
}
.select-all {
-webkit-user-select: all;
-moz-user-select: all;
@@ -1580,6 +1608,11 @@ video {
padding-bottom: 2rem;
}
.py-6 {
padding-top: 1.5rem;
padding-bottom: 1.5rem;
}
.pl-3 {
padding-left: 0.75rem;
}
@@ -1600,6 +1633,14 @@ video {
padding-top: 1.25rem;
}
.pb-4 {
padding-bottom: 1rem;
}
.pt-4 {
padding-top: 1rem;
}
.text-left {
text-align: left;
}
@@ -1866,6 +1907,21 @@ video {
outline-style: solid;
}
.ring-1 {
--tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
--tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(1px + var(--tw-ring-offset-width)) var(--tw-ring-color);
box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
}
.ring-inset {
--tw-ring-inset: inset;
}
.ring-gray-300 {
--tw-ring-opacity: 1;
--tw-ring-color: rgb(209 213 219 / var(--tw-ring-opacity));
}
.blur {
--tw-blur: blur(8px);
filter: var(--tw-blur) var(--tw-brightness) var(--tw-contrast) var(--tw-grayscale) var(--tw-hue-rotate) var(--tw-invert) var(--tw-saturate) var(--tw-sepia) var(--tw-drop-shadow);
@@ -2045,6 +2101,16 @@ video {
background-color: rgb(185 28 28 / var(--tw-bg-opacity));
}
.hover\:bg-indigo-500:hover {
--tw-bg-opacity: 1;
background-color: rgb(99 102 241 / var(--tw-bg-opacity));
}
.hover\:bg-red-500:hover {
--tw-bg-opacity: 1;
background-color: rgb(239 68 68 / var(--tw-bg-opacity));
}
.hover\:bg-opacity-30:hover {
--tw-bg-opacity: 0.3;
}
@@ -2114,6 +2180,11 @@ video {
color: rgb(153 27 27 / var(--tw-text-opacity));
}
.hover\:text-gray-600:hover {
--tw-text-opacity: 1;
color: rgb(75 85 99 / var(--tw-text-opacity));
}
.hover\:underline:hover {
text-decoration-line: underline;
}
@@ -2143,6 +2214,11 @@ video {
border-color: transparent;
}
.focus\:border-indigo-500:focus {
--tw-border-opacity: 1;
border-color: rgb(99 102 241 / var(--tw-border-opacity));
}
.focus\:outline-none:focus {
outline: 2px solid transparent;
outline-offset: 2px;
@@ -2241,6 +2317,11 @@ video {
margin-right: 0px;
}
.sm\:my-8 {
margin-top: 2rem;
margin-bottom: 2rem;
}
.sm\:mb-6 {
margin-bottom: 1.5rem;
}
@@ -2325,6 +2406,14 @@ video {
width: auto;
}
.sm\:w-full {
width: 100%;
}
.sm\:max-w-lg {
max-width: 32rem;
}
.sm\:grid-cols-2 {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
@@ -2411,6 +2500,10 @@ video {
padding: 1.5rem;
}
.sm\:p-0 {
padding: 0px;
}
.sm\:px-4 {
padding-left: 1rem;
padding-right: 1rem;
@@ -2476,6 +2569,30 @@ video {
display: table-cell;
}
.md\:h-20 {
height: 5rem;
}
.md\:h-\[calc\(100vh-4rem\)\] {
height: calc(100vh - 4rem);
}
.md\:h-full {
height: 100%;
}
.md\:min-h-0 {
min-height: 0px;
}
.md\:w-20 {
width: 5rem;
}
.md\:w-80 {
width: 20rem;
}
.md\:grid-cols-3 {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
@@ -2484,9 +2601,22 @@ video {
grid-template-columns: repeat(4, minmax(0, 1fr));
}
.md\:flex-row {
flex-direction: row;
}
.md\:gap-8 {
gap: 2rem;
}
.md\:border-r {
border-right-width: 1px;
}
.md\:text-2xl {
font-size: 1.5rem;
line-height: 2rem;
}
}
@media (min-width: 1024px) {