Skip to content

Commit

Permalink
fix: download issue #26, file api changed
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikubill committed Dec 9, 2020
1 parent 3c025ea commit 5762d84
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 26 deletions.
71 changes: 46 additions & 25 deletions downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"fmt"
"github.com/cheggaaa/pb/v3"
"io"
"io/ioutil"
"log"
Expand All @@ -14,21 +13,28 @@ import (
"strconv"
"sync"
"time"

"github.com/cheggaaa/pb/v3"
)

const (
downloadDetails = "https://cowtransfer.com/transfer/transferdetail?url=%s&treceive=undefined&passcode=%s"
downloadFiles = "https://cowtransfer.com/transfer/files?page=0&guid=%s"
downloadConfig = "https://cowtransfer.com/transfer/download?guid=%s"
)

var regex = regexp.MustCompile("[0-9a-f]{14}")

type downloadDetailsResponse struct {
GUID string `json:"guid"`
DownloadName string `json:"downloadName"`
Deleted bool `json:"deleted"`
Uploaded bool `json:"uploaded"`
Details []downloadDetailsBlock `json:"transferFileDtos"`
GUID string `json:"guid"`
DownloadName string `json:"downloadName"`
Deleted bool `json:"deleted"`
Uploaded bool `json:"uploaded"`
// Details []downloadDetailsBlock `json:"transferFileDtos"`
}

type downloadFilesResponse struct {
Details []downloadDetailsBlock `json:"transferFileDtos"`
}

type downloadDetailsBlock struct {
Expand All @@ -52,29 +58,13 @@ func download(v string) error {
log.Println("step1 -> api/getGuid")
}
fmt.Printf("Remote: %s\n", v)
detailsURL := fmt.Sprintf(downloadDetails, fileID, runConfig.passCode)
//resp, _ := http.Get(detailsURL)
req, err := http.NewRequest("GET", detailsURL, nil)
if err != nil {
return fmt.Errorf("getDownloadDetails returns error: %s", err)
}
req.Header.Set("Referer", fmt.Sprintf("https://cowtransfer.com/s/%s", fileID))
req.Header.Set("Cookie", fmt.Sprintf("cf-cs-k-20181214=%d;", time.Now().UnixNano()))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("getDownloadDetails returns error: %s", err)
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("readDownloadDetails returns error: %s", err)
}

_ = resp.Body.Close()
body, err := fetchWithCookie(fmt.Sprintf(downloadDetails, fileID, runConfig.passCode), fileID)

if runConfig.debugMode {
log.Printf("returns: %v\n", string(body))
}

details := new(downloadDetailsResponse)
if err := json.Unmarshal(body, details); err != nil {
return fmt.Errorf("unmatshal DownloadDetails returns error: %s", err)
Expand All @@ -92,7 +82,18 @@ func download(v string) error {
return fmt.Errorf("link not finish upload yet")
}

for _, item := range details.Details {
body, err = fetchWithCookie(fmt.Sprintf(downloadFiles, details.GUID), fileID)

if runConfig.debugMode {
log.Printf("returns: %v\n", string(body))
}

files := new(downloadFilesResponse)
if err := json.Unmarshal(body, files); err != nil {
return fmt.Errorf("unmatshal DownloadDetails returns error: %s", err)
}

for _, item := range files.Details {
err = downloadItem(item)
if err != nil {
fmt.Println(err)
Expand All @@ -101,6 +102,26 @@ func download(v string) error {
return nil
}

func fetchWithCookie(link, fileID string) ([]byte, error) {
req, err := http.NewRequest("GET", link, nil)
if err != nil {
return nil, fmt.Errorf("getDownloadDetails returns error: %s", err)
}
req.Header.Set("Referer", fmt.Sprintf("https://cowtransfer.com/s/%s", fileID))
req.Header.Set("Cookie", fmt.Sprintf("cf-cs-k-20181214=%d;", time.Now().UnixNano()))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("getDownloadDetails returns error: %s", err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("readDownloadDetails returns error: %s", err)
}

_ = resp.Body.Close()
return body, nil
}

func downloadItem(item downloadDetailsBlock) error {
if runConfig.debugMode {
log.Println("step2 -> api/getConf")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ require (
github.com/jessevdk/go-flags v1.4.0 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/orcaman/concurrent-map v0.0.0-20190826125027-8c72a8bb44f6
golang.org/x/sys v0.0.0-20201110211018-35f3e6cf4a65 // indirect
golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ golang.org/x/sys v0.0.0-20200406155108-e3b113bbe6a4 h1:c1Sgqkh8v6ZxafNGG64r8C8Ui
golang.org/x/sys v0.0.0-20200406155108-e3b113bbe6a4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201110211018-35f3e6cf4a65 h1:Qo9oJ566/Sq7N4hrGftVXs8GI2CXBCuOd4S2wHE/e0M=
golang.org/x/sys v0.0.0-20201110211018-35f3e6cf4a65/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d h1:MiWWjyhUzZ+jvhZvloX6ZrUsdEghn8a64Upd8EMHglE=
golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

0 comments on commit 5762d84

Please sign in to comment.