improved directory logic
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -54,15 +55,12 @@ type Source struct {
|
|||||||
File string `json:"file"`
|
File string `json:"file"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func DownloadSingle(url string, filename string, subtitleLang string) {
|
func DownloadSingle(url string, subtitleLang string, parentDirectory string) {
|
||||||
id := ExtractContentId(url)
|
id := ExtractContentId(url)
|
||||||
data := GetContentPageData(id)
|
data := GetContentPageData(id)
|
||||||
downloadUrl := GetDownloadUrl(data)
|
downloadUrl := GetDownloadUrl(data)
|
||||||
dirCreated := false
|
|
||||||
|
|
||||||
if filename == "" {
|
name := fmt.Sprintf("%s_%d_%d", data.Data.MainContent.Title, data.Data.MainContent.Season, data.Data.MainContent.Episode)
|
||||||
filename = fmt.Sprintf("%s_%d_%d", data.Data.MainContent.Title, data.Data.MainContent.Season, data.Data.MainContent.Episode)
|
|
||||||
}
|
|
||||||
|
|
||||||
if subtitleLang != "" {
|
if subtitleLang != "" {
|
||||||
subtitles := data.Data.MainContent.Medias[0].Subtitles
|
subtitles := data.Data.MainContent.Medias[0].Subtitles
|
||||||
@@ -76,29 +74,31 @@ func DownloadSingle(url string, filename string, subtitleLang string) {
|
|||||||
|
|
||||||
for _, subtitle := range subtitles {
|
for _, subtitle := range subtitles {
|
||||||
if subtitle.SrcLang == subtitleLang {
|
if subtitle.SrcLang == subtitleLang {
|
||||||
_ = os.Mkdir(filename, os.ModePerm) // dont care if directory fails to create
|
parentDirectory = filepath.Join(parentDirectory, name)
|
||||||
subitleFileName := fmt.Sprintf("%s/%s_%s", filename, filename, subtitle.FileName)
|
_ = os.Mkdir(parentDirectory, os.ModePerm) // dont care if directory fails to create
|
||||||
downloadFile(subtitle.Src, subitleFileName)
|
|
||||||
|
subitleFileName := fmt.Sprintf("%s_%s", name, subtitle.FileName)
|
||||||
|
subitleFilePath := filepath.Join(parentDirectory, subitleFileName)
|
||||||
|
|
||||||
|
downloadFile(subtitle.Src, subitleFilePath)
|
||||||
fmt.Println("Subtitles downloaded successfully")
|
fmt.Println("Subtitles downloaded successfully")
|
||||||
dirCreated = true
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
filepath := fmt.Sprintf("%s.mp4", filename)
|
|
||||||
|
|
||||||
if dirCreated {
|
filenameExt := fmt.Sprintf("%s.mp4", name)
|
||||||
filepath = fmt.Sprintf("%s/%s.mp4", filename, filename)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("Downloading %s to %s\n", url, filename)
|
path := filepath.Join(parentDirectory, filenameExt)
|
||||||
downloadFile(downloadUrl, filepath)
|
|
||||||
fmt.Printf("Finished Downloading %s to %s\n", url, filename)
|
fmt.Printf("Downloading %s to %s\n", url, path)
|
||||||
|
downloadFile(downloadUrl, path)
|
||||||
|
fmt.Println("Finished downloading")
|
||||||
|
fmt.Println()
|
||||||
}
|
}
|
||||||
|
|
||||||
func DownloadSeason(url string, seasonName string, subtitleLang string) {
|
func DownloadSeason(url string, seasonName string, subtitleLang string) {
|
||||||
id := ExtractContentId(url)
|
id := ExtractContentId(url)
|
||||||
// fmt.Println(id)
|
|
||||||
data := GetContentPageData(id)
|
data := GetContentPageData(id)
|
||||||
|
|
||||||
title := data.Data.MainContent.Title
|
title := data.Data.MainContent.Title
|
||||||
@@ -110,39 +110,14 @@ func DownloadSeason(url string, seasonName string, subtitleLang string) {
|
|||||||
for _, season := range seasonList.Seasons {
|
for _, season := range seasonList.Seasons {
|
||||||
if season.Name == seasonName {
|
if season.Name == seasonName {
|
||||||
for _, seasonContent := range season.Contents {
|
for _, seasonContent := range season.Contents {
|
||||||
_ = os.Mkdir(title, os.ModePerm) // dont care if directory fails to create
|
parentDirName := title
|
||||||
// fileName := fmt.Sprintf("%s/episood_%s", title, strconv.Itoa(i+1))
|
_ = os.Mkdir(parentDirName, os.ModePerm) // dont care if directory fails to create
|
||||||
DownloadSingle(seasonContent.Url, "", subtitleLang)
|
DownloadSingle(seasonContent.Url, subtitleLang, parentDirName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func downloadFile(url string, filepath string) {
|
|
||||||
out, err := os.Create(filepath)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer out.Close()
|
|
||||||
|
|
||||||
resp, err := http.Get(url)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer resp.Body.Close()
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
panic("Bad status")
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = io.Copy(out, resp.Body)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetContentPageData(contentId string) *ContentPageData {
|
func GetContentPageData(contentId string) *ContentPageData {
|
||||||
var data ContentPageData
|
var data ContentPageData
|
||||||
|
|
||||||
@@ -176,3 +151,28 @@ func ExtractContentId(url string) string {
|
|||||||
func GetDownloadUrl(data *ContentPageData) string {
|
func GetDownloadUrl(data *ContentPageData) string {
|
||||||
return fmt.Sprintf("https:%s", data.Data.MainContent.Medias[0].Src.File)
|
return fmt.Sprintf("https:%s", data.Data.MainContent.Medias[0].Src.File)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func downloadFile(url string, filepath string) {
|
||||||
|
out, err := os.Create(filepath)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer out.Close()
|
||||||
|
|
||||||
|
resp, err := http.Get(url)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
panic("Bad status")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = io.Copy(out, resp.Body)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
6
main.go
6
main.go
@@ -6,9 +6,9 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// downloader.Download("1038278")
|
// downloader.Download("1038278")
|
||||||
// downloader.Download("https://jupiter.err.ee/1038278/aktuaalne-kaamera", "")
|
downloader.DownloadSingle("https://jupiter.err.ee/1038278/aktuaalne-kaamera", "", "")
|
||||||
// downloader.DownloadSingle("https://jupiter.err.ee/1609406782/babulon-berliin", "", "ET")
|
// downloader.DownloadSingle("https://jupiter.err.ee/1609406782/babulon-berliin", "ET", "")
|
||||||
downloader.DownloadSeason("https://jupiter.err.ee/1235599/babulon-berliin", "4", "ET")
|
// downloader.DownloadSeason("https://jupiter.err.ee/1235599/babulon-berliin", "4", "ET")
|
||||||
// fs := http.FileServer(http.Dir("static/"))
|
// fs := http.FileServer(http.Dir("static/"))
|
||||||
//
|
//
|
||||||
// http.Handle("/", fs)
|
// http.Handle("/", fs)
|
||||||
|
|||||||
Reference in New Issue
Block a user