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
 
 
 
 
 
 
 
 
 
 
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package baiduphoto

import (
	"context"
	"encoding/hex"
	"fmt"
	"net/http"
	"strconv"
	"strings"
	"unicode"

	"github.com/alist-org/alist/v3/drivers/base"
	"github.com/alist-org/alist/v3/internal/model"
	"github.com/alist-org/alist/v3/pkg/utils"
	"github.com/go-resty/resty/v2"
)

const (
	API_URL         = "https://photo.baidu.com/youai"
	USER_API_URL    = API_URL + "/user/v1"
	ALBUM_API_URL   = API_URL + "/album/v1"
	FILE_API_URL_V1 = API_URL + "/file/v1"
	FILE_API_URL_V2 = API_URL + "/file/v2"
)

func (d *BaiduPhoto) Request(client *resty.Client, furl string, method string, callback base.ReqCallback, resp interface{}) (*resty.Response, error) {
	req := client.R().
		// SetQueryParam("access_token", d.AccessToken)
		SetHeader("Cookie", d.Cookie)
	if callback != nil {
		callback(req)
	}
	if resp != nil {
		req.SetResult(resp)
	}
	res, err := req.Execute(method, furl)
	if err != nil {
		return nil, err
	}

	erron := utils.Json.Get(res.Body(), "errno").ToInt()
	switch erron {
	case 0:
		break
	case 50805:
		return nil, fmt.Errorf("you have joined album")
	case 50820:
		return nil, fmt.Errorf("no shared albums found")
	case 50100:
		return nil, fmt.Errorf("illegal title, only supports 50 characters")
	// case -6:
	// 	if err = d.refreshToken(); err != nil {
	// 		return nil, err
	// 	}
	default:
		return nil, fmt.Errorf("errno: %d, refer to https://photo.baidu.com/union/doc", erron)
	}
	return res, nil
}

//func (d *BaiduPhoto) Request(furl string, method string, callback base.ReqCallback, resp interface{}) ([]byte, error) {
//	res, err := d.request(furl, method, callback, resp)
//	if err != nil {
//		return nil, err
//	}
//	return res.Body(), nil
//}

// func (d *BaiduPhoto) refreshToken() error {
// 	u := "https://openapi.baidu.com/oauth/2.0/token"
// 	var resp base.TokenResp
// 	var e TokenErrResp
// 	_, err := base.RestyClient.R().SetResult(&resp).SetError(&e).SetQueryParams(map[string]string{
// 		"grant_type":    "refresh_token",
// 		"refresh_token": d.RefreshToken,
// 		"client_id":     d.ClientID,
// 		"client_secret": d.ClientSecret,
// 	}).Get(u)
// 	if err != nil {
// 		return err
// 	}
// 	if e.ErrorMsg != "" {
// 		return &e
// 	}
// 	if resp.RefreshToken == "" {
// 		return errs.EmptyToken
// 	}
// 	d.AccessToken, d.RefreshToken = resp.AccessToken, resp.RefreshToken
// 	op.MustSaveDriverStorage(d)
// 	return nil
// }

func (d *BaiduPhoto) Get(furl string, callback base.ReqCallback, resp interface{}) (*resty.Response, error) {
	return d.Request(base.RestyClient, furl, http.MethodGet, callback, resp)
}

func (d *BaiduPhoto) Post(furl string, callback base.ReqCallback, resp interface{}) (*resty.Response, error) {
	return d.Request(base.RestyClient, furl, http.MethodPost, callback, resp)
}

// 获取所有文件
func (d *BaiduPhoto) GetAllFile(ctx context.Context) (files []File, err error) {
	var cursor string
	for {
		var resp FileListResp
		_, err = d.Get(FILE_API_URL_V1+"/list", func(r *resty.Request) {
			r.SetContext(ctx)
			r.SetQueryParams(map[string]string{
				"need_thumbnail":     "1",
				"need_filter_hidden": "0",
				"cursor":             cursor,
			})
		}, &resp)
		if err != nil {
			return
		}

		files = append(files, resp.List...)
		if !resp.HasNextPage() {
			return
		}
		cursor = resp.Cursor
	}
}

// 删除根文件
func (d *BaiduPhoto) DeleteFile(ctx context.Context, file *File) error {
	_, err := d.Get(FILE_API_URL_V1+"/delete", func(req *resty.Request) {
		req.SetContext(ctx)
		req.SetQueryParams(map[string]string{
			"fsid_list": fmt.Sprintf("[%d]", file.Fsid),
		})
	}, nil)
	return err
}

