Skip to content

Commit

Permalink
downloader: streams sorted by size, close #161
Browse files Browse the repository at this point in the history
  • Loading branch information
iawia002 committed Jul 12, 2018
1 parent b4faf3e commit 4498a12
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
20 changes: 18 additions & 2 deletions downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log"
"os"
"os/exec"
"sort"
"strings"
"time"

Expand All @@ -35,6 +36,7 @@ type FormatData struct {
URLs []URLData
Quality string
Size int64 // total size of all urls
name string
}

// VideoData data struct of video info
Expand Down Expand Up @@ -169,6 +171,12 @@ func printStream(k string, data FormatData) {
fmt.Println()
}

type formats []FormatData

func (f formats) Len() int { return len(f) }
func (f formats) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
func (f formats) Less(i, j int) bool { return f[i].Size > f[j].Size }

func (v VideoData) printInfo(format string) {
cyan := color.New(color.FgCyan)
fmt.Println()
Expand All @@ -181,8 +189,16 @@ func (v VideoData) printInfo(format string) {
if config.InfoOnly {
cyan.Printf(" Streams: ")
fmt.Println("# All available quality")
for k, data := range v.Formats {
printStream(k, data)
var sortedFormats formats
var k string
var data FormatData
for k, data = range v.Formats {
data.name = k
sortedFormats = append(sortedFormats, data)
}
sort.Sort(sortedFormats)
for _, data = range sortedFormats {
printStream(data.name, data)
}
} else {
cyan.Printf(" Stream: ")
Expand Down
11 changes: 9 additions & 2 deletions extractors/iqiyi.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,15 @@ func Iqiyi(url string) downloader.VideoData {
)
}
doc := parser.GetDoc(html)
title := strings.TrimSpace(doc.Find("h1>a").First().Text()) +
strings.TrimSpace(doc.Find("h1>span").First().Text())
title := strings.TrimSpace(doc.Find("h1>a").First().Text())
var sub string
for _, k := range []string{"span", "em"} {
if sub != "" {
break
}
sub = strings.TrimSpace(doc.Find("h1>" + k).First().Text())
}
title += sub
if title == "" {
title = doc.Find("title").Text()
}
Expand Down
17 changes: 4 additions & 13 deletions extractors/iqiyi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@ func TestIqiyi(t *testing.T) {
name string
args test.Args
}{
{
name: "normal test",
args: test.Args{
URL: "http://www.iqiyi.com/v_19rrbhikxo.html",
Title: "热血街舞团:鹿晗个人宣传片震撼发布 执着前行终现万丈荣光",
Size: 13843192,
Quality: "1280x720",
},
},
{
name: "normal test",
args: test.Args{
Expand All @@ -32,16 +23,16 @@ func TestIqiyi(t *testing.T) {
},
},
{
name: "normal test",
name: "title test 1",
args: test.Args{
URL: "http://www.iqiyi.com/a_19rrhcqtot.html#curid=958070800_e05591c8ad96022f79f41ec4fcc611a9",
Title: "热血街舞团》综艺专题-高清视频在线观看-爱奇艺",
URL: "http://www.iqiyi.com/v_19rrbhikxo.html",
Title: "热血街舞团:鹿晗个人宣传片震撼发布 执着前行终现万丈荣光",
Size: 13843192,
Quality: "1280x720",
},
},
{
name: "normal test",
name: "curid test 1",
args: test.Args{
URL: "http://www.iqiyi.com/a_19rrhcqtot.html#curid=958065800_03b77bd0648a6c1df86b0f7c4fd0e526",
Title: "《热血街舞团》综艺专题-高清视频在线观看-爱奇艺",
Expand Down

0 comments on commit 4498a12

Please sign in to comment.