Skip to content

Commit

Permalink
main: Specify the download range of the list
Browse files Browse the repository at this point in the history
  • Loading branch information
iawia002 committed May 24, 2018
1 parent bf8fe96 commit b3a7c4e
Show file tree
Hide file tree
Showing 10 changed files with 329 additions and 91 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -3,7 +3,6 @@ language: go
sudo: false

go:
- "1.9"
- "1.10"

script:
Expand Down
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -169,6 +169,17 @@ $ annie -i -p https://www.bilibili.com/bangumi/play/ep198061
......
```

You can use the `-start`, `-end` or `-items` option to specify the download range of the list:

```
-start
Playlist video to start at (default 1)
-end
Playlist video to end at (default is last)
-items
Playlist video items to download. Separated by commas like: 1,5,6
```

### Multiple inputs

You can also download multiple URLs at once:
Expand Down Expand Up @@ -397,9 +408,13 @@ Usage of annie:
-c string
Cookie
-d Debug mode
-end int
Playlist video to end at
-f string
Select specific format to download
-i Information only
-items string
Playlist video items to download. Separated by commas like: 1,5,6
-j Print extracted data
-n int
The number of download thread (default 10)
Expand All @@ -410,6 +425,8 @@ Usage of annie:
Use specified Referrer
-s string
SOCKS5 proxy
-start int
Playlist video to start at (default 1)
-v Show version
-x string
HTTP proxy
Expand Down
6 changes: 6 additions & 0 deletions config/config.go
Expand Up @@ -29,6 +29,12 @@ var (
ThreadNumber int
// File URLs file
File string
// PlaylistStart Playlist video to start at
PlaylistStart int
// PlaylistEnd Playlist video to end at
PlaylistEnd int
// PlaylistItems Playlist video items to download. Separated by commas like: 1,5,6
PlaylistItems string
)

// FakeHeaders fake http headers
Expand Down
12 changes: 10 additions & 2 deletions extractors/bilibili/bilibili.go
Expand Up @@ -156,7 +156,11 @@ func Download(url string) {
dataString := utils.MatchOneOf(html, `window.__INITIAL_STATE__=(.+?);`)[1]
var data bangumiData
json.Unmarshal([]byte(dataString), &data)
for _, u := range data.EpList {
needDownloadItems := utils.NeedDownloadList(len(data.EpList))
for index, u := range data.EpList {
if !utils.IntInSlice(index+1, needDownloadItems) {
continue
}
bilibiliDownload(
fmt.Sprintf("https://www.bilibili.com/bangumi/play/ep%d", u.EpID), options,
)
Expand All @@ -170,7 +174,11 @@ func Download(url string) {
return
}
// https://www.bilibili.com/video/av20827366/?p=1
for _, u := range data.VideoData.Pages {
needDownloadItems := utils.NeedDownloadList(len(data.VideoData.Pages))
for index, u := range data.VideoData.Pages {
if !utils.IntInSlice(index+1, needDownloadItems) {
continue
}
options.Aid = data.Aid
options.Cid = strconv.Itoa(u.Cid)
options.Subtitle = u.Part
Expand Down
6 changes: 5 additions & 1 deletion extractors/youtube/youtube.go
Expand Up @@ -73,7 +73,11 @@ func Download(uri string) {
html := request.Get("https://www.youtube.com/playlist?list="+listID, referer, nil)
// "videoId":"OQxX8zgyzuM","thumbnail"
videoIDs := utils.MatchAll(html, `"videoId":"([^,]+?)","thumbnail"`)
for _, videoID := range videoIDs {
needDownloadItems := utils.NeedDownloadList(len(videoIDs))
for index, videoID := range videoIDs {
if !utils.IntInSlice(index+1, needDownloadItems) {
continue
}
u := fmt.Sprintf(
"https://www.youtube.com/watch?v=%s&list=%s", videoID[1], listID,
)
Expand Down
6 changes: 6 additions & 0 deletions main.go
Expand Up @@ -30,6 +30,12 @@ func init() {
flag.BoolVar(&config.ExtractedData, "j", false, "Print extracted data")
flag.IntVar(&config.ThreadNumber, "n", 10, "The number of download thread")
flag.StringVar(&config.File, "F", "", "URLs file")
flag.IntVar(&config.PlaylistStart, "start", 1, "Playlist video to start at")
flag.IntVar(&config.PlaylistEnd, "end", 0, "Playlist video to end at")
flag.StringVar(
&config.PlaylistItems, "items", "",
"Playlist video items to download. Separated by commas like: 1,5,6",
)
}

func download(videoURL string) {
Expand Down
50 changes: 50 additions & 0 deletions utils/download.go
@@ -0,0 +1,50 @@
package utils

import (
"strconv"
"strings"

"github.com/iawia002/annie/config"
)

// ShouldExtract returns true, if we need to extract this format
func ShouldExtract(format, bestQuality string) bool {
extractAll := config.InfoOnly || config.ExtractedData
if extractAll {
return true
}
if config.Format != "" {
if format != config.Format {
return false
}
} else if format != bestQuality {
return false
}
return true
}

// NeedDownloadList return the indices of playlist that need download
func NeedDownloadList(length int) []int {
if config.PlaylistItems != "" {
var items []int
var index int
temp := strings.Split(config.PlaylistItems, ",")
for _, i := range temp {
index, _ = strconv.Atoi(i)
items = append(items, index)
}
return items
}
start := config.PlaylistStart
end := config.PlaylistEnd
if config.PlaylistStart < 1 {
start = 1
}
if end == 0 {
end = length
}
if end < start {
end = start
}
return Range(start, end)
}
153 changes: 153 additions & 0 deletions utils/download_test.go
@@ -0,0 +1,153 @@
package utils

import (
"testing"

"github.com/iawia002/annie/config"
)

func TestShouldExtract(t *testing.T) {
type args struct {
format string
bestQuality string
}
tests := []struct {
name string
args args
want bool
infoOnly bool
extractedData bool
format string
}{
{
name: "InfoOnly test",
args: args{
format: "1",
bestQuality: "2",
},
want: true,
infoOnly: true,
},
{
name: "ExtractedData test",
args: args{
format: "1",
bestQuality: "2",
},
want: true,
extractedData: true,
},
{
name: "Format test",
args: args{
format: "bd",
bestQuality: "bd2",
},
want: true,
format: "bd",
},
{
name: "Format test2",
args: args{
format: "bd2",
bestQuality: "bd2",
},
want: false,
format: "bd",
},
{
name: "bestQuality test",
args: args{
format: "bd2",
bestQuality: "bd2",
},
want: true,
},
{
name: "bestQuality test2",
args: args{
format: "bd",
bestQuality: "bd2",
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config.ExtractedData = tt.extractedData
config.InfoOnly = tt.infoOnly
config.Format = tt.format
if got := ShouldExtract(tt.args.format, tt.args.bestQuality); got != tt.want {
t.Errorf("ShouldExtract() = %v, want %v", got, tt.want)
}
})
}
}

func TestNeedDownloadList(t *testing.T) {
type args struct {
len int
}
tests := []struct {
name string
args args
want int
start int
end int
items string
}{
{
name: "start end test 1",
args: args{
len: 3,
},
start: 2,
end: 2,
want: 2,
},
{
name: "start end test 2",
args: args{
len: 3,
},
end: 2,
want: 1,
},
{
name: "start end test 3",
args: args{
len: 3,
},
start: 2,
end: 0,
want: 2,
},
{
name: "start end test 4",
args: args{
len: 3,
},
start: 2,
end: 1,
want: 2,
},
{
name: "items test",
args: args{
len: 3,
},
items: "1, 3",
want: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config.PlaylistStart = tt.start
config.PlaylistEnd = tt.end
config.PlaylistItems = tt.items
if got := NeedDownloadList(tt.args.len); got[0] != tt.want {
t.Errorf("NeedDownloadList() = %v, want %v", got, tt.want)
}
})
}
}
35 changes: 19 additions & 16 deletions utils/utils.go
Expand Up @@ -119,6 +119,16 @@ func StringInSlice(str string, list []string) bool {
return false
}

// IntInSlice if a number is in the list
func IntInSlice(i int, list []int) bool {
for _, a := range list {
if a == i {
return true
}
}
return false
}

// GetNameAndExt return the name and ext of the URL
// https://img9.bcyimg.com/drawer/15294/post/1799t/1f5a87801a0711e898b12b640777720f.jpg ->
// 1f5a87801a0711e898b12b640777720f, jpg
Expand Down Expand Up @@ -174,22 +184,6 @@ func PrintVersion() {
)
}

// ShouldExtract returns true, if we need to extract this format
func ShouldExtract(format, bestQuality string) bool {
extractAll := config.InfoOnly || config.ExtractedData
if extractAll {
return true
}
if config.Format != "" {
if format != config.Format {
return false
}
} else if format != bestQuality {
return false
}
return true
}

// Reverse Reverse a string
func Reverse(s string) string {
runes := []rune(s)
Expand All @@ -198,3 +192,12 @@ func Reverse(s string) string {
}
return string(runes)
}

// Range generate a sequence of numbers by range
func Range(min, max int) []int {
items := make([]int, max-min+1)
for index := range items {
items[index] = min + index
}
return items
}

0 comments on commit b3a7c4e

Please sign in to comment.