Skip to content

Commit

Permalink
Merge pull request #49 from JasonMillward/filebot
Browse files Browse the repository at this point in the history
Fixes #29 and #23
  • Loading branch information
JasonMillward committed Jul 21, 2014
2 parents f3a082f + b373e01 commit 3d3431c
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 159 deletions.
126 changes: 103 additions & 23 deletions autorip.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@
@license http://opensource.org/licenses/MIT
Usage:
autorip.py ( --rip | --compress | ( --rip --compress) ) [options]
autorip.py ( --rip | --compress | --extra ) [options]
autorip.py ( --rip [ --compress ] ) [options]
autorip.py --all [options]
Options:
-h --help Show this screen.
--version Show version.
--debug Output debug.
--rip Rip disc using makeMKV.
--compress Compress using handbrake.
--test Testing?
-h --help Show this screen.
--version Show version.
--debug Output debug.
--rip Rip disc using makeMKV.
--compress Compress using HandBrake.
--extra Lookup, rename and/or download extras.
--all Do everything
"""

Expand Down Expand Up @@ -188,30 +191,103 @@ def compress(config):

hb = handbrake.handBrake(config['debug'])

log.debug("Compressing started successfully")
log.debug("Compressing initialised")
log.debug("Looking for movies to compress")

if hb.loadMovie():
log.info( "Compressing %s" % hb.getMovieTitle())
dbMovie = database.next_movie_to_compress()

with stopwatch.stopwatch() as t:
convert = hb.convert(
args=config['handbrake']['com'],
nice=int(config['handbrake']['nice'])
)
if dbMovie is not None:
if hb.check_exists(dbMovie) is not False:

if convert:
log.info("Movie was compressed and encoded successfully")
database.update_movie(dbMovie, 5)

log.info( ("It took %s minutes to compress %s" %
(t.minutes, hb.getMovieTitle()))
)
log.info( "Compressing %s" % dbMovie.moviename )

with stopwatch.stopwatch() as t:
status = hb.convert(
args=config['handbrake']['com'],
nice=int(config['handbrake']['nice']),
dbMovie=dbMovie
)

if status:
log.info("Movie was compressed and encoded successfully")

log.info( ("It took %s minutes to compress %s" %
(t.minutes, dbMovie.moviename))
)

database.insert_history(
dbMovie,
"HandBakeCLI Completed successfully"
)

database.update_movie(dbMovie, 6, filename="%s.mkv" % dbMovie.moviename)

else:
database.update_movie(dbMovie, 5)

database.insert_history(dbMovie, "Handbrake failed", 4)

log.info( "HandBrake did not complete successfully")
else:
log.info( "HandBrake did not complete successfully")
database.update_movie(dbMovie, 2)

database.insert_history(
dbMovie, "Input file no longer exists", 4
)

else:
log.info( "Queue does not exist or is empty")

def extras(config):
"""
Main function for filebotting
Does everything
Returns nothing
"""
log = logger.logger("Extras", config['debug'])

fb = filebot.filebot(config['debug'])

dbMovie = database.next_movie_to_filebot()

if dbMovie is not None:
log.info( "Attempting movie rename" )

database.update_movie(dbMovie, 7)

status = fb.rename(dbMovie)

if status[0]:
log.info( "Rename success")
database.update_movie(dbMovie, 6, filename=status[1])

if config['filebot']['subtitles']:
log.info( "Grabbing subtitles" )

status = fb.get_subtitles(dbMovie, config['filebot']['language'])

if status:
log.info( "Subtitles downloaded" )
database.update_movie(dbMovie, 8)

else:
log.info( "Subtitles not downloaded, no match" )
database.update_movie(dbMovie, 8)

log.info( "Completed work on %s" % dbMovie.moviename )

else:
log.info( "Not grabbing subtitles" )
database.update_movie(dbMovie, 8)

else:
log.info( "Rename failed")

else:
log.info( "No movies ready for filebot")


if __name__ == '__main__':
arguments = docopt.docopt(__doc__, version=__version__)
Expand All @@ -222,8 +298,12 @@ def compress(config):
if bool(config['analytics']['enable']):
analytics.ping(__version__)

if arguments['--rip']:
if arguments['--rip'] or arguments['--all']:
rip(config)

if arguments['--compress']:
if arguments['--compress'] or arguments['--all']:
compress(config)

if arguments['--extra'] or arguments['--all']:
extras(config)

1 change: 1 addition & 0 deletions classes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'analytics',
'database',
'docopt',
'filebot',
'handbrake',
'logger',
'makemkv',
Expand Down
10 changes: 7 additions & 3 deletions classes/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,12 @@ def create_statusTypes():
for sID, sType in statusTypes:
Statustypes.create(statusid=sID, statustext=sType)

def next_movie():
for movie in Movies.select().where((Movies.statusid == 4) | (Movies.filename != None )):
def next_movie_to_compress():
for movie in Movies.select().where((Movies.statusid == 4) & (Movies.filename != None )):
return movie

def next_movie_to_filebot():
for movie in Movies.select().where((Movies.statusid == 6) & (Movies.filename != None ) & (Movies.filebot == 1 )):
return movie

def insert_history(dbMovie, text, typeid=1):
Expand Down Expand Up @@ -140,4 +144,4 @@ def dbintegritycheck():
create_historyTypes()
create_statusTypes()

dbintegritycheck()
dbintegritycheck()
95 changes: 82 additions & 13 deletions classes/filebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,93 @@

import os
import subprocess
import database
import logger


class filebot(object):

def __init__(self, debug):
self.db = database.database()
self.log = logger.logger("Filebot", debug)

def rename(self):
# filebot -rename title00.mkv --q [Movie Name] -non-strict
# [MOVE] Rename [/tmp/Euro Trip G1/Road Trip (2000).mkv] to [EuroTrip (2004).mkv]
pass

def subtitles(self):
# filebot -get-missing-subtitles --lang
# Matched
# Fetching
# Writing
pass
def rename(self, dbMovie):

command = [
'filebot',
'-rename',
"%s/%s" % (dbMovie.path, dbMovie.filename),
'--q',
"\"%s\"" % dbMovie.moviename,
'-non-strict',
'--db',
'OpenSubtitles'
]

proc = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)

output = proc.stdout.read()
renamedMovie = ""
checks = 0

if len(output) is not 0:
lines = output.split("\n")
for line in lines:
self.log.debug(line.strip())
if "MOVE" in line:
renamedMovie = line.split("] to [", 1)[1].rstrip(']')
checks += 1

if "Processed" in line:
checks += 1

if "Done" in line:
checks += 1

if checks >= 3 and renamedMovie:
return [True, renamedMovie]
else:
return [False]

def get_subtitles(self, dbMovie, lang):
command = [
'filebot',
'-get-subtitles',
dbMovie.path,
'--q',
"\"%s\"" % dbMovie.moviename,
'--lang',
lang,
'--output',
'srt',
'--encoding',
'utf8',
'-non-strict'
]

proc = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)

checks = 0
output = proc.stdout.read()

if len(output) is not 0:
lines = output.split("\n")
for line in lines:
self.log.debug(line.strip())

if "Processed" in line:
checks += 1

if "Done" in line:
checks += 1

if checks >= 2:
return True
else:
return False
Loading

0 comments on commit 3d3431c

Please sign in to comment.