Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
feat: Initial work on new version
Browse files Browse the repository at this point in the history
Signed-off-by: Bᴇʀɴᴅ Sᴄʜᴏʀɢᴇʀs <me@bjw-s.dev>
  • Loading branch information
bjw-s committed Jan 16, 2022
1 parent 1fd5cca commit f8ed105
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 16 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
name: golangci-lint
on:
push:
branches:
- main
pull_request:

permissions:
contents: read

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: golangci-lint
uses: golangci/golangci-lint-action@v2
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.vscode
.vscode
settings.json
trakt.json
16 changes: 10 additions & 6 deletions cmd/series-cleanup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type config struct {
Loglevel string
}
ScanFolders []string
FolderRegex string
Overrides []struct {
Folder string
Mapping mediafile.ShowMapping
Expand Down Expand Up @@ -179,15 +180,16 @@ func collectTvShowFiles(scanFolder string) ([]*mediafile.TVShowFile, error) {
return nil
}

file, err := mediafile.NewTVShowFile(path)
file, err := mediafile.NewTVShowFile(path, configData.FolderRegex)
if err != nil {
return err
}
if file != nil {
// Add mappings
skipShow := false
for _, item := range configData.Overrides {
if strings.EqualFold(file.ShowDir, item.Folder) {
parentFolderName := filepath.Base(filepath.Dir(file.Dir))
if strings.EqualFold(parentFolderName, item.Folder) {
file.Mappings = item.Mapping
skipShow = item.Skip
break
Expand All @@ -197,7 +199,7 @@ func collectTvShowFiles(scanFolder string) ([]*mediafile.TVShowFile, error) {
tvShowFiles = append(tvShowFiles, file)
} else {
log.WithFields(log.Fields{
"show": file.ShowDir,
"show": file.Show,
"file": file.Filename,
}).Debug("Show is configured to be skipped...")
return nil
Expand All @@ -218,17 +220,19 @@ func processTvShowFile(mediafile *mediafile.TVShowFile, user *trakt.User) error
}).Debug("Processing tv show file")

var watchedShow *trakt.WatchedShow
if mediafile.Mappings.TVDBID != 0 {
if len(mediafile.Mappings.IMDBID) != 0 {
watchedShow = user.FindWatchedShowByIMDBID(mediafile.Mappings.IMDBID)
} else if mediafile.Mappings.TVDBID != 0 {
watchedShow = user.FindWatchedShowByTVDBID(mediafile.Mappings.TVDBID)
} else if mediafile.Mappings.TraktName != "" {
watchedShow = user.FindWatchedShowByName(mediafile.Mappings.TraktName)
} else {
watchedShow = user.FindWatchedShowByName(mediafile.ShowDir)
watchedShow = user.FindWatchedShowByName(mediafile.Show)
}

if watchedShow == nil {
log.WithFields(log.Fields{
"show": mediafile.ShowDir,
"show": mediafile.Show,
}).Debug("Show is unwatched or could not be found, skipping...")
return nil
}
Expand Down
9 changes: 7 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
module github.com/bjw-s/seriescleanup

go 1.15
go 1.17

require github.com/apex/log v1.9.0
require (
github.com/apex/log v1.9.0
github.com/oriser/regroup v0.0.0-20210730155327-fca8d7531263
)

require github.com/pkg/errors v0.8.1 // indirect
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ github.com/aphistic/sweet v0.2.0/go.mod h1:fWDlIh/isSE9n6EPsRmC0det+whmX6dJid3st
github.com/aws/aws-sdk-go v1.20.6/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59/go.mod h1:q/89r3U2H7sSsE2t6Kca0lfwTK8JdoNGS/yzM/4iH5I=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
Expand All @@ -27,8 +28,11 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/oriser/regroup v0.0.0-20210730155327-fca8d7531263 h1:Qd1Ml+uEhpesT8Og0ysEhu5+DGhbhW+qxjapH8t1Kvs=
github.com/oriser/regroup v0.0.0-20210730155327-fca8d7531263/go.mod h1:odkMeLkWS8G6+WP2z3Pn2vkzhPSvBtFhAUYTKXAtZMQ=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/fastuuid v1.1.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
Expand All @@ -37,8 +41,10 @@ github.com/smartystreets/go-aws-auth v0.0.0-20180515143844-0c1422d1fdb9/go.mod h
github.com/smartystreets/gunit v1.0.0/go.mod h1:qwPWnhz6pn0NnRBP++URONOVyNkPyr4SauJk4cUOwJs=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tj/assert v0.0.0-20171129193455-018094318fb0/go.mod h1:mZ9/Rh9oLWpLLDRpvE+3b7gP/C2YyLFYxNmcLnPTMe0=
github.com/tj/assert v0.0.3 h1:Df/BlaZ20mq6kuai7f5z2TvPFiwC3xaWJSDQNiIS3Rk=
github.com/tj/assert v0.0.3/go.mod h1:Ne6X72Q+TB1AteidzQncjw9PabbMp4PBMZ1k+vd1Pvk=
github.com/tj/go-buffer v1.1.0/go.mod h1:iyiJpfFcR2B9sXu7KvjbT9fpM4mOelRSDTbntVj52Uc=
github.com/tj/go-elastic v0.0.0-20171221160941-36157cbbebc2/go.mod h1:WjeM0Oo1eNAjXGDx2yma7uG2XoyRZTq1uv3M/o7imD0=
Expand All @@ -64,4 +70,5 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWD
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c h1:grhR+C34yXImVGp7EzNk+DTIk+323eIUWOmEevy6bDo=
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
43 changes: 37 additions & 6 deletions internal/mediafile/tvshowfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"path/filepath"
"regexp"
"strconv"

"github.com/oriser/regroup"
)

// TVShowFile represents a tv show file on disk
type TVShowFile struct {
MediaFile

ShowDir string
Show string
Season int
Episode int
Mappings ShowMapping
Expand All @@ -20,10 +22,40 @@ type TVShowFile struct {
type ShowMapping struct {
TraktName string
TVDBID int
IMDBID string
}

func (tvShowFile *TVShowFile) determineShow(regex string) error {
show := filepath.Base(filepath.Dir(tvShowFile.Dir))
if len(regex) > 0 {
folderRegex := regroup.MustCompile(regex)
matches, err := folderRegex.Groups(show)
if err != nil {
return err
}

if matchShow, ok := matches["Show"]; ok {
show = matchShow
}

if matchIMDBID, ok := matches["IMDBID"]; ok {
tvShowFile.Mappings.IMDBID = matchIMDBID
}

if matchTVBID, ok := matches["TVDBID"]; ok {
tvdbID, err := strconv.Atoi(matchTVBID)
if err != nil {
return err
}
tvShowFile.Mappings.TVDBID = tvdbID
}
}
tvShowFile.Show = show
return nil
}

func (tvShowFile *TVShowFile) determineSeason() error {
r, err := regexp.Compile(".*[sS](\\d+)[eE]\\d+.*")
r, err := regexp.Compile(`.*[sS](\d+)[eE]\d+.*`)
if err != nil {
return err
}
Expand All @@ -38,7 +70,7 @@ func (tvShowFile *TVShowFile) determineSeason() error {
}

func (tvShowFile *TVShowFile) determineEpisode() error {
r, err := regexp.Compile(".*[sS]\\d+[eE](\\d+).*")
r, err := regexp.Compile(`.*[sS]\d+[eE](\d+).*`)
if err != nil {
return err
}
Expand All @@ -53,14 +85,13 @@ func (tvShowFile *TVShowFile) determineEpisode() error {
}

// NewTVShowFile creates a new MediaFile instance
func NewTVShowFile(path string) (*TVShowFile, error) {
func NewTVShowFile(path string, folderRegex string) (*TVShowFile, error) {
tvshowfile := new(TVShowFile)
tvshowfile.path = path
tvshowfile.getBasicFileData()
tvshowfile.getSubtitleFiles()
tvshowfile.determineShow(folderRegex)
tvshowfile.determineSeason()
tvshowfile.determineEpisode()

tvshowfile.ShowDir = filepath.Base(filepath.Dir(tvshowfile.Dir))
return tvshowfile, nil
}
17 changes: 16 additions & 1 deletion internal/trakt/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (user *User) FindWatchedShowByName(name string) *WatchedShow {
}

for _, show := range user.WatchedShows {
if strings.ToLower(show.Show.Title) == strings.ToLower(name) {
if strings.EqualFold(show.Show.Title, name) {
return &show
}
}
Expand All @@ -127,3 +127,18 @@ func (user *User) FindWatchedShowByTVDBID(tvdbid int) *WatchedShow {

return nil
}

// FindWatchedShowByTVDBID returns a watched show for this user by IMDb id
func (user *User) FindWatchedShowByIMDBID(imdbid string) *WatchedShow {
if user.WatchedShows == nil {
return nil
}

for _, show := range user.WatchedShows {
if strings.EqualFold(show.Show.IDS.IMDB, imdbid) {
return &show
}
}

return nil
}

0 comments on commit f8ed105

Please sign in to comment.