Skip to content

Commit

Permalink
Allows users to specify filename now & fixed a bug
Browse files Browse the repository at this point in the history
  • Loading branch information
lukecold committed Mar 7, 2016
1 parent e4af796 commit f254a9b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 33 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ Specify a video repository using absolute path (If you are a Windows user, I hig
```
gotube -d -id C0DPdy98e4c -q medium -ext video/mp4 -rep /Users/yourusername/Documents/videos
```
Specify the video filename:
```
gotube -d -id C0DPdy98e4c -f test.mp4 -q medium -ext video/mp4 -rep ./videos
```
Try search by keywords and see what would return:
```
gotube -l -s "curry highlights"
Expand Down
2 changes: 1 addition & 1 deletion api/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func GetVideoIdsFromSearch(searchUrl string) (idList []string, err error) {
return
}
//Retrive id list
idBeg := []byte("class=\"yt-lockup yt-lockup-tile yt-lockup-video vve-check clearfix yt-uix-tile\" data-context-item-id=\"")
idBeg := []byte("class=\"yt-lockup yt-lockup-tile yt-lockup-video vve-check clearfix\" data-context-item-id=\"")
beg := 0
for {
//Find the index of begin pattern
Expand Down
4 changes: 1 addition & 3 deletions api/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gotube

import (
"errors"
"net/url"
"strconv"
. "strings"
)
Expand Down Expand Up @@ -59,8 +58,7 @@ func GetSearchUrl(keywords string, pageNum int) (searchUrl string, err error) {
return r
}, keywords)
//Escape keyword to safely put into url
keywords = url.QueryEscape(keywords)
searchUrl = "https://www.youtube.com/results?search_query=" + keywords
searchUrl = "https://www.youtube.com/results?q=" + keywords
//Make sure page number is valid
switch {
case pageNum < 1:
Expand Down
43 changes: 23 additions & 20 deletions api/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"strconv"
. "strings"
"unicode/utf8"
)

/*
Expand All @@ -22,8 +23,8 @@ type Video struct {
* that shared the same YouTube url.
*/
type VideoList struct {
Title string
Videos []Video
Title string
Videos []Video
}

/*
Expand All @@ -44,10 +45,11 @@ func (video *Video) FindMissingFields() (missingFields []string) {
}

/*
* Download this video into the repository,
* if repository is not generated, download to current folder.
* Download this video into the repository, with specified name
* if repository is not generated, download to current folder,
* if name is not given, use video's name + extension
*/
func (video *Video) Download(rep string) error {
func (video *Video) Download(rep string, filename string) error {
//Get video from url
body, err := GetHttpFromUrl(video.url)
if err != nil {
Expand All @@ -61,18 +63,20 @@ func (video *Video) Download(rep string) error {
}
}

filename := video.Title + video.extension
//Make sure there is no invalid characters in filename
filename = Map(
func(r rune) rune {
switch r {
case '/', '\\':
r = '.'
case ':', '*', '?', '"', '<', '>', '|':
r = '-'
}
return r
}, filename)
if utf8.RuneCountInString(filename) <= 0 {
filename = video.Title + video.extension
//Make sure there is no invalid characters in filename
filename = Map(
func(r rune) rune {
switch r {
case '/', '\\':
r = '.'
case ':', '*', '?', '"', '<', '>', '|':
r = '-'
}
return r
}, filename)
}
filename = rep + filename
file, err := os.Create(filename)
if err != nil {
Expand All @@ -98,12 +102,11 @@ func (vl *VideoList) Append(v Video) {
* Filter the list first by the given key words,
* then download the first video in the list
*/
func (vl *VideoList) Download(rep string, quality, extension string) (err error) {
func (vl *VideoList) Download(rep, filename, quality, extension string) (err error) {
vl.Filter(quality, extension)

//No matter how many left, pick the first one
video := vl.Videos[0]
err = video.Download(rep)
err = video.Download(rep, filename)
return err
}

Expand Down
22 changes: 13 additions & 9 deletions script.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func main() {
k := flag.Int("k", 1, "return top k results, only valid with key word searching")
rep := flag.String("-videorepository", "", "(optional) repository to store videos")
flag.StringVar(rep, "rep", "", "(optional) repository to store videos")
filename := flag.String("-filename", "", "(optional) filename of video - only valid when downloading one video")
flag.StringVar(filename, "f", "", "(optional) filename of video - only valid when downloading one video")
quality := flag.String("-quality", "", "(optional) video quality. e.g. medium")
flag.StringVar(quality, "q", "", "(optional) video quality. e.g. medium")
extension := flag.String("-extension", "", "(optional) video extension. e.g. video/mp4, video/flv, video/webm")
Expand Down Expand Up @@ -81,17 +83,18 @@ func main() {
}
//Waiting group is used to prevent main thread ending before child threads end
wg := new(sync.WaitGroup)
wg.Add(len(ids))
if *isDownload {
wg.Add(len(ids))
}
//Channel is used to control the maximum threads
end := make(chan bool, MaxParallelism())
//end := make(chan bool, MaxParallelism())
for idx, vid := range ids {
vl, err = GetVideoListFromId(vid)
if err != nil {
log.Fatal(err)
}
if *isDownload {
go Exec(vl, *isDownload, *isRetList, *rep, *quality, *extension, wg, end)
end <- true
go Exec(vl, *isDownload, *isRetList, *rep, *filename, *quality, *extension, wg)
} else {
fmt.Printf("%v. %v\n", idx+1, vl.Title)
}
Expand All @@ -108,23 +111,23 @@ func main() {
//dummy variables
wg := new(sync.WaitGroup)
wg.Add(1)
var end chan bool
Exec(vl, *isDownload, *isRetList, *rep, *quality, *extension, wg, end)
Exec(vl, *isDownload, *isRetList, *rep, *filename, *quality, *extension, wg)
wg.Wait()
return
}

/*
* Choose either downloading or retrieving video list
*/
func Exec(vl VideoList, isDownload, isRetList bool, rep, quality, extension string, wg *sync.WaitGroup, end chan bool) {
func Exec(vl VideoList, isDownload, isRetList bool, rep, filename, quality, extension string, wg *sync.WaitGroup) {
//Set up synchronization function
defer func() {
<-end
wg.Done()
}()

if isDownload {
fmt.Printf("Downloading %v...\n", vl.Title)
err := vl.Download(rep, quality, extension)
err := vl.Download(rep, filename, quality, extension)
if err != nil {
log.Fatal(err)
}
Expand All @@ -136,6 +139,7 @@ func Exec(vl VideoList, isDownload, isRetList bool, rep, quality, extension stri
}
fmt.Println(vl)
}
return
}

/*
Expand Down

0 comments on commit f254a9b

Please sign in to comment.