Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
s77rt committed May 5, 2021
0 parents commit f5c0ace
Show file tree
Hide file tree
Showing 1,418 changed files with 74,271 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

bin/

*.syso
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Abdelhafidh Belalia

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
45 changes: 45 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
PROJECT_NAME = YouTubeDownloader

CMD_DIR = ./cmd/

BIN_DIR_MAC = ./bin/mac/
BIN_DIR_LINUX = ./bin/linux/
BIN_DIR_WINDOWS = ./bin/windows/

GIT_TAG = "$(shell git describe --tags)"

LD_FLAGS_MAC = "-X 'github.com/s77rt/YouTubeDownloader.Version=$(GIT_TAG)'"
LD_FLAGS_LINUX = "-X 'github.com/s77rt/YouTubeDownloader.Version=$(GIT_TAG)'"
LD_FLAGS_WINDOWS = "-X 'github.com/s77rt/YouTubeDownloader.Version=$(GIT_TAG)' -H windowsgui"

all: clean dep compile

clean:
@echo -n "Cleaning: "
@rm -rf $(BIN_DIR_MAC)
@rm -rf $(BIN_DIR_LINUX)
@rm -rf $(BIN_DIR_WINDOWS)
@echo "[OK]"

dep:
@echo -n "Downloading Dependencies: "
@go get -d ./...
@echo "[OK]"

compile:
@echo "Compiling: "

# MacOS (64bit)
@mkdir -p $(BIN_DIR_MAC)
GOOS=darwin GOARCH=amd64 go build -ldflags $(LD_FLAGS_MAC) -o $(BIN_DIR_MAC)$(PROJECT_NAME) $(CMD_DIR)$(PROJECT_NAME)

# Linux (64bit)
@mkdir -p $(BIN_DIR_LINUX)
GOOS=linux GOARCH=amd64 go build -ldflags $(LD_FLAGS_LINUX) -o $(BIN_DIR_LINUX)$(PROJECT_NAME) $(CMD_DIR)$(PROJECT_NAME)

# Windows (64bit)
@mkdir -p $(BIN_DIR_WINDOWS)
rsrc -arch amd64 -ico ico.ico -o $(CMD_DIR)$(PROJECT_NAME)/rsrc_windows_amd64.syso
GOOS=windows GOARCH=amd64 go build -ldflags $(LD_FLAGS_WINDOWS) -o $(BIN_DIR_WINDOWS)$(PROJECT_NAME).exe $(CMD_DIR)$(PROJECT_NAME)

@echo "[OK]"
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# YouTubeDownloader
YouTubeDownloader is a simple cross-platform YouTube downloader.

## TODO
- Write a better README.md
48 changes: 48 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package YouTubeDownloader

import (
"os"
"path/filepath"
E "github.com/s77rt/YouTubeDownloader/extractor"
D "github.com/s77rt/YouTubeDownloader/downloader"
)

var (
Version string = "dev"
)

var (
tmpdir string
outdir string
extractor E.Extractor // Currently only one is supported
downloader *D.Downloader
tasks Tasks
)

func Init() {
maketmpdir()
makeoutdir()
extractor = E.New_YouTube_Extractor() // Currently only YouTube is supported
downloader = D.New_Downloader()
tasks = NewTasks()
}

func Clean() {
os.RemoveAll(tmpdir)
}

func maketmpdir() {
tmpdir = filepath.Join(os.TempDir(), "YouTubeDownloader")
err := os.MkdirAll(tmpdir, 0o755)
if err != nil {
panic("unable to create tmpdir")
}
}

func makeoutdir() {
outdir = filepath.Join(".", "Downloads")
err := os.MkdirAll(outdir, 0o755)
if err != nil {
panic("unable to create outdir")
}
}
97 changes: 97 additions & 0 deletions binding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package YouTubeDownloader

import (
"github.com/zserge/lorca"
D "github.com/s77rt/YouTubeDownloader/downloader"
)

