all repos — fixyoutube-go @ fae2afdff695e4d35f8d1f87d38c94badab89425

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	return res, http.StatusOK
 51}
 52
 53func (c *Client) isNotTimedOut(instance string) bool {
 54	for i := range c.timeouts {
 55		cur := c.timeouts[i]
 56		if instance == cur.Instance {
 57			return false
 58		}
 59	}
 60	return true
 61}
 62
 63func (c *Client) NewInstance() error {
 64	now := time.Now()
 65
 66	timeoutsTest := func(t Timeout) bool { return now.Sub(t.Timestamp) < timeoutDuration }
 67	c.timeouts = filter(c.timeouts, timeoutsTest)
 68	c.timeouts = append(c.timeouts, Timeout{c.Instance, now})
 69
 70	resp, err := c.http.Get(instancesEndpoint)
 71	if err != nil {
 72		return err
 73	}
 74	defer resp.Body.Close()
 75
 76	body, err := io.ReadAll(resp.Body)
 77	if err != nil {
 78		return err
 79	}
 80
 81	if resp.StatusCode != http.StatusOK {
 82		return HTTPError{resp.StatusCode}
 83	}
 84
 85	var jsonArray [][]interface{}
 86	err = json.Unmarshal(body, &jsonArray)
 87	if err != nil {
 88		logger.Error("Could not unmarshal JSON response for instances.")
 89		return err
 90	}
 91
 92	for i := range jsonArray {
 93		instance := jsonArray[i][0].(string)
 94		instanceTest := func(t Timeout) bool { return t.Instance == instance }
 95		result := filter(c.timeouts, instanceTest)
 96		if len(result) == 0 {
 97			c.Instance = instance
 98			logger.Info("Using new instance: ", c.Instance)
 99			return nil
100		}
101	}
102
103	return fmt.Errorf("Cannot find a valid instance.")
104}