// 获取所有相册
func (d *BaiduPhoto) GetAllAlbum(ctx context.Context) (albums []Album, err error) {
	var cursor string
	for {
		var resp AlbumListResp
		_, err = d.Get(ALBUM_API_URL+"/list", func(r *resty.Request) {
			r.SetContext(ctx)
			r.SetQueryParams(map[string]string{
				"need_amount": "1",
				"limit":       "100",
				"cursor":      cursor,
			})
		}, &resp)
		if err != nil {
			return
		}
		if albums == nil {
			albums = make([]Album, 0, resp.TotalCount)
		}

		cursor = resp.Cursor
		albums = append(albums, resp.List...)

		if !resp.HasNextPage() {
			return
		}
	}
}

// 获取相册中所有文件
func (d *BaiduPhoto) GetAllAlbumFile(ctx context.Context, album *Album, passwd string) (files []AlbumFile, err error) {
	var cursor string
	for {
		var resp AlbumFileListResp
		_, err = d.Get(ALBUM_API_URL+"/listfile", func(r *resty.Request) {
			r.SetContext(ctx)
			r.SetQueryParams(map[string]string{
				"album_id":    album.AlbumID,
				"need_amount": "1",
				"limit":       "1000",
				"passwd":      passwd,
				"cursor":      cursor,
			})
		}, &resp)
		if err != nil {
			return
		}
		if files == nil {
			files = make([]AlbumFile, 0, resp.TotalCount)
		}

		cursor = resp.Cursor
		files = append(files, resp.List...)

		if !resp.HasNextPage() {
			return
		}
	}
}

// 创建相册
func (d *BaiduPhoto) CreateAlbum(ctx context.Context, name string) (*Album, error) {
	var resp JoinOrCreateAlbumResp
	_, err := d.Post(ALBUM_API_URL+"/create", func(r *resty.Request) {
		r.SetContext(ctx).SetResult(&resp)
		r.SetQueryParams(map[string]string{
			"title":  name,
			"tid":    getTid(),
			"source": "0",
		})
	}, nil)
	if err != nil {
		return nil, err
	}
	return d.GetAlbumDetail(ctx, resp.AlbumID)
}

// 相册改名
func (d *BaiduPhoto) SetAlbumName(ctx context.Context, album *Album, name string) (*Album, error) {
	_, err := d.Post(ALBUM_API_URL+"/settitle", func(r *resty.Request) {
		r.SetContext(ctx)
		r.SetFormData(map[string]string{
			"title":    name,
			"album_id": album.AlbumID,
			"tid":      fmt.Sprint(album.Tid),
		})
	}, nil)
	if err != nil {
		return nil, err
	}
	return renameAlbum(album, name), nil
}

// 删除相册
func (d *BaiduPhoto) DeleteAlbum(ctx context.Context, album *Album) error {
	_, err := d.Post(ALBUM_API_URL+"/delete", func(r *resty.Request) {
		r.SetContext(ctx)
		r.SetFormData(map[string]string{
			"album_id":            album.AlbumID,
			"tid":                 fmt.Sprint(album.Tid),
			"delete_origin_image": BoolToIntStr(d.DeleteOrigin), // 是否删除原图 0 不删除 1 删除
		})
	}, nil)
	return err
}

// 删除相册文件
func (d *BaiduPhoto) DeleteAlbumFile(ctx context.Context, file *AlbumFile) error {
	_, err := d.Post(ALBUM_API_URL+"/delfile", func(r *resty.Request) {
		r.SetContext(ctx)
		r.SetFormData(map[string]string{
			"album_id":   fmt.Sprint(file.AlbumID),
			"tid":        fmt.Sprint(file.Tid),
			"list":       fmt.Sprintf(`[{"fsid":%d,"uk":%d}]`, file.Fsid, file.Uk),
			"del_origin": BoolToIntStr(d.DeleteOrigin), // 是否删除原图 0 不删除 1 删除
		})
	}, nil)
	return err
}

