Skip to content
This repository has been archived by the owner on Feb 10, 2023. It is now read-only.

Commit

Permalink
save cookie for downloaded files
Browse files Browse the repository at this point in the history
  • Loading branch information
SoMuchForSubtlety committed Jun 8, 2019
1 parent 04d70bb commit d14d617
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
22 changes: 13 additions & 9 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ import (
)

// takes asset ID and downloads corresponding .m3u8
func downloadAsset(url string, title string) (string, error) {
func downloadAsset(url string, title string) (filepath string, cookie string, err error) {
// sanitize title
title = strings.Replace(title, ":", "", -1)
// abort if the URL is not valid
if len(url) < 10 {
return "", errors.New("invalid url")
return "", "", errors.New("invalid url")
}
// download and patch .m3u8 file
data, err := downloadData(url)
data, cookie, err := downloadData(url)
if err != nil {
return "", err
return "", "", err
}
fixedLineArray := fixData(data, url)
path, err := writeToFile(fixedLineArray, title+".m3u8")
if err != nil {
return "", err
return "", "", err
}
return strings.Replace(path, " ", "\x20", -1), nil
return strings.Replace(path, " ", "\x20", -1), cookie, nil
}

// returns valid m3u8 URL as string
Expand Down Expand Up @@ -93,19 +93,23 @@ func getPlayableURL(assetID string) (string, error) {
}

// downloads m3u8 data and returns it as slice
func downloadData(url string) ([]string, error) {
func downloadData(url string) (lines []string, cookie string, err error) {
// Get the data
resp, err := http.Get(url)
if err != nil {
return nil, err
return nil, "", err
}
defer resp.Body.Close()

for _, c := range resp.Cookies() {
cookie += fmt.Sprint(c)
}

// convert body to string array
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
lineArray := strings.Split(buf.String(), "\n")
return lineArray, nil
return lineArray, cookie, nil
}

// chage URIs in m3u8 data to full URLs
Expand Down
14 changes: 6 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,7 @@ func getYearAndRace(input string) (string, string, error) {

// prints to debug window
func debugPrint(i interface{}) {
var output string
switch v := i.(type) {
default:
output = fmt.Sprintf("%v", v)
}
output := fmt.Sprintf("%v", i)
if debugText != nil {
fmt.Fprintf(debugText, output+"\n")
debugText.ScrollToEnd()
Expand Down Expand Up @@ -386,7 +382,7 @@ func nodeSelected(node *tview.TreeNode) {
debugPrint(err.Error())
return
}
_, err = downloadAsset(url, urlAndTitle[1])
_, _, err = downloadAsset(url, urlAndTitle[1])
if err != nil {
debugPrint(err.Error())
}
Expand Down Expand Up @@ -429,23 +425,25 @@ func runCustomCommand(cc commandContext, node *tview.TreeNode) error {
return err
}
var filepath string
var cookie string
// run every command
for j := range com.Commands {
if len(com.Commands[j]) == 0 {
continue
}
tmpCommand := make([]string, len(com.Commands[j]))
copy(tmpCommand, com.Commands[j])
// replace $url and $file
// replace $url, $file and $cookie
for x, s := range tmpCommand {
tmpCommand[x] = s
if strings.Contains(s, "$file") && filepath == "" {
filepath, err = downloadAsset(url, cc.Title)
filepath, cookie, err = downloadAsset(url, cc.Title)
if err != nil {
return err
}
}
tmpCommand[x] = strings.Replace(tmpCommand[x], "$file", filepath, -1)
tmpCommand[x] = strings.Replace(tmpCommand[x], "$cookie", cookie, -1)
tmpCommand[x] = strings.Replace(tmpCommand[x], "$url", url, -1)
}
// run command
Expand Down
2 changes: 1 addition & 1 deletion update.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func updateAvailable() (bool, error) {
if err != nil {
return false, err
}
checksums, err := downloadData(url)
checksums, _, err := downloadData(url)
if err != nil {
return false, err
}
Expand Down

0 comments on commit d14d617

Please # to comment.