Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/drivers/baidu_share/driver.go
1986 views
1
package baidu_share
2
3
import (
4
"context"
5
"encoding/json"
6
"fmt"
7
"io"
8
"net/http"
9
"net/url"
10
"path"
11
"time"
12
13
"github.com/alist-org/alist/v3/internal/driver"
14
"github.com/alist-org/alist/v3/internal/errs"
15
"github.com/alist-org/alist/v3/internal/model"
16
"github.com/go-resty/resty/v2"
17
)
18
19
type BaiduShare struct {
20
model.Storage
21
Addition
22
client *resty.Client
23
info struct {
24
Root string
25
Seckey string
26
Shareid string
27
Uk string
28
}
29
}
30
31
func (d *BaiduShare) Config() driver.Config {
32
return config
33
}
34
35
func (d *BaiduShare) GetAddition() driver.Additional {
36
return &d.Addition
37
}
38
39
func (d *BaiduShare) Init(ctx context.Context) error {
40
// TODO login / refresh token
41
//op.MustSaveDriverStorage(d)
42
d.client = resty.New().
43
SetBaseURL("https://pan.baidu.com").
44
SetHeader("User-Agent", "netdisk").
45
SetCookie(&http.Cookie{Name: "BDUSS", Value: d.BDUSS}).
46
SetCookie(&http.Cookie{Name: "ndut_fmt"})
47
respJson := struct {
48
Errno int64 `json:"errno"`
49
Data struct {
50
List [1]struct {
51
Path string `json:"path"`
52
} `json:"list"`
53
Uk json.Number `json:"uk"`
54
Shareid json.Number `json:"shareid"`
55
Seckey string `json:"seckey"`
56
} `json:"data"`
57
}{}
58
resp, err := d.client.R().
59
SetBody(url.Values{
60
"pwd": {d.Pwd},
61
"root": {"1"},
62
"shorturl": {d.Surl},
63
}.Encode()).
64
SetResult(&respJson).
65
Post("share/wxlist?channel=weixin&version=2.2.2&clienttype=25&web=1")
66
if err == nil {
67
if resp.IsSuccess() && respJson.Errno == 0 {
68
d.info.Root = path.Dir(respJson.Data.List[0].Path)
69
d.info.Seckey = respJson.Data.Seckey
70
d.info.Shareid = respJson.Data.Shareid.String()
71
d.info.Uk = respJson.Data.Uk.String()
72
} else {
73
err = fmt.Errorf(" %s; %s; ", resp.Status(), resp.Body())
74
}
75
}
76
return err
77
}
78
79
func (d *BaiduShare) Drop(ctx context.Context) error {
80
return nil
81
}
82
83
func (d *BaiduShare) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
84
// TODO return the files list, required
85
reqDir := dir.GetPath()
86
isRoot := "0"
87
if reqDir == d.RootFolderPath {
88
reqDir = path.Join(d.info.Root, reqDir)
89
}
90
if reqDir == d.info.Root {
91
isRoot = "1"
92
}
93
objs := []model.Obj{}
94
var err error
95
var page uint64 = 1
96
more := true
97
for more && err == nil {
98
respJson := struct {
99
Errno int64 `json:"errno"`
100
Data struct {
101
More bool `json:"has_more"`
102
List []struct {
103
Fsid json.Number `json:"fs_id"`
104
Isdir json.Number `json:"isdir"`
105
Path string `json:"path"`
106
Name string `json:"server_filename"`
107
Mtime json.Number `json:"server_mtime"`
108
Size json.Number `json:"size"`
109
} `json:"list"`
110
} `json:"data"`
111
}{}
112
resp, e := d.client.R().
113
SetBody(url.Values{
114
"dir": {reqDir},
115
"num": {"1000"},
116
"order": {"time"},
117
"page": {fmt.Sprint(page)},
118
"pwd": {d.Pwd},
119
"root": {isRoot},
120
"shorturl": {d.Surl},
121
}.Encode()).
122
SetResult(&respJson).
123
Post("share/wxlist?channel=weixin&version=2.2.2&clienttype=25&web=1")
124
err = e
125
if err == nil {
126
if resp.IsSuccess() && respJson.Errno == 0 {
127
page++
128
more = respJson.Data.More
129
for _, v := range respJson.Data.List {
130
size, _ := v.Size.Int64()
131
mtime, _ := v.Mtime.Int64()
132
objs = append(objs, &model.Object{
133
ID: v.Fsid.String(),
134
Path: v.Path,
135
Name: v.Name,
136
Size: size,
137
Modified: time.Unix(mtime, 0),
138
IsFolder: v.Isdir.String() == "1",
139
})
140
}
141
} else {
142
err = fmt.Errorf(" %s; %s; ", resp.Status(), resp.Body())
143
}
144
}
145
}
146
return objs, err
147
}
148
149
func (d *BaiduShare) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
150
// TODO return link of file, required
151
link := model.Link{Header: d.client.Header}
152
sign := ""
153
stamp := ""
154
signJson := struct {
155
Errno int64 `json:"errno"`
156
Data struct {
157
Stamp json.Number `json:"timestamp"`
158
Sign string `json:"sign"`
159
} `json:"data"`
160
}{}
161
resp, err := d.client.R().
162
SetQueryParam("surl", d.Surl).
163
SetResult(&signJson).
164
Get("share/tplconfig?fields=sign,timestamp&channel=chunlei&web=1&app_id=250528&clienttype=0")
165
if err == nil {
166
if resp.IsSuccess() && signJson.Errno == 0 {
167
stamp = signJson.Data.Stamp.String()
168
sign = signJson.Data.Sign
169
} else {
170
err = fmt.Errorf(" %s; %s; ", resp.Status(), resp.Body())
171
}
172
}
173
if err == nil {
174
respJson := struct {
175
Errno int64 `json:"errno"`
176
List [1]struct {
177
Dlink string `json:"dlink"`
178
} `json:"list"`
179
}{}
180
resp, err = d.client.R().
181
SetQueryParam("sign", sign).
182
SetQueryParam("timestamp", stamp).
183
SetBody(url.Values{
184
"encrypt": {"0"},
185
"extra": {fmt.Sprintf(`{"sekey":"%s"}`, d.info.Seckey)},
186
"fid_list": {fmt.Sprintf("[%s]", file.GetID())},
187
"primaryid": {d.info.Shareid},
188
"product": {"share"},
189
"type": {"nolimit"},
190
"uk": {d.info.Uk},
191
}.Encode()).
192
SetResult(&respJson).
193
Post("api/sharedownload?app_id=250528&channel=chunlei&clienttype=12&web=1")
194
if err == nil {
195
if resp.IsSuccess() && respJson.Errno == 0 && respJson.List[0].Dlink != "" {
196
link.URL = respJson.List[0].Dlink
197
} else {
198
err = fmt.Errorf(" %s; %s; ", resp.Status(), resp.Body())
199
}
200
}
201
if err == nil {
202
resp, err = d.client.R().
203
SetDoNotParseResponse(true).
204
Get(link.URL)
205
if err == nil {
206
defer resp.RawBody().Close()
207
if resp.IsError() {
208
byt, _ := io.ReadAll(resp.RawBody())
209
err = fmt.Errorf(" %s; %s; ", resp.Status(), byt)
210
}
211
}
212
}
213
}
214
return &link, err
215
}
216
217
func (d *BaiduShare) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
218
// TODO create folder, optional
219
return errs.NotSupport
220
}
221
222
func (d *BaiduShare) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
223
// TODO move obj, optional
224
return errs.NotSupport
225
}
226
227
func (d *BaiduShare) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
228
// TODO rename obj, optional
229
return errs.NotSupport
230
}
231
232
func (d *BaiduShare) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
233
// TODO copy obj, optional
234
return errs.NotSupport
235
}
236
237
func (d *BaiduShare) Remove(ctx context.Context, obj model.Obj) error {
238
// TODO remove obj, optional
239
return errs.NotSupport
240
}
241
242
func (d *BaiduShare) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
243
// TODO upload file, optional
244
return errs.NotSupport
245
}
246
247
//func (d *Template) Other(ctx context.Context, args model.OtherArgs) (interface{}, error) {
248
// return nil, errs.NotSupport
249
//}
250
251
var _ driver.Driver = (*BaiduShare)(nil)
252
253