feat: add modpack versions API endpoint
This commit is contained in:
@@ -84,8 +84,8 @@ func (i *CurseForgeImporter) getFileInfo(projectID, fileID int) (*CurseForgeFile
|
||||
return &fileInfo, nil
|
||||
}
|
||||
|
||||
// findModpackBySlug ищет ID проекта по его "слагу" (части URL).
|
||||
func (i *CurseForgeImporter) findModpackBySlug(slug string) (int, error) {
|
||||
// FindModpackBySlug ищет ID проекта по его "слагу" (части URL).
|
||||
func (i *CurseForgeImporter) FindModpackBySlug(slug string) (int, error) {
|
||||
apiURL := fmt.Sprintf("https://api.curseforge.com/v1/mods/search?gameId=432&slug=%s", url.QueryEscape(slug))
|
||||
req, err := http.NewRequestWithContext(context.Background(), "GET", apiURL, nil)
|
||||
if err != nil {
|
||||
@@ -113,30 +113,40 @@ func (i *CurseForgeImporter) findModpackBySlug(slug string) (int, error) {
|
||||
|
||||
// getLatestModpackFileURL находит URL для скачивания последнего файла проекта.
|
||||
func (i *CurseForgeImporter) getLatestModpackFileURL(projectID int) (string, error) {
|
||||
files, err := i.GetModpackFiles(projectID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if len(files) == 0 {
|
||||
return "", fmt.Errorf("no files found for projectID %d", projectID)
|
||||
}
|
||||
|
||||
latestFile := files[0]
|
||||
return latestFile.DownloadURL, nil
|
||||
}
|
||||
|
||||
// GetModpackFiles возвращает список файлов (версий) для проекта.
|
||||
func (i *CurseForgeImporter) GetModpackFiles(projectID int) ([]CurseForgeFileData, error) {
|
||||
apiURL := fmt.Sprintf("https://api.curseforge.com/v1/mods/%d/files", projectID)
|
||||
req, err := http.NewRequestWithContext(context.Background(), "GET", apiURL, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("x-api-key", i.APIKey)
|
||||
|
||||
resp, err := i.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var filesResp CurseForgeFilesResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&filesResp); err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(filesResp.Data) == 0 {
|
||||
return "", fmt.Errorf("no files found for projectID %d", projectID)
|
||||
}
|
||||
|
||||
latestFile := filesResp.Data[0]
|
||||
return latestFile.DownloadURL, nil
|
||||
return filesResp.Data, nil
|
||||
}
|
||||
|
||||
// DownloadModpackFromURL скачивает zip-архив модпака по URL страницы CurseForge.
|
||||
@@ -148,7 +158,7 @@ func (i *CurseForgeImporter) DownloadModpackFromURL(pageURL string) (string, err
|
||||
slug := path.Base(parsedURL.Path)
|
||||
log.Printf("Importer: Extracted slug '%s' from URL", slug)
|
||||
|
||||
projectID, err := i.findModpackBySlug(slug)
|
||||
projectID, err := i.FindModpackBySlug(slug)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -31,16 +31,22 @@ type CurseForgeSearchResponse struct {
|
||||
|
||||
// CurseForgeFilesResponse - ответ от эндпоинта получения файлов проекта
|
||||
type CurseForgeFilesResponse struct {
|
||||
Data []struct {
|
||||
ID int `json:"id"`
|
||||
FileName string `json:"fileName"`
|
||||
DownloadURL string `json:"downloadUrl"`
|
||||
} `json:"data"`
|
||||
Data []CurseForgeFileData `json:"data"`
|
||||
Pagination struct {
|
||||
TotalCount int `json:"totalCount"`
|
||||
} `json:"pagination"`
|
||||
}
|
||||
|
||||
// CurseForgeFileData - данные о файле
|
||||
type CurseForgeFileData struct {
|
||||
ID int `json:"id"`
|
||||
DisplayName string `json:"displayName"`
|
||||
FileName string `json:"fileName"`
|
||||
DownloadURL string `json:"downloadUrl"`
|
||||
FileDate string `json:"fileDate"`
|
||||
GameVersions []string `json:"gameVersions"`
|
||||
}
|
||||
|
||||
// CurseForgeFile представляет полную информацию о файле с API.
|
||||
type CurseForgeFile struct {
|
||||
Data struct {
|
||||
|
||||
Reference in New Issue
Block a user