Skip to content

Commit

Permalink
simplified bins; cleaner prompting
Browse files Browse the repository at this point in the history
  • Loading branch information
danb235 committed Jan 14, 2015
1 parent 88e4046 commit 5c65ea7
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 162 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ Point **mediatidy** at your movie or TV show directory and it will do the follow
* Delete all non-video type files
* Delete all corrupt/incomplete video files
* Delete all sample files
* Process files to find dupes; keep the highest quality of the dupes and delete the rest

Coming soon:
* Delete files under a specified size
* Process files to find dupes; keep the highest resolution of the dupes and delete the rest
* Process files to find dupes; keep the largest sized file of the dupes (if they are the same resolution) and delete the rest
* Delete empty directories

<!-- ## Current Assumptions
Expand Down Expand Up @@ -44,13 +43,13 @@ sudo npm install -g mediatidy
Add media folder to **mediatidy** you would like to process with:

```
mediatidy config paths-update
mediatidy paths-update
```

Let's tidy up those media directories!

```
mediatidy media update
mediatidy update
```

As always options etc. can be brought up with:
Expand Down
96 changes: 91 additions & 5 deletions bin/mediatidy
Original file line number Diff line number Diff line change
@@ -1,13 +1,99 @@
#!/usr/bin/env coffee

program = require 'commander'
pkg = require '../package.json'
path = require 'path'
program = require 'commander'
Config = require '../lib/config'
Media = require '../lib/media'
colors = require 'colors'
async = require 'async'

program.version(pkg.version)

program.command('update')
.description('look for for files and directories and add to mediatidy database')
.action () ->
media = new Media

# Perform action in series with async
async.series [
(callback) ->
media.setup ->
callback()
(callback) ->
media.addFiles ->
callback()
(callback) ->
media.exists ->
callback()
(callback) ->
media.fileMetaUpdate ->
callback()
(callback) ->
media.deleteCorrupt ->
callback()
(callback) ->
media.deleteSamples ->
callback()
(callback) ->
media.deleteOthers ->
callback()
(callback) ->
media.deleteDupes ->
callback()
], (err, results) ->
throw err if err
console.log 'Media update complete.'

program
.command('paths-update')
.description('update paths to media files for mediatidy to tidy up!')
.action () ->
config = new Config

async.series [
(seriesCallback) ->
config.setup ->
seriesCallback()
(seriesCallback) ->
config.pathPrompt ->
seriesCallback()
], (err, results) ->
throw err if err
console.log 'path update complete.'

program
.command('paths-clear')
.description('clear paths to media files for mediatidy')
.action () ->
config = new Config

async.series [
(seriesCallback) ->
config.setup ->
seriesCallback()
(seriesCallback) ->
config.pathsDelete ->
seriesCallback()
], (err, results) ->
throw err if err
console.log 'path clear complete.'

program
.version(pkg.version)
.command('media <action>', 'perform media file <action> such as tidy')
.command('config <action>', 'perform config <action> such as paths-update')
.command('files-clear')
.description('purge all media files from the database')
.action () ->
config = new Config

async.series [
(seriesCallback) ->
config.setup ->
seriesCallback()
(seriesCallback) ->
config.filesDelete ->
seriesCallback()
], (err, results) ->
throw err if err
console.log 'files clear complete.'

program.parse(process.argv)

Expand Down
64 changes: 0 additions & 64 deletions bin/mediatidy-config

This file was deleted.

63 changes: 0 additions & 63 deletions bin/mediatidy-media

This file was deleted.

4 changes: 2 additions & 2 deletions lib/config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ class Config extends Database
prompt.delimiter = ": ".green
prompt.properties =
path:
description: 'enter full path to media files (movies or tv shows)'
message: 'enter path to media files'
description: 'full path to media files (example: /Volumes/Movies)'
pattern: /^\/\w+/
required: true

