all repos — gopipe @ 67133f23417228de33b3483dd9c9559082403ae0

Embed YouTube videos on Telegram, Discord and more!

src/app/video.go (view raw)

  1package app
  2
  3import (
  4	"errors"
  5	"fmt"
  6	"log"
  7	"regexp"
  8	"strconv"
  9	"strings"
 10	"time"
 11
 12	g "github.com/birabittoh/gopipe/src/globals"
 13	"github.com/kkdai/youtube/v2"
 14)
 15
 16const (
 17	maxMB                = 20
 18	maxContentLength     = maxMB * 1048576
 19	defaultCacheDuration = 6 * time.Hour
 20)
 21
 22var (
 23	expireRegex = regexp.MustCompile(`(?i)expire=(\d+)`)
 24)
 25
 26func parseExpiration(url string) time.Duration {
 27	expireString := expireRegex.FindStringSubmatch(url)
 28	expireTimestamp, err := strconv.ParseInt(expireString[1], 10, 64)
 29	if err != nil {
 30		log.Println("parseExpiration ERROR: ", err)
 31		return defaultCacheDuration
 32	}
 33
 34	return time.Until(time.Unix(expireTimestamp, 0))
 35}
 36
 37func getFormat(video youtube.Video, formatID int) *youtube.Format {
 38	if formatID != 0 {
 39		f := video.Formats.Select(formatsSelectFn)
 40		l := len(f)
 41		if l > 0 {
 42			return &f[(formatID-1)%l]
 43		}
 44	}
 45
 46	f := video.Formats.Select(formatsSelectFnBest)
 47	if len(f) > 0 {
 48		return &f[0]
 49	}
 50
 51	return nil
 52}
 53
 54func getCaptions(video youtube.Video) map[string]Captions {
 55	c := make(map[string]Captions)
 56	for _, caption := range video.CaptionTracks {
 57		c[caption.LanguageCode] = Captions{
 58			VideoID:  video.ID,
 59			Language: caption.LanguageCode,
 60			URL:      caption.BaseURL,
 61		}
 62	}
 63
 64	return c
 65}
 66
 67func formatsSelectFn(f youtube.Format) bool {
 68	return f.AudioChannels > 1 && f.ContentLength < maxContentLength && strings.HasPrefix(f.MimeType, "video/mp4")
 69}
 70
 71func formatsSelectFnBest(f youtube.Format) bool {
 72	return f.AudioChannels > 1 && strings.HasPrefix(f.MimeType, "video/mp4")
 73}
 74
 75func getURL(videoID string) string {
 76	return fmt.Sprintf(fmtYouTubeURL, videoID)
 77}
 78
 79func getFromCache(videoID string, formatID int) (video *youtube.Video, format *youtube.Format, err error) {
 80	video, err = g.KS.Get(videoID)
 81	if err != nil {
 82		return
 83	}
 84
 85	if video == nil {
 86		err = errors.New("video should not be nil")
 87		return
 88	}
 89
 90	format = getFormat(*video, formatID)
 91	return
 92}
 93
 94func getFromYT(videoID string, formatID int) (video *youtube.Video, format *youtube.Format, err error) {
 95	url := getURL(videoID)
 96
 97	log.Println("Requesting video ", url)
 98	video, err = g.YT.GetVideo(url)
 99	if err != nil || video == nil {
100		return
101	}
102
103	format = getFormat(*video, formatID)
104	duration := defaultCacheDuration
105	if format != nil {
106		duration = parseExpiration(format.URL)
107	}
108
109	g.KS.Set(videoID, *video, duration)
110	return
111}
112
113func getVideo(videoID string, formatID int) (video *youtube.Video, format *youtube.Format, err error) {
114	video, format, err = getFromCache(videoID, formatID)
115	if err != nil {
116		video, format, err = getFromYT(videoID, formatID)
117	}
118	return
119}