init
This commit is contained in:
		
							
								
								
									
										
											BIN
										
									
								
								youtube/test.mp4
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								youtube/test.mp4
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										32
									
								
								youtube/youtube.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								youtube/youtube.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
			
		||||
package youtube
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/rylio/ytdl"
 | 
			
		||||
	"os"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func Get(url string) (string, error) {
 | 
			
		||||
	v, err := ytdl.GetVideoInfo(url)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return "", err
 | 
			
		||||
	}
 | 
			
		||||
	u, err := v.GetDownloadURL(v.Formats.Best(ytdl.FormatResolutionKey)[0])
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return "", err
 | 
			
		||||
	}
 | 
			
		||||
	return u.String(), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Download(url string, file string) error {
 | 
			
		||||
	v, err := ytdl.GetVideoInfo(url)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	format := v.Formats.Best(ytdl.FormatResolutionKey)[0]
 | 
			
		||||
	f, err := os.Create(file)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	defer f.Close()
 | 
			
		||||
	return v.Download(format, f)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										19
									
								
								youtube/youtube_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								youtube/youtube_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,19 @@
 | 
			
		||||
package youtube
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/stretchr/testify/assert"
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestGet(t *testing.T) {
 | 
			
		||||
	u, err := Get("https://www.youtube.com/watch?v=1rZ-JorHJEY")
 | 
			
		||||
	assert.NoError(t, err)
 | 
			
		||||
	assert.NotEmpty(t, u)
 | 
			
		||||
	fmt.Println(u)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestDownload(t *testing.T) {
 | 
			
		||||
	err := Download("https://www.youtube.com/watch?v=1rZ-JorHJEY", "test.mp4")
 | 
			
		||||
	assert.NoError(t, err)
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user