Skip to content

Commit

Permalink
add default video height config setting
Browse files Browse the repository at this point in the history
  • Loading branch information
mejgun committed Sep 25, 2024
1 parent 1630f45 commit 3416f83
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 93 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- default video height config setting

## 2.1.0 - 2024-09-25
### Fixed
Expand Down
13 changes: 7 additions & 6 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,12 @@ func getNewApp(log logger.T, v config.SubConfigT) (app.Option, error) {
return app.Option{}, nameerr(texts[2], err)
}
return app.Option{
Name: v.Name,
Sites: v.Sites,
X: xtr,
S: strm,
C: cch,
L: logger.NewLayer(log, fmt.Sprintf("[%s] app", v.Name)),
Name: v.Name,
Sites: v.Sites,
X: xtr,
S: strm,
C: cch,
L: logger.NewLayer(log, fmt.Sprintf("[%s] app", v.Name)),
DefaultVideoHeight: v.DefaultVideoHeight,
}, nil
}
4 changes: 4 additions & 0 deletions config.example.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
// web server listen port.
// DEFAULT 8080
"port": 8080,
// used if video height not set in request
// DEFAULT 720
"default-video-height": 360,
// logger config
"log": {
// log level
Expand Down Expand Up @@ -135,6 +138,7 @@
"sites": [
"my.streamer.example"
],
"default-video-height": 1080,
"extractor": {
"path": "direct"
},
Expand Down
93 changes: 55 additions & 38 deletions lib/app/app.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"fmt"
cache "lib/cache"
extractor "lib/extractor"
extractor_config "lib/extractor/config"
Expand All @@ -17,17 +18,17 @@ import (
)

const (
defaultVideoHeight = "720"
defaultVideoFormat = "mp4"
)

type app struct {
cache cache.T
extractor extractor.T
streamer streamer.T
name string
sites []string
log logger.T
cache cache.T
extractor extractor.T
streamer streamer.T
name string
sites []string
log logger.T
defaultVideoHeight uint16
}

type AppLogic struct {
Expand All @@ -38,12 +39,13 @@ type AppLogic struct {
}

type Option struct {
Name string
Sites []string
X extractor.T
S streamer.T
C cache.T
L logger.T
Name string
Sites []string
X extractor.T
S streamer.T
C cache.T
L logger.T
DefaultVideoHeight uint16
}

func New(log logger.T, def Option, opts []Option) *AppLogic {
Expand All @@ -55,22 +57,24 @@ func New(log logger.T, def Option, opts []Option) *AppLogic {
func (t *AppLogic) set(log logger.T, def Option, opts []Option) {
t.log = log
t.defaultApp = app{
log: def.L,
name: "default",
cache: def.C,
extractor: def.X,
streamer: def.S,
log: def.L,
name: "default",
cache: def.C,
extractor: def.X,
streamer: def.S,
defaultVideoHeight: def.DefaultVideoHeight,
}

t.appList = make([]app, 0)
for _, v := range opts {
t.appList = append(t.appList, app{
cache: v.C,
extractor: v.X,
streamer: v.S,
name: v.Name,
sites: v.Sites,
log: v.L,
cache: v.C,
extractor: v.X,
streamer: v.S,
name: v.Name,
sites: v.Sites,
log: v.L,
defaultVideoHeight: v.DefaultVideoHeight,
})
}
}
Expand Down Expand Up @@ -101,8 +105,9 @@ func (t *AppLogic) Run(w http.ResponseWriter, r *http.Request) {
}
}
now := time.Now()
req := parseQuery(r.RequestURI)
resapp := t.selectApp(req.URL)
link, height, format := parseQuery(r.RequestURI)
resapp := t.selectApp(link)
req := resapp.fixRequest(link, height, format)
t.log.LogInfo("Request", req, "app", resapp.name)
if res, ok, expired := resapp.cacheCheck(req, now); ok {
printExpired(resapp, expired)
Expand Down Expand Up @@ -169,30 +174,42 @@ func remove_http(url string) string {
return url
}

func parseQuery(query string) extractor_config.RequestT {
var req extractor_config.RequestT
func parseQuery(query string) (string, uint64, string) {
query = strings.TrimSpace(strings.TrimPrefix(query, "/play/"))
splitted := strings.Split(query, "?/?")
req.URL = remove_http(splitted[0])
req.HEIGHT = defaultVideoHeight
req.FORMAT = defaultVideoFormat
link := remove_http(splitted[0])
format := defaultVideoFormat
var height uint64
if len(splitted) != 2 {
return req
return link, 0, format
}
tOpts, tErr := url.ParseQuery(splitted[1])
if tErr == nil {
if tvh, ok := tOpts["vh"]; ok && len(tvh[0]) > 2 && len(tvh[0]) < 5 {
if _, err := strconv.ParseUint(tvh[0], 10, 64); err == nil {
req.HEIGHT = tvh[0]
}
if tvh, ok := tOpts["vh"]; ok {
height, _ = strconv.ParseUint(tvh[0], 10, 64)
}
if tvf, ok := tOpts["vf"]; ok {
if tvf[0] == "mp4" || tvf[0] == "m4a" {
req.FORMAT = tvf[0]
format = tvf[0]
}
}
}
return req
return link, height, format

}

func (t *app) fixRequest(link string, height uint64, format string) extractor_config.RequestT {
var h string
if height == 0 {
h = fmt.Sprintf("%d", t.defaultVideoHeight)
} else {
h = fmt.Sprintf("%d", height)
}
return extractor_config.RequestT{
URL: link,
HEIGHT: h,
FORMAT: format,
}
}

func (t *AppLogic) Shutdown() {
Expand Down
53 changes: 13 additions & 40 deletions lib/app/app_test.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,25 @@
package app

import (
"fmt"
"strings"
"testing"

extractor_config "lib/extractor/config"
)

func TestParseQuery(t *testing.T) {
var testPairs = map[string]extractor_config.RequestT{
"/play/youtu.be/jNQXAC9IVRw?/?vh=360&vf=mp4": {
URL: "youtu.be/jNQXAC9IVRw",
HEIGHT: "360",
FORMAT: "mp4",
},
"/play/youtu.be/jNQXAC9IVRw?/?vh=720?vf=avi": {
URL: "youtu.be/jNQXAC9IVRw",
HEIGHT: "720",
FORMAT: defaultVideoFormat,
},
"/play/youtu.be/jNQXAC9IVRw": {
URL: "youtu.be/jNQXAC9IVRw",
HEIGHT: defaultVideoHeight,
FORMAT: defaultVideoFormat,
},
"/play/youtu.be/jNQXAC9IVRw?/?": {
URL: "youtu.be/jNQXAC9IVRw",
HEIGHT: defaultVideoHeight,
FORMAT: defaultVideoFormat,
},
"/play/youtu.be/jNQXAC9IVRw?/?vf=avi": {
URL: "youtu.be/jNQXAC9IVRw",
HEIGHT: defaultVideoHeight,
FORMAT: defaultVideoFormat,
},
"/play/youtu.be/jNQXAC9IVRw?/?vf=mp4": {
URL: "youtu.be/jNQXAC9IVRw",
HEIGHT: defaultVideoHeight,
FORMAT: "mp4",
},
"/play/youtu.be/jNQXAC9IVRw?/?vf=mp4&vh=11111": {
URL: "youtu.be/jNQXAC9IVRw",
HEIGHT: defaultVideoHeight,
FORMAT: "mp4",
},
var testPairs = map[string]string{
"/play/youtu.be/jNQXAC9IVRw?/?vh=360&vf=mp4": "youtu.be/jNQXAC9IVRw|360|mp4",
"/play/youtu.be/jNQXAC9IVRw?/?vh=720&vf=avi": "youtu.be/jNQXAC9IVRw|720|mp4",
"/play/youtu.be/jNQXAC9IVRw": "youtu.be/jNQXAC9IVRw|0|mp4",
"/play/youtu.be/jNQXAC9IVRw?/?": "youtu.be/jNQXAC9IVRw|0|mp4",
"/play/youtu.be/jNQXAC9IVRw?/?vf=avi": "youtu.be/jNQXAC9IVRw|0|mp4",
"/play/youtu.be/jNQXAC9IVRw?/?vf=mp4": "youtu.be/jNQXAC9IVRw|0|mp4",
"/play/youtu.be/jNQXAC9IVRw?/?vf=mp4&vh=11111": "youtu.be/jNQXAC9IVRw|11111|mp4",
}
for k, v := range testPairs {
if r := parseQuery(k); r != v {
t.Error("For", k, "expected", v, "got", r)
l, h, f := parseQuery(k)
if strings.Join([]string{l, fmt.Sprintf("%d", h), f}, "|") != v {
t.Error("For", k, "expected", v, "got", l, h, f)
}
}
}
Expand Down
23 changes: 14 additions & 9 deletions lib/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import (
)

type ConfigT struct {
PortInt uint16 `json:"port"`
Host string `json:"host"`
Streamer streamer.ConfigT `json:"streamer"`
Extractor extractor_config.ConfigT `json:"extractor"`
Log logger_config.ConfigT `json:"log"`
Cache cache.ConfigT `json:"cache"`
SubConfig []SubConfigT `json:"sub-config"`
PortInt uint16 `json:"port"`
Host string `json:"host"`
DefaultVideoHeight uint16 `json:"default-video-height"`
Streamer streamer.ConfigT `json:"streamer"`
Extractor extractor_config.ConfigT `json:"extractor"`
Log logger_config.ConfigT `json:"log"`
Cache cache.ConfigT `json:"cache"`
SubConfig []SubConfigT `json:"sub-config"`
}

type SubConfigT struct {
Expand Down Expand Up @@ -49,8 +50,9 @@ func defaultConfig() ConfigT {
lf := "log.txt"
exp := "3h"
return ConfigT{
PortInt: 8080,
Host: "0.0.0.0",
PortInt: 8080,
Host: "0.0.0.0",
DefaultVideoHeight: 720,
Streamer: streamer.ConfigT{
EnableErrorHeaders: &fls,
IgnoreMissingHeaders: &fls,
Expand Down Expand Up @@ -91,6 +93,9 @@ func appendConfig(src ConfigT, dst ConfigT) ConfigT {
if dst.Host == "" {
dst.Host = src.Host
}
if dst.DefaultVideoHeight == 0 {
dst.DefaultVideoHeight = src.DefaultVideoHeight
}
// streamer
if dst.Streamer.EnableErrorHeaders == nil {
dst.Streamer.EnableErrorHeaders = src.Streamer.EnableErrorHeaders
Expand Down

0 comments on commit 3416f83

Please sign in to comment.