Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加PikPak网盘的离线下载功能 #6632

Open
4 tasks done
Muione opened this issue Jun 18, 2024 · 1 comment · May be fixed by #6648
Open
4 tasks done

增加PikPak网盘的离线下载功能 #6632

Muione opened this issue Jun 18, 2024 · 1 comment · May be fixed by #6648
Labels
enhancement New feature or request

Comments

@Muione
Copy link

Muione commented Jun 18, 2024

Please make sure of the following things

  • I have read the documentation.
  • I'm sure there are no duplicate issues or discussions.
  • I'm sure this feature is not implemented.
  • I'm sure it's a reasonable and popular requirement.

Description of the feature / 需求描述

因为PikPak网盘离线下载的速度比较快,能不能在/api/fs/add_offline_download API下增加对PikPak离线下载的支持

Suggested solution / 实现思路

这段代码可以通过PikPak离线下载任务的接口实现(可能有些小问题):

// 离线下载文件
func (d *PikPak) OfflineDownload(ctx context.Context, fileUrl string, parentId string, name string) error {
	requestBody := base.Json{
		"kind":        "drive#file",
		"name":        name,
		"upload_type": "UPLOAD_TYPE_URL",
		"url": base.Json{
			"url": fileUrl,
		},
		"folder_type": "DOWNLOAD",
	}
	if parentId != "" {
		requestBody["folder_type"] = ""
		requestBody["parent_id"] = parentId
	}
	_, err := d.request("https://api-drive.mypikpak.com/drive/v1/files", http.MethodPost, func(req *resty.Request) {
		req.SetBody(requestBody)
	}, nil)
	return err
}

// OfflineList 获取离线下载列表
func (d *PikPak) OfflineList(ctx context.Context, size int, nextPageToken string, phase []string) (*OfflineListResp, error) {
	if size <= 0 {
		size = 10000
	}
	url := "https://api-drive.mypikpak.com/drive/v1/tasks"
	data := map[string]interface{}{
		"type":            "offline",
		"thumbnail_size": "SIZE_SMALL",
		"limit":           size,
		"filters":         fmt.Sprintf(`{"phase": {"in": "%s"}}`, strings.Join(phase, ",")),
		"with":            "reference_resource",
	}
	if nextPageToken != "" {
		data["page_token"] = nextPageToken
	}
	var resp OfflineListResp
	_, err := d.request(url, http.MethodGet, func(req *resty.Request) {
		req.SetQueryParams(data)
	}, &resp)
	return &resp, err
}

// GetTaskStatus 获取离线下载任务状态
func (d *PikPak) GetTaskStatus(ctx context.Context, taskId string, fileId string) (DownloadStatus, error) {
	// 首先尝试从任务列表中查找
	listResp, err := d.OfflineList(ctx, 10000, "", []string{"PHASE_TYPE_RUNNING", "PHASE_TYPE_ERROR"})
	if err != nil {
		return DownloadStatusError, err
	}
	for _, task := range listResp.Tasks {
		if task.ID == taskId {
			return DownloadStatusDownloading, nil
		}
	}

	// 如果在任务列表中找不到,则尝试获取文件信息
	fileInfo, err := d.offlineFileInfo(fileId)
	if err != nil {
		if err.(*resty.Response).StatusCode() == http.StatusNotFound {
			return DownloadStatusNotFound, nil
		}
		return DownloadStatusError, err
	}
	if fileInfo != nil {
		return DownloadStatusDone, nil
	}

	return DownloadStatusNotFound, nil
}

// offlineFileInfo 获取离线下载文件信息
func (d *PikPak) offlineFileInfo(fileId string) (*File, error) {
	url := fmt.Sprintf("https://api-drive.mypikpak.com/drive/v1/files/%s", fileId)
	var resp File
	_, err := d.request(url, http.MethodGet, func(req *resty.Request) {
		req.SetQueryParam("thumbnail_size", "SIZE_LARGE")
	}, &resp)
	if err != nil {
		return nil, err
	}
	return &resp, nil
}

// DownloadStatus 表示下载状态
type DownloadStatus string

const (
	DownloadStatusDownloading DownloadStatus = "Downloading"
	DownloadStatusDone        DownloadStatus = "Done"
	DownloadStatusNotFound    DownloadStatus = "NotFound"
	DownloadStatusError       DownloadStatus = "Error"
)

// OfflineListResp 离线下载列表响应
type OfflineListResp struct {
	Tasks []struct {
		ID string `json:"id"`
	} `json:"tasks"`
}

Additional context / 附件

No response

@Muione Muione added the enhancement New feature or request label Jun 18, 2024
Copy link

welcome bot commented Jun 18, 2024

Thanks for opening your first issue here! Be sure to follow the issue template!

@Muione Muione linked a pull request Jun 22, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant