mirror of
https://github.com/pikami/tiktok-dl.git
synced 2026-01-07 11:35:14 +00:00
TTDL-7 Added flag; Code clean up
This commit is contained in:
53
utils/archive.go
Normal file
53
utils/archive.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
models "../models"
|
||||
config "../models/config"
|
||||
log "./log"
|
||||
)
|
||||
|
||||
// IsItemInArchive - Checks if the item is already archived
|
||||
func IsItemInArchive(upload models.Upload) bool {
|
||||
if len(RemoveArchivedItems([]models.Upload{upload})) == 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// RemoveArchivedItems - Returns items slice without archived items
|
||||
func RemoveArchivedItems(uploads []models.Upload) []models.Upload {
|
||||
archiveFilePath := config.Config.ArchiveFilePath
|
||||
|
||||
if archiveFilePath == "" || !CheckIfExists(archiveFilePath) {
|
||||
return uploads
|
||||
}
|
||||
|
||||
removeArchivedItemsDelegate := func(archivedItem string) {
|
||||
for i, upload := range uploads {
|
||||
if upload.GetUploadID() == archivedItem {
|
||||
uploads = append(uploads[:i], uploads[i+1:]...)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lenBeforeRemoval := len(uploads)
|
||||
ReadFileLineByLine(archiveFilePath, removeArchivedItemsDelegate)
|
||||
|
||||
removedCount := lenBeforeRemoval - len(uploads)
|
||||
if removedCount > 0 {
|
||||
log.Logf("%d items, found in archive. Skipping...\n", removedCount)
|
||||
}
|
||||
|
||||
return uploads
|
||||
}
|
||||
|
||||
// AddItemToArchive - Adds item to archived list
|
||||
func AddItemToArchive(uploadID string) {
|
||||
archiveFilePath := config.Config.ArchiveFilePath
|
||||
|
||||
if archiveFilePath == "" {
|
||||
return
|
||||
}
|
||||
|
||||
AppendToFile(uploadID, archiveFilePath)
|
||||
}
|
||||
@@ -4,21 +4,23 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
checkErr "./checkErr"
|
||||
)
|
||||
|
||||
// DownloadFile - Downloads content from `url` and stores it in `outputPath`
|
||||
func DownloadFile(outputPath string, url string) {
|
||||
// Get the data
|
||||
resp, err := http.Get(url)
|
||||
CheckErr(err)
|
||||
checkErr.CheckErr(err)
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Create the file
|
||||
out, err := os.Create(outputPath)
|
||||
CheckErr(err)
|
||||
checkErr.CheckErr(err)
|
||||
defer out.Close()
|
||||
|
||||
// Write the body to file
|
||||
_, err = io.Copy(out, resp.Body)
|
||||
CheckErr(err)
|
||||
checkErr.CheckErr(err)
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"bufio"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
checkErr "./checkErr"
|
||||
)
|
||||
|
||||
type delegateString func(string)
|
||||
@@ -37,7 +39,7 @@ func ReadFileToString(path string) string {
|
||||
// ReadFileLineByLine - Reads file line by line and calls delegate
|
||||
func ReadFileLineByLine(path string, delegate delegateString) {
|
||||
file, err := os.Open(path)
|
||||
CheckErr(err)
|
||||
checkErr.CheckErr(err)
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
@@ -49,3 +51,14 @@ func ReadFileLineByLine(path string, delegate delegateString) {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// AppendToFile - Appends line to file
|
||||
func AppendToFile(str string, filePath string) {
|
||||
f, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
checkErr.CheckErr(err)
|
||||
|
||||
defer f.Close()
|
||||
if _, err := f.WriteString(str + "\n"); err != nil {
|
||||
checkErr.CheckErr(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
config "../models/config"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
config "../../models/config"
|
||||
)
|
||||
|
||||
// Log - Write to std out
|
||||
@@ -2,11 +2,13 @@ package utils
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
|
||||
checkErr "./checkErr"
|
||||
)
|
||||
|
||||
// ReadFileAsString - Returns contents of given file
|
||||
func ReadFileAsString(fileName string) string {
|
||||
content, err := ioutil.ReadFile(fileName)
|
||||
CheckErr(err)
|
||||
checkErr.CheckErr(err)
|
||||
return string(content)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user