/
main.go
58 lines (53 loc) · 1.31 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"bufio"
"flag"
"fmt"
"io"
"net/url"
"os"
"strings"
"github.com/Sirupsen/logrus"
)
func createYoutubeLink(src io.Reader) {
var ids []string
scanner := bufio.NewScanner(src)
for scanner.Scan() {
line := scanner.Text()
url, err := url.Parse(line)
if err != nil {
logrus.WithFields(logrus.Fields{"err": err, "line": line}).Error("This line is not a proper url")
continue
}
values := url.Query()
if id, ok := values["v"]; !ok {
logrus.WithField("line", line).Error("This line is not properly formed. Could not find id of video")
} else {
ids = append(ids, id[0])
}
}
if len(ids) <= 0 {
logrus.Error("Could not parse a single line to produce a youtube playlist")
os.Exit(-1)
} else {
var playListUrl = "https://www.youtube.com/watch_videos?video_ids=" + strings.Join(ids, ",")
fmt.Println("URL of the playlist: ", playListUrl)
}
}
func main() {
flag.Parse()
if flag.NArg() > 0 {
if _, err := os.Stat(flag.Arg(0)); err != nil {
logrus.WithField("err", err).Error("Error while opening youtube link list")
os.Exit(-1)
}
fh, err := os.Open(flag.Arg(0))
if err != nil {
logrus.WithFields(logrus.Fields{"err": err, "file": flag.Arg(0)}).Error("Could not open file")
os.Exit(-1)
}
createYoutubeLink(fh)
} else {
createYoutubeLink(os.Stdin)
}
}