func (self *PiDownloader) getAria2GlobalStat(args []string) (string, error) { globalStat, err := aria2rpc.GetGlobalStat() if err != nil { return "", err } speed := utils.FormatSizeString(globalStat["downloadSpeed"].(string)) numActive := globalStat["numActive"].(string) numStopped := globalStat["numStopped"].(string) numWaiting := globalStat["numWaiting"].(string) return fmt.Sprintf("spd:%s ; act:%s ; wait:%s ; stop:%s", speed, numActive, numWaiting, numStopped), nil }
func (self *PiDownloader) formatOutput(tasks []map[string]interface{}) (string, error) { if tasks == nil || len(tasks) == 0 { return "no records", nil } var buffer bytes.Buffer buffer.WriteString("\n") for _, task := range tasks { gid := task["gid"].(string) buffer.WriteString(fmt.Sprintf("gid: %s\n", gid)) title := self.getTitle(task) buffer.WriteString(fmt.Sprintf("title: %s\n", title)) dSpd := task["downloadSpeed"] if dSpd != nil { speed := utils.FormatSizeString(dSpd.(string)) buffer.WriteString(fmt.Sprintf("spd: %s\n", speed)) } total, _ := strconv.ParseFloat(task["totalLength"].(string), 64) totalFmt := utils.FormatSize(int64(total)) buffer.WriteString(fmt.Sprintf("total: %s\n", totalFmt)) completed, _ := strconv.ParseFloat(task["completedLength"].(string), 64) buffer.WriteString(fmt.Sprintf("prog: %.2f%%\n", completed*100/total)) if dSpd != nil { spd, _ := strconv.Atoi(dSpd.(string)) var timeLeftFmt string if spd == 0 { timeLeftFmt = "N/A" } else { timeLeft := int64(total-completed) / int64(spd) timeLeftFmt = utils.FormatTime(timeLeft) } buffer.WriteString(fmt.Sprintf("tiemleft: %s\n", timeLeftFmt)) } status := task["status"] if status != nil { buffer.WriteString(fmt.Sprintf("status: %s\n", status.(string))) } errorCode := task["errorCode"] if status != nil { buffer.WriteString(fmt.Sprintf("statusCode: %s\n", errorCode.(string))) } buffer.WriteString("==================\n") } return buffer.String(), nil }
func (self *PiDownloader) getCurrentDownloadInfo() (string, error) { globalStat, getStatErr := aria2rpc.GetGlobalStat() if getStatErr != nil { return "", getStatErr } // total active download task numActive, _ := strconv.Atoi(globalStat["numActive"].(string)) if numActive == 0 { return "no active task", nil } // total speed speed := utils.FormatSizeString(globalStat["downloadSpeed"].(string)) // the longest left time in active download task var longestTimeLeft int64 = -1 keys := []string{"gid", "totalLength", "completedLength", "downloadSpeed"} tasks, getActErr := aria2rpc.GetActive(keys) if getActErr != nil { return "", getActErr } for _, task := range tasks { dSpd := task["downloadSpeed"] if dSpd != nil { total, _ := strconv.ParseInt(task["totalLength"].(string), 10, 64) completed, _ := strconv.ParseInt(task["completedLength"].(string), 10, 64) spd, _ := strconv.ParseInt(dSpd.(string), 10, 64) if spd > 0 { timeLeft := (total - completed) / spd if timeLeft > longestTimeLeft { longestTimeLeft = timeLeft } } } } var timeLeftFmt string if longestTimeLeft > 0 { timeLeftFmt = utils.FormatTime(longestTimeLeft) } else { timeLeftFmt = "N/A" } return fmt.Sprintf("spd: %s ; act: %d ; left: %s", speed, numActive, timeLeftFmt), nil }