Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only check date range when defined #112

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 2 additions & 41 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package cmd

import (
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"time"

"github.com/erdaltsksn/cui"
"github.com/fatih/color"
Expand Down Expand Up @@ -57,6 +55,8 @@ var importCmd = &cobra.Command{
}
tagNames := getFlagSlice(cmd, "tag-names")

utils.ParseDateRange(dateRange, dateFormat)

if useGoPro, err := cmd.Flags().GetBool("use-gopro"); err == nil && useGoPro {
detectedGoPro, connectionType, err := gopro.Detect()
if err != nil {
Expand Down Expand Up @@ -96,7 +96,6 @@ var importCmd = &cobra.Command{
DateFormat: dateFormat,
BufferSize: bufferSize,
Prefix: prefix,
DateRange: parseDateRange(dateRange, dateFormat),
TagNames: tagNames,
Connection: connection,
Sort: sortOptions,
Expand Down Expand Up @@ -152,44 +151,6 @@ func init() {
importCmd.Flags().Bool("use-insta360", false, "Detect Insta360 camera attached")
}

func parseDateRange(dateRange []string, dateFormat string) []time.Time {
dateStart := time.Date(0o000, time.Month(1), 1, 0, 0, 0, 0, time.UTC)
dateEnd := time.Now()

if len(dateRange) == 1 {
today := time.Date(dateEnd.Year(), dateEnd.Month(), dateEnd.Day(), 0, 0, 0, 0, dateEnd.Location())
switch dateRange[0] {
case "today":
dateStart = today
case "yesterday":
dateStart = today.Add(-24 * time.Hour)
case "week":
dateStart = today.Add(-24 * time.Duration((int(dateEnd.Weekday()) - 1)) * time.Hour)
case "week-back":
dateStart = today.Add((-24 * 7) * time.Hour)
}
}

if len(dateRange) == 2 {
start, err := time.Parse(utils.DateFormatReplacer.Replace(dateFormat), dateRange[0])
if err != nil {
log.Fatal(err.Error())
}
if err == nil {
dateStart = time.Date(start.Year(), start.Month(), start.Day(), 0, 0, 0, 0, start.Location())
}
end, err := time.Parse(utils.DateFormatReplacer.Replace(dateFormat), dateRange[1])
if err != nil {
log.Fatal(err.Error())
}
if err == nil {
dateEnd = time.Date(end.Year(), end.Month(), end.Day(), 0, 0, 0, 0, end.Location())
}
}

return []time.Time{dateStart, dateEnd}
}

func callImport(cameraIf utils.Import, params utils.ImportParams) (*utils.Result, error) {
return cameraIf.Import(params)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/android/android.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (Entrypoint) Import(params utils.ImportParams) (*utils.Result, error) {
}

// check if is in date range
if entries.Entry().ModifiedAt.Before(params.DateRange[0]) || entries.Entry().ModifiedAt.After(params.DateRange[0]) {
if !utils.IsValidDate(entries.Entry().ModifiedAt) {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/dji/dji.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (Entrypoint) Import(params utils.ImportParams) (*utils.Result, error) {

// check if is in date range

if d.Before(params.DateRange[0]) || d.After(params.DateRange[1]) {
if !utils.IsValidDate(d) {
return godirwalk.SkipThis
}

Expand Down
6 changes: 2 additions & 4 deletions pkg/gopro/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,7 @@ func ImportConnect(params utils.ImportParams) (*utils.Result, error) {
continue
}
tm := time.Unix(i, 0).UTC()
start := params.DateRange[0]
end := params.DateRange[1]
zoneName, _ := end.Zone()
zoneName, _ := utils.DateZone()
newTime := strings.Replace(tm.Format(time.UnixDate), "UTC", zoneName, -1)
tm, _ = time.Parse(time.UnixDate, newTime)
mediaDate := tm.Format("02-01-2006")
Expand All @@ -224,7 +222,7 @@ func ImportConnect(params utils.ImportParams) (*utils.Result, error) {
mediaDate = tm.Format(utils.DateFormatReplacer.Replace(params.DateFormat))
}

if tm.Before(start) || tm.After(end) {
if !utils.IsValidDate(tm) {
continue
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/gopro/gopro.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ folderLoop:
d := getFileTime(osPathname, true)
mediaDate := getMediaDate(getFileTime(osPathname, true), params.DateFormat)

if d.Before(params.DateRange[0]) || d.After(params.DateRange[1]) {
if !utils.IsValidDate(d) {
return godirwalk.SkipThis
}

Expand Down Expand Up @@ -339,7 +339,7 @@ func importFromGoProV1(params utils.ImportParams) utils.Result {
d := getFileTime(osPathname, true)
mediaDate := getMediaDate(d, params.DateFormat)

if d.Before(params.DateRange[0]) || d.After(params.DateRange[1]) {
if !utils.IsValidDate(d) {
return godirwalk.SkipThis
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/insta360/insta360.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (Entrypoint) Import(params utils.ImportParams) (*utils.Result, error) {

// check if is in date range

if d.Before(params.DateRange[0]) || d.After(params.DateRange[1]) {
if !utils.IsValidDate(d) {
return godirwalk.SkipThis
}

Expand Down
2 changes: 0 additions & 2 deletions pkg/utils/cameras.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,5 +332,3 @@ func GetNewBar(progressBar *mpb.Progress, total int64, filename string, barType
),
)
}

var DateFormatReplacer = strings.NewReplacer("dd", "02", "mm", "01", "yyyy", "2006")
62 changes: 60 additions & 2 deletions pkg/utils/import.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package utils

import "time"
import (
"log"
"strings"
"time"
)

type ImportParams struct {
Input, Output, CameraName string
SkipAuxiliaryFiles bool
DateFormat string
BufferSize int
Prefix string
DateRange []time.Time
TagNames []string
Connection ConnectionType
Sort SortOptions
Expand All @@ -17,3 +20,58 @@ type ImportParams struct {
type Import interface {
Import(params ImportParams) (*Result, error)
}

var (
dateEnd time.Time
dateStart time.Time
DateFormatReplacer = strings.NewReplacer("dd", "02", "mm", "01", "yyyy", "2006")
)

func ParseDateRange(dateRange []string, dateFormat string) {
if len(dateRange) == 1 {
today := time.Date(dateEnd.Year(), dateEnd.Month(), dateEnd.Day(), 0, 0, 0, 0, dateEnd.Location())
switch dateRange[0] {
case "today":
dateStart = today
case "yesterday":
dateStart = today.Add(-24 * time.Hour)
case "week":
dateStart = today.Add(-24 * time.Duration((int(dateEnd.Weekday()) - 1)) * time.Hour)
case "week-back":
dateStart = today.Add(-24 * 7 * time.Hour)
}
}

if len(dateRange) == 2 {
start, err := time.Parse(DateFormatReplacer.Replace(dateFormat), dateRange[0])
if err != nil {
log.Fatal(err.Error())
}
if err == nil {
dateStart = time.Date(start.Year(), start.Month(), start.Day(), 0, 0, 0, 0, start.Location())
}
end, err := time.Parse(DateFormatReplacer.Replace(dateFormat), dateRange[1])
if err != nil {
log.Fatal(err.Error())
}
if err == nil {
dateEnd = time.Date(end.Year(), end.Month(), end.Day(), 0, 0, 0, 0, end.Location())
}
}
}

func IsValidDate(tm time.Time) bool {
if (!dateStart.IsZero() && tm.Before(dateStart)) || (!dateEnd.IsZero() && tm.After(dateEnd)) {
return false
}

return true
}

func DateZone() (string, int) {
if dateEnd.IsZero() {
return time.Now().Zone()
}

return dateEnd.Zone()
}