-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
93 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,25 @@ | ||
package main | ||
|
||
import ( | ||
client "./client" | ||
models "./models" | ||
utils "./utils" | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
workflows "./workflows" | ||
) | ||
|
||
func main() { | ||
models.GetConfig() | ||
url := models.Config.URL | ||
|
||
// Single video | ||
match, _ := regexp.MatchString("\\/@.+\\/video\\/[0-9]+", url) | ||
if match { | ||
getUsernameFromVidURLRegex, _ := regexp.Compile("com\\/@.*") | ||
parts := strings.Split(getUsernameFromVidURLRegex.FindString(url), "/") | ||
username := parts[1][1:] | ||
upload := client.GetVideoDetails(url) | ||
downloadDir := fmt.Sprintf("%s/%s", models.Config.OutputPath, username) | ||
|
||
utils.InitOutputDirectory(downloadDir) | ||
downloadVideo(upload, downloadDir) | ||
if workflows.CanUseDownloadSingleVideo(url) { | ||
workflows.DownloadSingleVideo(url) | ||
return | ||
} | ||
|
||
// Tiktok user | ||
downloadUser() | ||
} | ||
|
||
func downloadVideo(upload models.Upload, downloadDir string) { | ||
uploadID := upload.GetUploadID() | ||
downloadPath := fmt.Sprintf("%s/%s.mp4", downloadDir, uploadID) | ||
|
||
if utils.CheckIfExists(downloadPath) { | ||
fmt.Println("Upload '" + uploadID + "' already downloaded, skipping") | ||
if workflows.CanUseDownloadUser(url) { | ||
workflows.DownloadUser(models.GetUsername()) | ||
return | ||
} | ||
|
||
fmt.Println("Downloading upload item '" + uploadID + "' to " + downloadPath) | ||
utils.DownloadFile(downloadPath, upload.URL) | ||
|
||
if models.Config.MetaData { | ||
metadataPath := fmt.Sprintf("%s/%s.json", downloadDir, uploadID) | ||
upload.WriteToFile(metadataPath) | ||
} | ||
} | ||
|
||
func downloadUser() { | ||
username := models.Config.URL | ||
downloadDir := fmt.Sprintf("%s/%s", models.Config.OutputPath, username) | ||
uploads := client.GetUserUploads(username) | ||
|
||
utils.InitOutputDirectory(downloadDir) | ||
|
||
for _, upload := range uploads { | ||
downloadVideo(upload, downloadDir) | ||
} | ||
panic("Could not recognise URL format") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package workflows | ||
|
||
import ( | ||
client "../client" | ||
models "../models" | ||
utils "../utils" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// CanUseDownloadUser - Test's if this workflow can be used for parameter | ||
func CanUseDownloadUser(url string) bool { | ||
match := strings.Contains(url, "/") | ||
return !match | ||
} | ||
|
||
// DownloadUser - Download all user's videos | ||
func DownloadUser(username string) { | ||
downloadDir := fmt.Sprintf("%s/%s", models.Config.OutputPath, username) | ||
uploads := client.GetUserUploads(username) | ||
|
||
utils.InitOutputDirectory(downloadDir) | ||
|
||
for _, upload := range uploads { | ||
downloadVideo(upload, downloadDir) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package workflows | ||
|
||
import ( | ||
client "../client" | ||
models "../models" | ||
utils "../utils" | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
// CanUseDownloadSingleVideo - Check's if DownloadSingleVideo can be used for parameter | ||
func CanUseDownloadSingleVideo(url string) bool { | ||
match, _ := regexp.MatchString("\\/@.+\\/video\\/[0-9]+", url) | ||
return match | ||
} | ||
|
||
// DownloadSingleVideo - Downloads single video | ||
func DownloadSingleVideo(url string) { | ||
username := models.GetUsername() | ||
upload := client.GetVideoDetails(url) | ||
downloadDir := fmt.Sprintf("%s/%s", models.Config.OutputPath, username) | ||
|
||
utils.InitOutputDirectory(downloadDir) | ||
downloadVideo(upload, downloadDir) | ||
} | ||
|
||
// DownloadVideo - Downloads one video | ||
func downloadVideo(upload models.Upload, downloadDir string) { | ||
uploadID := upload.GetUploadID() | ||
downloadPath := fmt.Sprintf("%s/%s.mp4", downloadDir, uploadID) | ||
|
||
if utils.CheckIfExists(downloadPath) { | ||
fmt.Println("Upload '" + uploadID + "' already downloaded, skipping") | ||
return | ||
} | ||
|
||
fmt.Println("Downloading upload item '" + uploadID + "' to " + downloadPath) | ||
utils.DownloadFile(downloadPath, upload.URL) | ||
|
||
if models.Config.MetaData { | ||
metadataPath := fmt.Sprintf("%s/%s.json", downloadDir, uploadID) | ||
upload.WriteToFile(metadataPath) | ||
} | ||
} |