-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
148 lines (123 loc) · 4.48 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"flag"
"fmt"
"log"
"math"
"os"
"os/exec"
"sync"
"github.com/vbauerster/mpb/v8"
"github.com/vbauerster/mpb/v8/decor"
)
var (
version = "dev"
date = ""
)
func main() {
showVersion := flag.Bool("version", false, "Show version")
flag.BoolVar(showVersion, "v", false, "Show version")
flag.Parse()
if *showVersion {
fmt.Printf("Impartus Video Downloader\nVersion: %s\nBuild Date: %s\n", version, date)
os.Exit(0)
}
_, err := exec.LookPath("ffmpeg")
if err != nil {
log.Fatalln("Please add ffmpeg to your path")
}
// Logging
logFile, err := os.OpenFile("run.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Println("Could not start logs")
}
defer logFile.Close()
log.SetOutput(logFile)
fmt.Println("Impartus Video Downloader")
fmt.Println("If you are facing any issues, please check the section at https://github.com/pnicto/impartus-video-downloader#faqtroubleshooting")
fmt.Print("\n\n")
LoginAndSetToken()
courses := GetCourses()
courseIndex := ChooseCourse(courses)
lectures := GetLectures(courses[courseIndex])
startLectureIndex, endLectureIndex, skipEmptyLectures := ChooseLectures(lectures)
var chosenLectures Lectures
if skipEmptyLectures {
chosenLectures = removeEmptyLectures(lectures[startLectureIndex : endLectureIndex+1])
} else {
chosenLectures = lectures[startLectureIndex : endLectureIndex+1]
}
config := GetConfig()
if config.Slides {
for _, lecture := range chosenLectures {
DownloadLectureSlides(lecture)
}
}
playlists := GetPlaylist(chosenLectures)
err = os.MkdirAll(config.TempDirLocation, 0755)
if err != nil {
log.Fatalln("Could not create temp dir")
}
fmt.Println()
numWorkers := config.NumWorkers
playlistJobs := make(chan ParsedPlaylist, numWorkers)
p := mpb.New(mpb.WithWidth(70))
downloadBar := p.AddBar(int64(len(playlists)),
mpb.PrependDecorators(
decor.Name("Downloaded ", decor.WCSyncWidth),
decor.CountersNoUnit("%d / %d", decor.WCSyncWidth),
),
mpb.AppendDecorators(decor.Percentage(decor.WCSyncWidth)),
mpb.BarPriority(math.MaxInt-1),
)
joiningBar := p.AddBar(int64(len(playlists)),
mpb.PrependDecorators(
decor.Name("Joined ", decor.WCSyncWidth),
decor.CountersNoUnit("%d / %d", decor.WCSyncWidth),
),
mpb.AppendDecorators(decor.Percentage(decor.WCSyncWidth)),
mpb.BarPriority(math.MaxInt),
)
var joinWg sync.WaitGroup
for i := 0; i < numWorkers; i++ {
go func() {
for playlist := range playlistJobs {
// fmt.Println("Downloading playlist: ", playlist.Title, playlist.SeqNo)
downloadedPlaylist := DownloadPlaylist(playlist, p)
metadataFile := CreateTempM3U8File(downloadedPlaylist)
downloadBar.Increment()
// fmt.Println("Downloaded playlist: ", playlist.Title, playlist.SeqNo)
go func(file M3U8File) {
defer joiningBar.Increment()
defer joinWg.Done()
// fmt.Println("Joining chunks for: ", file.Playlist.Title, file.Playlist.SeqNo)
var left, right string
if file.FirstViewFile != "" && config.Views != "right" {
left = JoinChunksFromM3U8(file.FirstViewFile, fmt.Sprintf("LEC %03d %s LEFT VIEW.mp4", file.Playlist.SeqNo, file.Playlist.Title))
}
if file.SecondViewFile != "" && config.Views != "left" {
right = JoinChunksFromM3U8(file.SecondViewFile, fmt.Sprintf("LEC %03d %s RIGHT VIEW.mp4", file.Playlist.SeqNo, file.Playlist.Title))
}
if left != "" && right != "" && config.Views == "both" {
JoinViews(left, right, fmt.Sprintf("LEC %03d %s", file.Playlist.SeqNo, file.Playlist.Title))
}
// fmt.Println("Joined chunks for: ", file.Playlist.Title, file.Playlist.SeqNo)
}(metadataFile)
}
}()
}
for _, playlist := range playlists {
// fmt.Println("Adding playlist to job queue: ", playlist.Title, playlist.SeqNo)
joinWg.Add(1)
playlistJobs <- playlist
}
joinWg.Wait()
p.Wait()
close(playlistJobs)
fmt.Print("\n\n")
fmt.Println("Please delete the temp directory if you are running low on space. The temp directory is located at:", config.TempDirLocation)
fmt.Println("If you are facing any issues, please check the section at https://github.com/pnicto/impartus-video-downloader#faqtroubleshooting")
fmt.Print("\n\n")
fmt.Println("It is recommended that you use this tool as sparingly as possible. Heavy usage of this tool puts more strain on impartus server leading to potential IP bans, breaking API changes and possibly legal action.")
fmt.Println("If this project helped you, consider starring it on GitHub: https://github.com/pnicto/impartus-video-downloader")
}