prompt.start()
Expand Down
52 changes: 28 additions & 24 deletions lib/media.coffee
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
dir = require 'node-dir'
fs = require 'fs-extra'
probe = require 'node-ffprobe'
async = require 'async'
colors = require 'colors'
prompt = require 'prompt'
Database = require './db'
_ = require 'lodash'
dir = require 'node-dir'
fs = require 'fs-extra'
probe = require 'node-ffprobe'
async = require 'async'
colors = require 'colors'
prompt = require 'prompt'
Database = require './db'
prettyBytes = require 'pretty-bytes'
ProgressBar = require 'progress'
_ = require 'lodash'

class Media extends Database

Expand Down Expand Up @@ -36,7 +36,7 @@ class Media extends Database
@dbBulkPathGet '\'MEDIA\'', (array) =>
if array.length is 0
console.log "No paths have been added to mediatidy. Add paths to your media files with",
"\"mediatidy config paths-update\"".red
"\"mediatidy paths-update\"".red
else
# get files asynchronously for each 'MEDIA' path
async.eachSeries array, ((basePath, seriesCallback) =>
Expand Down Expand Up @@ -142,11 +142,10 @@ class Media extends Database

findDupes: (array, callback) ->
arrayLength = array.length

if arrayLength > 0

async.waterfall [
(callback) ->
# collect dupes by adding them their filtered_filename as a key
objectStore = {}
_.forEach array, (file, iteration) =>
objectStore[file.filtered_filename] = [] unless objectStore.hasOwnProperty(file.filtered_filename)
Expand All @@ -155,6 +154,7 @@ class Media extends Database
if iteration is arrayLength - 1
callback null, objectStore
(objectStore, callback) ->
# go through each key and find detect duplicates and push to array
possibleDupes = []
objectLength = _.size(objectStore)
count = 1
Expand Down Expand Up @@ -207,17 +207,16 @@ class Media extends Database

if arrayLength is iteration + 1
@dbBulkFileDelete array.slice(1), ->
console.log 'files deleted and removed from database...'
callback()
else
fileDelete(iteration + 1)
fileDelete(0)

else
console.log "No files deleted..."
callback()

dupeSort: (array, callback) ->
# sort arrays
sortedDupes = []
_.forEach array, (dupes, i) =>

Expand All @@ -235,15 +234,20 @@ class Media extends Database
# get all files with tag 'HEALTHY'
@dbBulkFileGetTag '\'HEALTHY\'', (files) =>
@findDupes files, (dupes) =>
@dupeSort dupes, (sortedDupes) =>
console.log sortedDupes
deleteDupes = (iteration) =>
@promptUserDupeDelete sortedDupes[iteration], ->
if sortedDupes.length is iteration + 1
callback()
else
deleteDupes(iteration + 1)
deleteDupes(0)
if dupes.length is 0
console.log 'No duplicates found that needed to be deleted...'
callback()
else
@dupeSort dupes, (sortedDupes) =>

# Loop over sortedDupes asynchronously
deleteDupes = (iteration) =>
@promptUserDupeDelete sortedDupes[iteration], ->
if sortedDupes.length is iteration + 1
callback()
else
deleteDupes(iteration + 1)
deleteDupes(0)

deleteOthers: (callback) ->
console.log '==> '.cyan.bold + 'delete files which are not video types'
Expand Down Expand Up @@ -297,7 +301,6 @@ class Media extends Database
if probedFiles
# update database with meta info
@dbBulkFileUpdate probedFiles, ->
console.log 'finished adding probe data to mediatidy database'
callback()
else
callback()
Expand Down Expand Up @@ -360,7 +363,8 @@ class Media extends Database
streamCallback()

if arrayLength is iteration + 1
console.log probedFiles.length + ' out of ' + arrayLength + ' files probed...'
# newline after progress bar
process.stdout.write "\n"
callback probedFiles
else
singleFileProbe(iteration + 1)
Expand Down

0 comments on commit 5c65ea7

Please sign in to comment.