// 增加相册文件
func (d *BaiduPhoto) AddAlbumFile(ctx context.Context, album *Album, file *File) (*AlbumFile, error) {
	_, err := d.Get(ALBUM_API_URL+"/addfile", func(r *resty.Request) {
		r.SetContext(ctx)
		r.SetQueryParams(map[string]string{
			"album_id": fmt.Sprint(album.AlbumID),
			"tid":      fmt.Sprint(album.Tid),
			"list":     fsidsFormatNotUk(file.Fsid),
		})
	}, nil)
	if err != nil {
		return nil, err
	}
	return moveFileToAlbumFile(file, album, d.Uk), nil
}

// 保存相册文件为根文件
func (d *BaiduPhoto) CopyAlbumFile(ctx context.Context, file *AlbumFile) (*File, error) {
	var resp CopyFileResp
	_, err := d.Post(ALBUM_API_URL+"/copyfile", func(r *resty.Request) {
		r.SetContext(ctx)
		r.SetFormData(map[string]string{
			"album_id": file.AlbumID,
			"tid":      fmt.Sprint(file.Tid),
			"uk":       fmt.Sprint(file.Uk),
			"list":     fsidsFormatNotUk(file.Fsid),
		})
		r.SetResult(&resp)
	}, nil)
	if err != nil {
		return nil, err
	}
	return copyFile(file, &resp.List[0]), nil
}

// 加入相册
func (d *BaiduPhoto) JoinAlbum(ctx context.Context, code string) (*Album, error) {
	var resp InviteResp
	_, err := d.Get(ALBUM_API_URL+"/querypcode", func(req *resty.Request) {
		req.SetContext(ctx)
		req.SetQueryParams(map[string]string{
			"pcode": code,
			"web":   "1",
		})
	}, &resp)
	if err != nil {
		return nil, err
	}
	var resp2 JoinOrCreateAlbumResp
	_, err = d.Get(ALBUM_API_URL+"/join", func(req *resty.Request) {
		req.SetContext(ctx)
		req.SetQueryParams(map[string]string{
			"invite_code": resp.Pdata.InviteCode,
		})
	}, &resp2)
	if err != nil {
		return nil, err
	}
	return d.GetAlbumDetail(ctx, resp2.AlbumID)
}

// 获取相册详细信息
func (d *BaiduPhoto) GetAlbumDetail(ctx context.Context, albumID string) (*Album, error) {
	var album Album
	_, err := d.Get(ALBUM_API_URL+"/detail", func(req *resty.Request) {
		req.SetContext(ctx).SetResult(&album)
		req.SetQueryParams(map[string]string{
			"album_id": albumID,
		})
	}, &album)
	if err != nil {
		return nil, err
	}
	return &album, nil
}

func (d *BaiduPhoto) linkAlbum(ctx context.Context, file *AlbumFile, args model.LinkArgs) (*model.Link, error) {
	headers := map[string]string{
		"User-Agent": base.UserAgent,
	}
	if args.Header.Get("User-Agent") != "" {
		headers["User-Agent"] = args.Header.Get("User-Agent")
	}
	if !utils.IsLocalIPAddr(args.IP) {
		headers["X-Forwarded-For"] = args.IP
	}

	resp, err := d.Request(base.NoRedirectClient, ALBUM_API_URL+"/download", http.MethodHead, func(r *resty.Request) {
		r.SetContext(ctx)
		r.SetHeaders(headers)
		r.SetQueryParams(map[string]string{
			"fsid":     fmt.Sprint(file.Fsid),
			"album_id": file.AlbumID,
			"tid":      fmt.Sprint(file.Tid),
			"uk":       fmt.Sprint(file.Uk),
		})
	}, nil)

	if err != nil {
		return nil, err
	}

	if resp.StatusCode() != 302 {
		return nil, fmt.Errorf("not found 302 redirect")
	}

	location := resp.Header().Get("Location")

	link := &model.Link{
		URL: location,
		Header: http.Header{
			"User-Agent": []string{headers["User-Agent"]},
			"Referer":    []string{"https://photo.baidu.com/"},
		},
	}
	return link, nil
}

