0
Fork 0
mirror of https://codeberg.org/SafeTwitch/safetwitch-backend.git synced 2024-12-22 13:13:00 -05:00

Create proper parsers for streams

This commit is contained in:
dragongoose 2023-06-02 08:13:45 -04:00
parent 36cc5a0c40
commit b2cc516967
No known key found for this signature in database
GPG key ID: 50DB99B921579009

View file

@ -3,6 +3,7 @@ package twitch
import (
"encoding/base64"
"errors"
"os"
"safetwitch-backend/extractor"
"safetwitch-backend/extractor/structs"
"strings"
@ -137,14 +138,33 @@ func ParseBadges(data gjson.Result) ([]structs.Badge, error) {
return formattedBadges, nil
}
func ProxyPlaylistFile(playlist string) string {
func StreamSubProxyUrl(url string) string {
encodedUrl := base64.StdEncoding.EncodeToString([]byte(url))
backendUrl := os.Getenv("URL")
return backendUrl + "/proxy/stream/sub/" + encodedUrl
}
func StreamSegmentProxyUrl(url string) string {
encodedUrl := base64.StdEncoding.EncodeToString([]byte(url))
backendUrl := os.Getenv("URL")
return backendUrl + "/proxy/stream/segment/" + encodedUrl
}
func ProxyPlaylistFile(playlist string, isSubPlaylist bool) string {
// Split the playlist into individual entries
entries := strings.Split(playlist, "\n")[1:] // Ignore the first line which contains the M3U header
// Loop through each entry and replace the URL
for i, entry := range entries {
if strings.HasPrefix(entry, "http") { // Only modify lines that contain URLs
newURL := extractor.ProxyUrl(entry)
var newURL string
if isSubPlaylist {
newURL = StreamSegmentProxyUrl(entry)
} else {
newURL = StreamSubProxyUrl(entry)
}
entries[i] = newURL
}
}