all repos — gopipe @ dadc4690b5715ff53e41c968a5024894d0018950

Embed YouTube videos on Telegram, Discord and more!

cleaner code
Marco Andronaco andronacomarco@gmail.com
Tue, 15 Oct 2024 23:22:31 +0200
commit

dadc4690b5715ff53e41c968a5024894d0018950

parent

d9ab51a35a4242ee8f1f5c0760ced3905f84e872

2 files changed, 32 insertions(+), 25 deletions(-)

jump to
M src/app/handlers.gosrc/app/handlers.go

@@ -62,6 +62,11 @@ http.Error(w, err500, http.StatusInternalServerError)

return } + if video == nil || format == nil { + http.Error(w, err500, http.StatusInternalServerError) + return + } + data := map[string]interface{}{ "VideoID": videoID, "VideoURL": format.URL,
M src/app/video.gosrc/app/video.go

@@ -23,15 +23,24 @@ emptyVideo = youtube.Video{}

expireRegex = regexp.MustCompile(`(?i)expire=(\d+)`) ) -func parseExpiration(url string) (time.Duration, error) { +func parseExpiration(url string) time.Duration { expireString := expireRegex.FindStringSubmatch(url) expireTimestamp, err := strconv.ParseInt(expireString[1], 10, 64) if err != nil { log.Println("parseExpiration ERROR: ", err) - return time.Duration(0), err + return defaultCacheDuration } - return time.Until(time.Unix(expireTimestamp, 0)), nil + return time.Until(time.Unix(expireTimestamp, 0)) +} + +func getFormat(video youtube.Video) *youtube.Format { + formats := video.Formats.Select(formatsSelectFn) + if len(formats) == 0 { + return nil + } + + return &formats[0] } func formatsSelectFn(f youtube.Format) bool {

@@ -42,7 +51,7 @@ func getURL(videoID string) string {

return fmt.Sprintf(fmtYouTubeURL, videoID) } -func getFromCache(videoID string) (video *youtube.Video, format youtube.Format, err error) { +func getFromCache(videoID string) (video *youtube.Video, format *youtube.Format, err error) { video, err = g.KS.Get(videoID) if err != nil { return

@@ -58,39 +67,32 @@ err = errors.New("no formats for this video")

return } - formats := video.Formats.Select(formatsSelectFn) - if len(formats) == 0 { - err = errors.New("no formats for this video") - return - } - - format = formats[0] + format = getFormat(*video) return } -func getFromYT(videoID string) (video *youtube.Video, format youtube.Format, err error) { - video, err = g.YT.GetVideo(getURL(videoID)) +func getFromYT(videoID string) (video *youtube.Video, format *youtube.Format, err error) { + url := getURL(videoID) + + log.Println("Requesting video ", url) + video, err = g.YT.GetVideo(url) if err != nil { return } - formats := video.Formats.Select(formatsSelectFn) - if len(formats) == 0 { - g.KS.Set(videoID, emptyVideo, defaultCacheDuration) - return - } - - format = formats[0] - expiration, err := parseExpiration(format.URL) - if err != nil { - expiration = defaultCacheDuration + format = getFormat(*video) + duration := defaultCacheDuration + v := emptyVideo + if format != nil { + v = *video + duration = parseExpiration(format.URL) } - g.KS.Set(videoID, *video, expiration) + g.KS.Set(videoID, v, duration) return } -func getVideo(videoID string) (video *youtube.Video, format youtube.Format, err error) { +func getVideo(videoID string) (video *youtube.Video, format *youtube.Format, err error) { video, format, err = getFromCache(videoID) if err != nil { video, format, err = getFromYT(videoID)