func BindUI(ui lorca.UI) {
ui.Bind("Ready", func() {
ui.Eval(`setVersion('`+EscapeJS(Version)+`');`)
})

ui.Bind("GetVideo", func(url string) {
video, err := extractor.GetVideo(url)
if err != nil {
ui.Eval(`w_GetVideo__onerror('`+EscapeJS(err.Error())+`');`)
} else {
ui.Eval(`add_video(`+EscapeJS(MarshalJSONS(video))+`);`)
}
ui.Eval(`h_unlock();`)
})

ui.Bind("GetVideos", func(url string) {
videos, err := extractor.GetVideos(url)
if err != nil {
ui.Eval(`w_GetVideo__onerror('`+EscapeJS(err.Error())+`');`)
} else {
for _, video := range videos {
ui.Eval(`add_video(`+EscapeJS(MarshalJSONS(video))+`);`)
}
}
ui.Eval(`h_unlock();`)
})

ui.Bind("GetExistingVideo", func(uuid string, video interface{}, format interface{}) {
filename, err := getOutputFile(extractor, downloader, video, format)
if err != nil {
ui.Eval(`w_GetExistingVideo__onerror('`+EscapeJS(uuid)+`', '`+EscapeJS(err.Error())+`');`)
} else if FileExists(filename) {
ui.Eval(`update_video_success('`+EscapeJS(uuid)+`','`+EscapeJS(filename)+`');`)
} else {
ui.Eval(`w_GetExistingVideo__onerror('`+EscapeJS(uuid)+`', 'not available');`)
}
})

ui.Bind("DownloadVideo", func(uuid string, video interface{}, format interface{}) {
task := tasks.New(uuid)
filename, err := DownloadVideo(task.Context, task.Tmpdir, extractor, downloader, video, format, func(p1, p2 *D.Progress) {
ui.Eval(`update_video_progress('`+EscapeJS(uuid)+`',`+EscapeJS(MarshalJSONS(p1))+`,`+EscapeJS(MarshalJSONS(p2))+`);`)
})
if err != nil {
if task.Context.Err() != nil {
/*
Errors that are catched in this state where the context is closed
are probably fine to ignore, because as the context get closed, by default a "post" function
get executed and will delete the tmp files for that task, which may result in "no such file or directory" error
for a function that may still trying to use such temp files, thus the error.
the error may also be context.Canceled which is fine.
this should be further investigated...
*/
} else {
ui.Eval(`w_DownloadVideo__onerror('`+EscapeJS(uuid)+`', '`+EscapeJS(err.Error())+`');`)
}
} else {
ui.Eval(`update_video_success('`+EscapeJS(uuid)+`','`+EscapeJS(filename)+`');`)
}
task.Done()
})

ui.Bind("CancelDownload", func(uuid string) {
err := tasks.Abort(uuid)
if err != nil {
ui.Eval(`w_CancelDownload__onerror('`+EscapeJS(uuid)+`', '`+EscapeJS(err.Error())+`');`)
}
})

ui.Bind("PlayVideo", func(uuid string, video_filename string) {
err := RunFile(video_filename)
if err != nil {
ui.Eval(`w_PlayVideo__onerror('`+EscapeJS(uuid)+`', '`+EscapeJS(err.Error())+`');`)
}
})

ui.Bind("OpenFolder", func(uuid string, video_filename string) {
err := OpenFolder(video_filename)
if err != nil {
ui.Eval(`w_OpenFolder__onerror('`+EscapeJS(uuid)+`', '`+EscapeJS(err.Error())+`');`)
}
})

ui.Bind("DeleteVideo", func(uuid string, video_filename string) {
err := DeleteFile(video_filename)
if err != nil {
ui.Eval(`w_DeleteVideo__onerror('`+EscapeJS(uuid)+`', '`+EscapeJS(err.Error())+`');`)
}
})
}
32 changes: 32 additions & 0 deletions cmd/YouTubeDownloader/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"os"
"os/signal"

"github.com/s77rt/YouTubeDownloader"
)

func main() {
YouTubeDownloader.Init()

server := YouTubeDownloader.NewServer()
defer server.Close()

ui := YouTubeDownloader.NewUI()
defer ui.Close()

YouTubeDownloader.BindUI(ui)

YouTubeDownloader.LoadUI(ui, server)

// Wait until the interrupt signal arrives or browser window is closed
sigc := make(chan os.Signal)
signal.Notify(sigc, os.Interrupt)
select {
case <-sigc:
case <-ui.Done():
}

YouTubeDownloader.Clean()
}
Loading

0 comments on commit f5c0ace

Please sign in to comment.