invidious/invidious.go (view raw)
1package invidious
2
3import (
4 "fmt"
5 "net/http"
6 "regexp"
7 "strconv"
8 "time"
9
10 "github.com/sirupsen/logrus"
11)
12
13const timeoutDuration = 10 * time.Minute
14const maxSizeBytes = 20000000 // 20 MB
15const instancesEndpoint = "https://api.invidious.io/instances.json?sort_by=api,type"
16const videosEndpoint = "https://%s/api/v1/videos/%s?fields=videoId,title,description,author,lengthSeconds,size,formatStreams"
17
18var expireRegex = regexp.MustCompile(`(?i)expire=(\d+)`)
19var logger = logrus.New()
20
21type Timeout struct {
22 Instance string
23 Timestamp time.Time
24}
25
26type Client struct {
27 http *http.Client
28 timeouts []Timeout
29 Instance string
30}
31
32type Format struct {
33 VideoId string
34 Name string `json:"qualityLabel"`
35 Height int
36 Width int
37 Url string `json:"url"`
38 Container string `json:"container"`
39 Size string `json:"size"`
40}
41
42type Video struct {
43 VideoId string `json:"videoId"`
44 Title string `json:"title"`
45 Description string `json:"description"`
46 Uploader string `json:"author"`
47 Duration int `json:"lengthSeconds"`
48 Formats []Format `json:"formatStreams"`
49 Timestamp time.Time
50 Expire time.Time
51}
52
53func filter[T any](ss []T, test func(T) bool) (ret []T) {
54 for _, s := range ss {
55 if test(s) {
56 ret = append(ret, s)
57 }
58 }
59 return
60}
61
62func parseOrZero(number string) int {
63 res, err := strconv.Atoi(number)
64 if err != nil {
65 return 0
66 }
67 return res
68}
69
70type HTTPError struct {
71 StatusCode int
72}
73
74func (e HTTPError) Error() string {
75 return fmt.Sprintf("HTTP error: %d", e.StatusCode)
76}
77
78func (c *Client) GetVideo(videoId string, fromCache bool) (*Video, error) {
79 logger.Info("Video https://youtu.be/", videoId, " was requested.")
80
81 var video *Video
82 var err error
83
84 if fromCache {
85 video, err = GetVideoDB(videoId)
86 if err == nil {
87 logger.Info("Found a valid cache entry.")
88 return video, nil
89 }
90 }
91
92 video, httpErr := c.fetchVideo(videoId)
93
94 switch httpErr {
95 case http.StatusOK:
96 logger.Info("Retrieved by API.")
97 break
98 case http.StatusNotFound:
99 logger.Debug("Video does not exist or can't be retrieved.")
100 return nil, err
101 default:
102 fallthrough
103 case http.StatusInternalServerError:
104 err = c.NewInstance()
105 if err != nil {
106 logger.Error("Could not get a new instance: ", err)
107 time.Sleep(10 * time.Second)
108 }
109 return c.GetVideo(videoId, true)
110 }
111
112 err = CacheVideoDB(*video)
113 if err != nil {
114 logger.Warn("Could not cache video id: ", videoId)
115 logger.Warn(err)
116 }
117 return video, nil
118}
119
120func NewClient(httpClient *http.Client) *Client {
121 InitDB()
122 client := &Client{
123 http: httpClient,
124 timeouts: []Timeout{},
125 Instance: "",
126 }
127 err := client.NewInstance()
128 if err != nil {
129 logger.Fatal(err)
130 }
131 return client
132}