-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
150 lines (125 loc) · 3.5 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"github.com/cavaliergopher/grab/v3"
"github.com/charmbracelet/lipgloss"
"github.com/google/go-querystring/query"
)
type JellyfinItem struct {
Name,
Id,
SeriesName,
SeasonName string
SeasonNumber int `json:"ParentIndexNumber"`
EpisodeNumber int `json:"IndexNumber"`
IsFolder bool
}
type Response struct {
Items []JellyfinItem
}
type Query struct {
ParentId string `url:"parentId,omitempty"`
Ids []string `url:"ids,omitempty"`
}
const itemsUrl = "/Users/{userId}/Items"
const downloadUrl = "/Items/{id}/Download"
const auth = "MediaBrowser Client=\"Download Client\", Device=\"Linux\", DeviceId=\"PlRvNOqV9GYvBBUssdhY\", Version=\"1.0\", Token=\"{token}\""
var client = &http.Client{}
func getChilds(parentId string, config *Config) Response {
if parentId == "" {
return queryItems(nil, config)
} else {
return queryItems(&Query{ParentId: parentId}, config)
}
}
func getItems(items []string, config *Config) Response {
var res Response
chunked := chunkBy(items, 200)
for _, v := range chunked {
current := queryItems(&Query{Ids: v}, config)
res.Items = append(res.Items, current.Items...)
}
return res
}
type incorrectAPIEndPointMsg string //With the message if there is
func queryItems(q *Query, config *Config) Response {
var userId = config.UserId
if userId == "" {
userId = "0"
}
requestUrl := strings.ReplaceAll(config.APIEndpoint+itemsUrl, "{userId}", userId)
if q != nil {
queryString, _ := query.Values(q)
requestUrl += "?" + queryString.Encode()
}
program.Send(infoMsg{"Fetching " + requestUrl})
req, err := http.NewRequest("GET", requestUrl, nil)
checkError(err)
req.Header.Add("X-Emby-Authorization", strings.ReplaceAll(auth, "{token}", config.APIKey))
resp, err2 := client.Do(req)
if err2 != nil {
switch err2 := err2.(type) {
case *url.Error:
program.Send(incorrectAPIEndPointMsg("Incorrect Endpoint"))
program.Send(infoMsg{""})
return Response{}
default:
checkError(err2)
}
}
checkResp(resp)
data, err3 := ioutil.ReadAll(resp.Body)
checkError(err3)
body := Response{}
json.Unmarshal(data, &body)
program.Send(infoMsg{""})
return body
}
var red = lipgloss.NewStyle().Foreground(lipgloss.Color("9"))
type incorrectAPIKeyMsg string
type incorrectUserIdMsg string
func checkResp(resp *http.Response) {
if resp.StatusCode != 200 {
bodyText, err := ioutil.ReadAll(resp.Body)
checkError(err)
if resp.StatusCode == 401 {
program.Send(incorrectAPIKeyMsg("Incorrect API key"))
return
}
if resp.StatusCode == 400 {
program.Send(incorrectUserIdMsg("Incorrect UserId"))
return
}
body := string(bodyText)
if body != "" {
body = "null"
}
checkError(fmt.Errorf("Server responded with error code %d when calling %s \nbody: %s", resp.StatusCode, resp.Request.URL, body))
}
}
type startDownloadingItemMsg string
type downloadStartedMsg struct {
Id string
resp *grab.Response
}
var grabClient = grab.NewClient()
func downloadFile(id, dest string, config *Config) (string, error) {
program.Send(startDownloadingItemMsg(id))
req, err := grab.NewRequest(dest, strings.ReplaceAll(config.APIEndpoint+downloadUrl, "{id}", id))
checkError(err)
req.HTTPRequest.Header.Add("X-Emby-Authorization", strings.ReplaceAll(auth, "{token}", config.APIKey))
resp := grabClient.Do(req)
program.Send(downloadStartedMsg{id, resp})
<-resp.Done
if err := resp.Err(); err != nil {
os.Remove(resp.Filename)
return "", err
}
return resp.Filename, nil
}