all repos — fixyoutube-go @ 0c53037d82b827f00b3bdce97e197c45af008673

A better way to embed YouTube videos everywhere (inspired by FixTweet).

invidious/api.go (view raw)

  1package invidious
  2
  3import (
  4	"encoding/json"
  5	"fmt"
  6	"io"
  7	"net/http"
  8	"net/url"
  9	"strconv"
 10	"time"
 11)
 12
 13func (c *Client) fetchVideo(videoId string) (*Video, int) {
 14	endpoint := fmt.Sprintf(videosEndpoint, c.Instance, url.QueryEscape(videoId))
 15	resp, err := c.http.Get(endpoint)
 16	if err != nil {
 17		logger.Error(err)
 18		return nil, http.StatusInternalServerError
 19	}
 20	defer resp.Body.Close()
 21
 22	if resp.StatusCode != http.StatusOK {
 23		logger.Warn("Invidious gave the following status code: ", resp.StatusCode)
 24		return nil, http.StatusNotFound
 25	}
 26
 27	body, err := io.ReadAll(resp.Body)
 28	if err != nil {
 29		logger.Error(err)
 30		return nil, http.StatusInternalServerError
 31	}
 32
 33	res := &Video{}
 34	err = json.Unmarshal(body, res)
 35	if err != nil {
 36		logger.Error(err)
 37		return nil, http.StatusInternalServerError
 38	}
 39
 40	mp4Test := func(f Format) bool { return f.Container == "mp4" }
 41	res.Formats = filter(res.Formats, mp4Test)
 42
 43	expireString := expireRegex.FindStringSubmatch(res.Formats[0].Url)
 44	expireTimestamp, err := strconv.ParseInt(expireString[1], 10, 64)
 45	if err != nil {
 46		logger.Error(err)
 47		return nil, http.StatusInternalServerError
 48	}
 49	res.Expire = time.Unix(expireTimestamp, 0)
 50
 51	_, l, i := c.findCompatibleFormat(res)
 52	if l == 0 {
 53		logger.Warn("No compatible formats found for video.")
 54		res.Url = ""
 55	}
 56
 57	res.Url = res.Formats[i].Url
 58	return res, http.StatusOK
 59}
 60
 61func (c *Client) isNotTimedOut(instance string) bool {
 62	for i := range c.timeouts {
 63		cur := c.timeouts[i]
 64		if instance == cur.Instance {
 65			return false
 66		}
 67	}
 68	return true
 69}
 70
 71func (c *Client) NewInstance() error {
 72	now := time.Now()
 73
 74	timeoutsTest := func(t Timeout) bool { return now.Sub(t.Timestamp) < timeoutDuration }
 75	c.timeouts = filter(c.timeouts, timeoutsTest)
 76	c.timeouts = append(c.timeouts, Timeout{c.Instance, now})
 77
 78	resp, err := c.http.Get(instancesEndpoint)
 79	if err != nil {
 80		return err
 81	}
 82	defer resp.Body.Close()
 83
 84	body, err := io.ReadAll(resp.Body)
 85	if err != nil {
 86		return err
 87	}
 88
 89	if resp.StatusCode != http.StatusOK {
 90		return fmt.Errorf("HTTP error: %d", resp.StatusCode)
 91	}
 92
 93	var jsonArray [][]interface{}
 94	err = json.Unmarshal(body, &jsonArray)
 95	if err != nil {
 96		logger.Error("Could not unmarshal JSON response for instances.")
 97		return err
 98	}
 99
100	for i := range jsonArray {
101		instance := jsonArray[i][0].(string)
102		instanceTest := func(t Timeout) bool { return t.Instance == instance }
103		result := filter(c.timeouts, instanceTest)
104		if len(result) == 0 {
105			c.Instance = instance
106			logger.Info("Using new instance: ", c.Instance)
107			return nil
108		}
109	}
110
111	return fmt.Errorf("Cannot find a valid instance.")
112}