Skip to content

Commit

Permalink
add keywords, remove keywords, enhanced keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
danb235 committed Feb 6, 2015
1 parent 774a70c commit 9e9f6f4
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 75 deletions.
18 changes: 16 additions & 2 deletions lib/commands.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,22 @@ program
callback()
], (err, results) ->
throw err if err
console.log 'All media paths have been removed from mediatidy.'

program
.command('remove-keywords')
.description('Remove all keywords from mediatidy')
.action () ->
config = new Config

async.series [
(callback) ->
config.setup ->
callback()
(callback) ->
config.keywordsDelete ->
callback()
], (err, results) ->
throw err if err

program
.command('remove-files')
Expand All @@ -136,7 +151,6 @@ program
callback()
], (err, results) ->
throw err if err
console.log 'All file data has been removed from mediatidy database.'

program.parse(process.argv)

Expand Down
39 changes: 35 additions & 4 deletions lib/config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,43 @@ class Config extends Database
@keywordPromptYesNo ->
callback()
else
console.log "Finished adding keywords..."
callback()

keywordsDelete: (callback) ->
console.log '==> '.cyan.bold + 'remove all keywords from mediatidy'

# get all keywords
@dbBulkKeywordGet '\'DIR\'', (array) =>

arrayLength = array.length
i = 0
while i < arrayLength
console.log "CURRENT KEYWORD:".yellow, array[i].string
i++

prompt.message = "mediatidy".yellow
prompt.delimiter = ": ".green
prompt.properties =
yesno:
default: 'no'
message: 'Delete all keywords from mediatidy?'
required: true
warning: "Must respond yes or no"
validator: /y[es]*|n[o]?/

# Start the prompt
prompt.start()

# get the simple yes or no property
prompt.get ['yesno'], (err, result) =>
if result.yesno.match(/yes/i)
@dbKeywordDelete '\'DIR\'', =>
console.log "All keywords removed..."
callback()
else
console.log "No keywords were removed..."
callback()

pathsDelete: (callback) ->
console.log '==> '.cyan.bold + 'remove all media paths from mediatidy'

Expand Down Expand Up @@ -156,10 +190,8 @@ class Config extends Database
@pathPromptYesNo ->
callback()
else
console.log "Finished adding paths..."
callback()


pathPrompt: (callback) ->
console.log '==> '.cyan.bold + 'update paths to media files for mediatidy to tidy up!'
@pathPromptYesNo =>
Expand Down Expand Up @@ -215,7 +247,6 @@ class Config extends Database
@pathPromptYesNo ->
callback()
else
console.log "Finished adding paths..."
callback()

setup: (callback) ->
Expand Down
6 changes: 6 additions & 0 deletions lib/db.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ class Database
db.close ->
callback()

dbKeywordDelete: (tag, callback) ->
db = new sqlite3.Database(@dbFile)
db.run "DELETE FROM KEYWORDS WHERE tag=#{tag}", ->
db.close ->
callback()

dbPathAdd: (path, tag, callback) ->
db = new sqlite3.Database(@dbFile)

Expand Down
49 changes: 31 additions & 18 deletions lib/dirs.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ async = require 'async'
colors = require 'colors'
prompt = require 'prompt'
Database = require './db'
Config = require './config'
_ = require 'lodash'

class Dirs extends Database
Expand Down Expand Up @@ -62,7 +63,7 @@ class Dirs extends Database
dirExist(0)
else
console.log 'No dirs in database to check...'
callback()
callback missingDirs

getEmptyDirs: (array, callback) ->
# check that each directory path in database exists in the file system
Expand Down Expand Up @@ -92,7 +93,8 @@ class Dirs extends Database
if arrayLength > 0
_.forEach dirs, (dir, iteration) =>
_.forEach keywords, (keyword) =>
if dir.path.indexOf(keyword.string) > 0
# Look for string and remove casing
if dir.path.toUpperCase().indexOf(keyword.string.toUpperCase()) > -1
if (_.findIndex matches, 'path': dir.path) is -1
matches.push
path: dir.path
Expand Down Expand Up @@ -134,27 +136,38 @@ class Dirs extends Database
deleteUnwantedDirs: (callback) ->
console.log '==> '.cyan.bold + 'delete unwanted directories by keyword'
@dbBulkKeywordGet '\'DIR\'', (keywords) =>
@dbBulkDirsGetAll (dirs) =>
@getKeywordMatches keywords, dirs, (matches) =>
if matches.length is 0
console.log "No directories needed to be deleted..."
callback()
else
# Loop over sortedDupes asynchronously
deleteKeywordMatch = (iteration) =>
@promptUserKeywordDelete matches[iteration], ->
if matches.length is iteration + 1
callback()
else
deleteKeywordMatch(iteration + 1)
deleteKeywordMatch(0)
if keywords.length is 0
console.log "No keywords have been added to mediatidy. Add keywords to remove bad directories with",
"\"mediatidy add-keywords\"".red
callback()
else
@dbBulkDirsGetAll (dirs) =>
@getKeywordMatches keywords, dirs, (matches) =>
if !matches
callback()
else if matches.length is 0
console.log "No directories needed to be deleted..."
callback()
else
# Loop over sortedDupes asynchronously
deleteKeywordMatch = (iteration) =>
@promptUserKeywordDelete matches[iteration], ->
if matches.length is iteration + 1
callback()
else
deleteKeywordMatch(iteration + 1)
deleteKeywordMatch(0)

