Use memmap to cache json replies from ytdl
parent
8bee2cc93f
commit
0411652b5b
48
ytdl/ytdl.go
48
ytdl/ytdl.go
|
@ -3,13 +3,24 @@ package ytdl
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gohugoio/hugo/cache/filecache"
|
||||||
|
"github.com/spf13/afero"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var cache *filecache.Cache
|
||||||
|
var yturl = "https://youtu.be/%s"
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
fs := afero.NewMemMapFs()
|
||||||
|
cache = filecache.NewCache(fs, 6*time.Hour, "")
|
||||||
|
}
|
||||||
|
|
||||||
type YTdl struct {
|
type YTdl struct {
|
||||||
Title string
|
Title string
|
||||||
Url string
|
Url string
|
||||||
|
@ -17,19 +28,30 @@ type YTdl struct {
|
||||||
Len time.Duration
|
Len time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewYTdl(uri string) (*YTdl, error) {
|
func NewYTdl(vid string) (*YTdl, error) {
|
||||||
ytdl_js, err := exec.Command(
|
log.Printf("Loading %s from youtube\n", vid)
|
||||||
"./bin/yt-dlp_linux",
|
_, ytdl_js, err := cache.GetOrCreateBytes(vid+".json", func() ([]byte, error) {
|
||||||
uri,
|
log.Printf("%s not found in cache, downloading info.\n", vid)
|
||||||
"--cookies", "./cookies.txt",
|
js, err := exec.Command(
|
||||||
"--no-call-home",
|
"./bin/yt-dlp_linux",
|
||||||
"--no-cache-dir",
|
fmt.Sprintf(yturl, vid),
|
||||||
"--ignore-errors",
|
"--cookies", "./cookies.txt",
|
||||||
"--newline",
|
"--no-call-home",
|
||||||
"--restrict-filenames",
|
"--no-cache-dir",
|
||||||
"-f", "140",
|
"--ignore-errors",
|
||||||
"-j",
|
"--newline",
|
||||||
).Output()
|
"--restrict-filenames",
|
||||||
|
"-f", "140",
|
||||||
|
"-j",
|
||||||
|
).Output()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("%s is now cached.\n", vid)
|
||||||
|
|
||||||
|
return js, nil
|
||||||
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
Loading…
Reference in New Issue