File cmd/channeldump.go changed (mode: 100644) (index b1b843c..4a1ebf0) |
... |
... |
package cmd |
3 |
3 |
import ( |
import ( |
4 |
4 |
"github.com/spf13/cobra" |
"github.com/spf13/cobra" |
5 |
5 |
"net/url" |
"net/url" |
6 |
|
"fmt" |
|
7 |
6 |
"os" |
"os" |
8 |
7 |
"strings" |
"strings" |
9 |
8 |
"time" |
"time" |
10 |
9 |
"bufio" |
"bufio" |
11 |
10 |
"log" |
"log" |
12 |
11 |
"github.com/terorie/yt-mango/api" |
"github.com/terorie/yt-mango/api" |
|
12 |
|
"fmt" |
13 |
13 |
) |
) |
14 |
14 |
|
|
15 |
15 |
var channelDumpCmd = cobra.Command{ |
var channelDumpCmd = cobra.Command{ |
16 |
|
Use: "dumpurls <channel ID> <file>", |
|
|
16 |
|
Use: "dumpurls <channel ID> [file]", |
17 |
17 |
Short: "Get all public video URLs from channel", |
Short: "Get all public video URLs from channel", |
18 |
18 |
Long: "Write all videos URLs of a channel to a file", |
Long: "Write all videos URLs of a channel to a file", |
19 |
|
Args: cobra.ExactArgs(2), |
|
|
19 |
|
Args: cobra.RangeArgs(1, 2), |
20 |
20 |
Run: func(cmd *cobra.Command, args []string) { |
Run: func(cmd *cobra.Command, args []string) { |
|
21 |
|
printResults := false |
|
22 |
|
fileName := "" |
21 |
23 |
channelID := args[0] |
channelID := args[0] |
22 |
|
fileName := args[1] |
|
|
24 |
|
if len(args) != 2 { |
|
25 |
|
printResults = true |
|
26 |
|
} else { |
|
27 |
|
fileName = args[1] |
|
28 |
|
} |
23 |
29 |
|
|
24 |
30 |
if !matchChannelID.MatchString(channelID) { |
if !matchChannelID.MatchString(channelID) { |
25 |
31 |
// Check if youtube.com domain |
// Check if youtube.com domain |
26 |
32 |
_url, err := url.Parse(channelID) |
_url, err := url.Parse(channelID) |
27 |
33 |
if err != nil || (_url.Host != "www.youtube.com" && _url.Host != "youtube.com") { |
if err != nil || (_url.Host != "www.youtube.com" && _url.Host != "youtube.com") { |
28 |
|
fmt.Fprintln(os.Stderr, "Not a channel ID:", channelID) |
|
|
34 |
|
log.Fatal("Not a channel ID:", channelID) |
29 |
35 |
os.Exit(1) |
os.Exit(1) |
30 |
36 |
} |
} |
31 |
37 |
|
|
32 |
38 |
// Check if old /user/ URL |
// Check if old /user/ URL |
33 |
39 |
if strings.HasPrefix(_url.Path, "/user/") { |
if strings.HasPrefix(_url.Path, "/user/") { |
34 |
40 |
// TODO Implement extraction of channel ID |
// TODO Implement extraction of channel ID |
35 |
|
fmt.Fprintln(os.Stderr, "New /channel/ link is required!\n" + |
|
|
41 |
|
log.Fatal("New /channel/ link is required!\n" + |
36 |
42 |
"The old /user/ links do not work.") |
"The old /user/ links do not work.") |
37 |
43 |
os.Exit(1) |
os.Exit(1) |
38 |
44 |
} |
} |
|
... |
... |
var channelDumpCmd = cobra.Command{ |
41 |
47 |
channelID = strings.TrimPrefix(_url.Path, "/channel/") |
channelID = strings.TrimPrefix(_url.Path, "/channel/") |
42 |
48 |
if len(channelID) == len(_url.Path) { |
if len(channelID) == len(_url.Path) { |
43 |
49 |
// No such prefix to be removed |
// No such prefix to be removed |
44 |
|
fmt.Fprintln(os.Stderr, "Not a channel ID:", channelID) |
|
|
50 |
|
log.Fatal("Not a channel ID:", channelID) |
45 |
51 |
os.Exit(1) |
os.Exit(1) |
46 |
52 |
} |
} |
47 |
53 |
|
|
|
... |
... |
var channelDumpCmd = cobra.Command{ |
62 |
68 |
flags = os.O_WRONLY | os.O_CREATE | os.O_EXCL |
flags = os.O_WRONLY | os.O_CREATE | os.O_EXCL |
63 |
69 |
} |
} |
64 |
70 |
|
|
65 |
|
file, err := os.OpenFile(fileName, flags, 0640) |
|
66 |
|
defer file.Close() |
|
67 |
|
writer := bufio.NewWriter(file) |
|
68 |
|
defer writer.Flush() |
|
|
71 |
|
var file *os.File |
|
72 |
|
var writer *bufio.Writer |
|
73 |
|
|
|
74 |
|
if !printResults { |
|
75 |
|
var err error |
|
76 |
|
file, err = os.OpenFile(fileName, flags, 0640) |
|
77 |
|
if err != nil { |
|
78 |
|
log.Fatal(err) |
|
79 |
|
os.Exit(1) |
|
80 |
|
} |
|
81 |
|
defer file.Close() |
69 |
82 |
|
|
70 |
|
if err != nil { |
|
71 |
|
fmt.Fprintln(os.Stderr, err) |
|
72 |
|
os.Exit(1) |
|
|
83 |
|
writer = bufio.NewWriter(file) |
|
84 |
|
defer writer.Flush() |
73 |
85 |
} |
} |
74 |
86 |
|
|
75 |
87 |
totalURLs := 0 |
totalURLs := 0 |
76 |
88 |
for i := offset; true; i++ { |
for i := offset; true; i++ { |
77 |
89 |
channelURLs, err := api.DefaultAPI.GetChannelVideoURLs(channelID, uint(i)) |
channelURLs, err := api.DefaultAPI.GetChannelVideoURLs(channelID, uint(i)) |
78 |
90 |
if err != nil { |
if err != nil { |
79 |
|
log.Printf("Aborting on error %v.", err) |
|
|
91 |
|
log.Printf("Aborting on error: %v.", err) |
80 |
92 |
break |
break |
81 |
93 |
} |
} |
82 |
94 |
if len(channelURLs) == 0 { |
if len(channelURLs) == 0 { |
|
... |
... |
var channelDumpCmd = cobra.Command{ |
86 |
98 |
totalURLs += len(channelURLs) |
totalURLs += len(channelURLs) |
87 |
99 |
log.Printf("Received page %d: %d videos.", i, len(channelURLs)) |
log.Printf("Received page %d: %d videos.", i, len(channelURLs)) |
88 |
100 |
|
|
89 |
|
for _, _url:= range channelURLs { |
|
90 |
|
_, err := writer.WriteString(_url + "\n") |
|
91 |
|
if err != nil { panic(err) } |
|
|
101 |
|
if printResults { |
|
102 |
|
for _, _url := range channelURLs { |
|
103 |
|
fmt.Println(_url) |
|
104 |
|
} |
|
105 |
|
} else { |
|
106 |
|
for _, _url := range channelURLs { |
|
107 |
|
_, err := writer.WriteString(_url + "\n") |
|
108 |
|
if err != nil { panic(err) } |
|
109 |
|
} |
92 |
110 |
} |
} |
93 |
111 |
} |
} |
94 |
112 |
|
|