deleteEmptyDirs: (callback) ->
console.log '==> '.cyan.bold + 'delete directories that are empty'
@dbBulkDirsGetAll (dirs) =>
@getEmptyDirs dirs, (emptyDirs) =>
promptMessage = "Delete all directories that are empty?"
@promptUserBulkDelete emptyDirs, promptMessage, ->
# if no empty dirs then just callback
if emptyDirs
promptMessage = "Delete all directories that are empty?"
@promptUserBulkDelete emptyDirs, promptMessage, ->
callback()
else
callback()

dirExists: (callback) ->
Expand Down
108 changes: 57 additions & 51 deletions lib/media.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -37,59 +37,59 @@ class Media extends Database
if array.length is 0
console.log "No paths have been added to mediatidy. Add paths to your media files with",
"\"mediatidy add-paths\"".red
else
# get files asynchronously for each 'MEDIA' path
async.eachSeries array, ((basePath, seriesCallback) =>

fs.exists basePath.path, (exists) =>
if exists
console.log basePath.path + ':', 'searching for files...'
# get files for given path
dir.files basePath.path, (err, files) =>
throw err if err
return
# get files asynchronously for each 'MEDIA' path
async.eachSeries array, ((basePath, seriesCallback) =>

fs.exists basePath.path, (exists) =>
if exists
console.log basePath.path + ':', 'searching for files...'
# get files for given path
dir.files basePath.path, (err, files) =>
throw err if err

# add files to db asynchronously
async.waterfall [
# add video files to db
(callback) =>
# filter files with video file extension
@filterFileTypes files, movieFileExtensions, (movieFiles) ->
callback null, movieFiles
(movieFiles, callback) =>
# convert array of files to array of objects
@convertArray movieFiles, 'VIDEO', (fileObjects) ->
callback null, fileObjects
(fileObjects, callback) =>
# add files to database
@dbBulkFileAdd fileObjects, (result) ->
console.log basePath.path + ':', result, 'video file types...'
callback()

# add files to db asynchronously
async.waterfall [
# add video files to db
(callback) =>
# filter files with video file extension
@filterFileTypes files, movieFileExtensions, (movieFiles) ->
callback null, movieFiles
(movieFiles, callback) =>
# convert array of files to array of objects
@convertArray movieFiles, 'VIDEO', (fileObjects) ->
callback null, fileObjects
(fileObjects, callback) =>
# add files to database
@dbBulkFileAdd fileObjects, (result) ->
console.log basePath.path + ':', result, 'video file types...'
callback()

# add all other files to db
(callback) =>
# filter files without video extensions
@filterFileTypesOpposite files, movieFileExtensions, (otherFiles) ->
callback null, otherFiles
(otherFiles, callback) =>
# convert array of files to array of objects
@convertArray otherFiles, 'OTHER', (fileObjects) ->
callback null, fileObjects
(fileObjects, callback) =>
# add files to database
@dbBulkFileAdd fileObjects, (result) ->
console.log basePath.path + ':', result, 'other file types...'
callback()
], (err, result) ->
throw err if err
seriesCallback()
else
console.log basePath.path, 'could not be found. Consider updating media dirs...'
seriesCallback()
), (err) ->
if err
console.log "Something broke when looking for files...", err
# add all other files to db
(callback) =>
# filter files without video extensions
@filterFileTypesOpposite files, movieFileExtensions, (otherFiles) ->
callback null, otherFiles
(otherFiles, callback) =>
# convert array of files to array of objects
@convertArray otherFiles, 'OTHER', (fileObjects) ->
callback null, fileObjects
(fileObjects, callback) =>
# add files to database
@dbBulkFileAdd fileObjects, (result) ->
console.log basePath.path + ':', result, 'other file types...'
callback()
], (err, result) ->
throw err if err
seriesCallback()
else
callback()
console.log basePath.path, 'could not be found. Consider updating media dirs...'
seriesCallback()
), (err) ->
if err
console.log "Something broke when looking for files...", err
else
callback()

convertArray: (array, tag, callback) ->
# convert each result from array to array of objects
Expand Down Expand Up @@ -295,6 +295,12 @@ class Media extends Database

# get all files
@dbBulkFileGetAll (files) =>
# if no files in db prompt user action
if files.length is 0
console.log "No files were found for mediatidy to tidy up! Check your media paths with",
"\"mediatidy add-paths\"".red
return

# check if files exist for a given path
@checkExists files, (missingFiles) =>
if missingFiles.length > 0
Expand Down

0 comments on commit 9e9f6f4

Please sign in to comment.