Skip to content

Commit

Permalink
do not insert channels without titles
Browse files Browse the repository at this point in the history
  • Loading branch information
sonroyaalmerol committed Mar 2, 2024
1 parent c21f25a commit 9df85c2
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 98 deletions.
1 change: 0 additions & 1 deletion m3u/m3u_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ http://example.com/channel3
expectedStreams := []database.StreamInfo{
{URLs: []database.StreamURL{{Content: "http://example.com/channel1", M3UIndex: 0}}, TvgID: "channel1", Title: "Channel 1", Group: "Group 1", LogoURL: "logo1.jpg"},
{URLs: []database.StreamURL{{Content: "http://example.com/channel2", M3UIndex: 0}}, TvgID: "channel2", Title: "Channel 2", Group: "Group 2", LogoURL: "logo2.jpg"},
{URLs: []database.StreamURL{{Content: "http://example.com/channel3", M3UIndex: 0}}, LogoURL: "http://example.com/logo3.jpg"},
}

// Retrieve streams from the database
Expand Down
14 changes: 8 additions & 6 deletions m3u/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func parseM3UFile(filePath string, m3uIndex int) (error) {
matches := regex.FindAllStringSubmatch(line, -1)

for _, match := range matches {
key := match[1]
value := match[2]
key := strings.TrimSpace(match[1])
value := strings.TrimSpace(match[2])

switch key {
case "tvg-id":
Expand All @@ -64,10 +64,12 @@ func parseM3UFile(filePath string, m3uIndex int) (error) {
M3UIndex: m3uIndex,
},
}

err = database.InsertStream(currentStream)
if err != nil {
return fmt.Errorf("InsertStream error: %v", err)

if currentStream.Title != "" {
err = database.InsertStream(currentStream)
if err != nil {
return fmt.Errorf("InsertStream error: %v", err)
}
}
}
}
Expand Down
91 changes: 0 additions & 91 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,105 +2,14 @@ package main

import (
"context"
"errors"
"fmt"
"io"
"log"
"m3u-stream-merger/database"
"m3u-stream-merger/m3u"
"m3u-stream-merger/utils"
"net/http"
"os"
"strconv"
"strings"
"syscall"
"time"
)

func mp4Handler(w http.ResponseWriter, r *http.Request) {
// Log the incoming request
log.Printf("Received request from %s for URL: %s\n", r.RemoteAddr, r.URL.Path)

// Extract the m3u ID from the URL path
m3uID := strings.TrimSuffix(strings.TrimPrefix(r.URL.Path, "/stream/"), ".mp4")
if m3uID == "" {
http.NotFound(w, r)
return
}

streamName := utils.GetStreamName(m3uID)
if streamName == "" {
http.NotFound(w, r)
return
}

stream, err := database.GetStreamByTitle(streamName)
if err != nil {
http.NotFound(w, r)
return
}

// You can modify the response header as needed
w.Header().Set("Content-Type", "video/mp4")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Access-Control-Allow-Origin", "*")

var resp *http.Response
defer func() {
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
}()

for _, url := range stream.URLs {
resp, err = http.Get(url.Content)
if err == nil {
break
}
// Log the error
log.Printf("Error fetching MP4 stream: %s\n", err.Error())
}

if resp == nil {
// Log the error
log.Println("Error fetching MP4 stream. Exhausted all streams.")
// Check if the connection is still open before writing to the response
select {
case <-r.Context().Done():
// Connection closed, handle accordingly
log.Println("Client disconnected")
return
default:
// Connection still open, proceed with writing to the response
http.Error(w, "Error fetching MP4 stream. Exhausted all streams.", http.StatusInternalServerError)
return
}
}

// Log the successful response
log.Printf("Sent MP4 stream to %s\n", r.RemoteAddr)

// Check if the connection is still open before copying the MP4 stream to the response
select {
case <-r.Context().Done():
// Connection closed, handle accordingly
log.Println("Client disconnected after fetching MP4 stream")
return
default:
// Connection still open, proceed with writing to the response
_, err := io.Copy(w, resp.Body)
if err != nil {
// Log the error
if errors.Is(err, syscall.EPIPE) {
log.Println("Client disconnected after fetching MP4 stream")
} else {
log.Printf("Error copying MP4 stream to response: %s\n", err.Error())
}
return
}
}
}

func updateSource(ctx context.Context) {
for {
select {
Expand Down
Empty file added mp4_handler.go
Empty file.

0 comments on commit 9df85c2

Please sign in to comment.