func (d *BaiduPhoto) linkFile(ctx context.Context, file *File, args model.LinkArgs) (*model.Link, error) {
	headers := map[string]string{
		"User-Agent": base.UserAgent,
	}
	if args.Header.Get("User-Agent") != "" {
		headers["User-Agent"] = args.Header.Get("User-Agent")
	}
	if !utils.IsLocalIPAddr(args.IP) {
		headers["X-Forwarded-For"] = args.IP
	}

	var downloadUrl struct {
		Dlink string `json:"dlink"`
	}
	_, err := d.Get(FILE_API_URL_V2+"/download", func(r *resty.Request) {
		r.SetContext(ctx)
		r.SetHeaders(headers)
		r.SetQueryParams(map[string]string{
			"fsid": fmt.Sprint(file.Fsid),
		})
	}, &downloadUrl)

	// resp, err := d.Request(base.NoRedirectClient, FILE_API_URL_V1+"/download", http.MethodHead, func(r *resty.Request) {
	// 	r.SetContext(ctx)
	// 	r.SetHeaders(headers)
	// 	r.SetQueryParams(map[string]string{
	// 		"fsid": fmt.Sprint(file.Fsid),
	// 	})
	// }, nil)

	if err != nil {
		return nil, err
	}

	// if resp.StatusCode() != 302 {
	// 	return nil, fmt.Errorf("not found 302 redirect")
	// }

	// location := resp.Header().Get("Location")
	link := &model.Link{
		URL: downloadUrl.Dlink,
		Header: http.Header{
			"User-Agent": []string{headers["User-Agent"]},
			"Referer":    []string{"https://photo.baidu.com/"},
		},
	}
	return link, nil
}

/*func (d *BaiduPhoto) linkStreamAlbum(ctx context.Context, file *AlbumFile) (*model.Link, error) {
	return &model.Link{
		Header: http.Header{},
		Writer: func(w io.Writer) error {
			res, err := d.Get(ALBUM_API_URL+"/streaming", func(r *resty.Request) {
				r.SetContext(ctx)
				r.SetQueryParams(map[string]string{
					"fsid":     fmt.Sprint(file.Fsid),
					"album_id": file.AlbumID,
					"tid":      fmt.Sprint(file.Tid),
					"uk":       fmt.Sprint(file.Uk),
				}).SetDoNotParseResponse(true)
			}, nil)
			if err != nil {
				return err
			}
			defer res.RawBody().Close()
			_, err = io.Copy(w, res.RawBody())
			return err
		},
	}, nil
}*/

/*func (d *BaiduPhoto) linkStream(ctx context.Context, file *File) (*model.Link, error) {
	return &model.Link{
		Header: http.Header{},
		Writer: func(w io.Writer) error {
			res, err := d.Get(FILE_API_URL_V1+"/streaming", func(r *resty.Request) {
				r.SetContext(ctx)
				r.SetQueryParams(map[string]string{
					"fsid": fmt.Sprint(file.Fsid),
				}).SetDoNotParseResponse(true)
			}, nil)
			if err != nil {
				return err
			}
			defer res.RawBody().Close()
			_, err = io.Copy(w, res.RawBody())
			return err
		},
	}, nil
}*/

// 获取uk
func (d *BaiduPhoto) uInfo() (*UInfo, error) {
	var info UInfo
	_, err := d.Get(USER_API_URL+"/getuinfo", func(req *resty.Request) {

	}, &info)
	if err != nil {
		return nil, err
	}
	return &info, nil
}

func DecryptMd5(encryptMd5 string) string {
	if _, err := hex.DecodeString(encryptMd5); err == nil {
		return encryptMd5
	}

	var out strings.Builder
	out.Grow(len(encryptMd5))
	for i, n := 0, int64(0); i < len(encryptMd5); i++ {
		if i == 9 {
			n = int64(unicode.ToLower(rune(encryptMd5[i])) - 'g')
		} else {
			n, _ = strconv.ParseInt(encryptMd5[i:i+1], 16, 64)
		}
		out.WriteString(strconv.FormatInt(n^int64(15&i), 16))
	}

	encryptMd5 = out.String()
	return encryptMd5[8:16] + encryptMd5[:8] + encryptMd5[24:32] + encryptMd5[16:24]
}

func EncryptMd5(originalMd5 string) string {
	reversed := originalMd5[8:16] + originalMd5[:8] + originalMd5[24:32] + originalMd5[16:24]

	var out strings.Builder
	out.Grow(len(reversed))
	for i, n := 0, int64(0); i < len(reversed); i++ {
		n, _ = strconv.ParseInt(reversed[i:i+1], 16, 64)
		n ^= int64(15 & i)
		if i == 9 {
			out.WriteRune(rune(n) + 'g')
		} else {
			out.WriteString(strconv.FormatInt(n, 16))
		}
	}
	return out.String()
}