/
anilibria.go
161 lines (129 loc) · 4.21 KB
/
anilibria.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
151
152
153
154
155
156
157
158
159
160
161
package anilibria
import (
"context"
"crypto/tls"
"net"
"net/http"
"net/url"
"strings"
"time"
"github.com/rs/zerolog"
"github.com/urfave/cli/v2"
"golang.org/x/net/http2"
)
var (
gCli *cli.Context
gLog *zerolog.Logger
)
type ApiClient struct {
http *http.Client
apiBaseUrl *url.URL
siteBaseUrl *url.URL
unauthorized bool
}
// TODO:
// - Check keepalive!!
func NewApiClient(ctx *cli.Context, log *zerolog.Logger) (*ApiClient, error) {
gCli, gLog = ctx, log
defaultTransportDialContext := func(dialer *net.Dialer) func(context.Context, string, string) (net.Conn, error) {
return dialer.DialContext
}
http1Transport := &http.Transport{
DialContext: defaultTransportDialContext(&net.Dialer{
Timeout: gCli.Duration("http-tcp-timeout"),
KeepAlive: gCli.Duration("http-keepalive-timeout"),
}),
TLSClientConfig: &tls.Config{
InsecureSkipVerify: gCli.Bool("http-client-insecure"),
MinVersion: tls.VersionTLS12,
MaxVersion: tls.VersionTLS12,
},
TLSHandshakeTimeout: gCli.Duration("http-tls-handshake-timeout"),
MaxIdleConns: gCli.Int("http-max-idle-conns"),
IdleConnTimeout: gCli.Duration("http-idle-timeout"),
DisableCompression: false,
DisableKeepAlives: false,
ForceAttemptHTTP2: true,
}
var httpTransport http.RoundTripper = http1Transport
http2Transport, err := http2.ConfigureTransports(http1Transport)
if err != nil {
httpTransport = http2Transport
gLog.Warn().Err(err).Msg("could not upgrade http transport to v2 because of internal error")
}
var apiClient = &ApiClient{
http: &http.Client{
Timeout: time.Duration(gCli.Int("http-client-timeout")) * time.Second,
Transport: httpTransport,
},
}
// ??
// todo optimize
if err = apiClient.getSiteBaseUrl(); err != nil {
gLog.Error().Err(err).Msg("there are some errors in parsing login url; sleeping for 30 seconds")
time.Sleep(30 * time.Second)
} else {
apiClient.checkAuthData()
}
return apiClient, apiClient.getApiBaseUrl()
}
func (m *ApiClient) getApiBaseUrl() (e error) {
m.apiBaseUrl, e = url.Parse(gCli.String("anilibria-api-baseurl"))
return e
}
func (m *ApiClient) getSiteBaseUrl() (e error) {
m.siteBaseUrl, e = url.Parse(gCli.String("anilibria-baseurl"))
return e
}
func (m *ApiClient) checkAuthData() {
if gCli.String("anilibria-login-username") == "" || gCli.String("anilibria-login-password") == "" {
m.unauthorized = true
gLog.Warn().Msg("could not parse username and\\or password")
gLog.Warn().Msg("ATTENTION! Unauthorized peering detected; Anilibria could not detect the seeder, so it's stats will be disable!!!")
gLog.Info().Msg("\"unauthorized\" has been toggled; sleeping for 3 seconds...")
time.Sleep(3 * time.Second)
}
}
// popular domains origin
// - getSchedule
// - online top
// - releases // ??
// https://api.anilibria.tv/v2/getSchedule?days=0&filter=id,code,names,updated,last_change,status,type,torrents
func (m *ApiClient) GetActiveSessions() (_ *map[string][]string, e error) {
var buf *[]byte
if buf, e = m.getSessionsPage(); e != nil {
return
}
s := newSession()
return s.getActiveAniSessions(buf)
}
func (m *ApiClient) DropActiveSession(sid string) (bool, error) {
return m.dropActiveSession(sid)
}
func (m *ApiClient) DropActiveSessions(sids ...string) {
var ok bool
var err error
for _, sid := range sids {
gLog.Debug().Str("session_id", sid).Msg("trying to close anilibria session...")
if ok, err = m.dropActiveSession(sid); err != nil {
gLog.Warn().Err(err).Str("session_id", sid).Msg("got an error while trying to close the anilibria session")
}
if !ok {
gLog.Warn().Str("session_id", sid).Msg("there was abnormal result from the anilibria site; drop session api said nonOk with 200 OK")
}
}
}
func (m *TitleTorrent) GetShortHash() string {
return m.Hash[0:9]
}
func (m *TitleTorrent) GetName() string {
// https://github.com/MindHunter86/aniliSeeder/issues/74
name := strings.ReplaceAll(m.Metadata.Name, "_", " ")
name, _, _ = strings.Cut(name, "- AniLibria.TV")
return strings.TrimSpace(name)
}
func (m *TitleTorrent) GetTorrentFileName() string {
// https://github.com/MindHunter86/aniliSeeder/issues/74
name := strings.ReplaceAll(m.Metadata.Name, "_", " ")
return strings.Join([]string{name, "torrent"}, ".")
}