Delete recordings.py

From MythTV Official Wiki
Jump to: navigation, search

Important.png Note: The correct title of this article is delete_recordings.py. It appears incorrectly here due to technical restrictions.


Author Raymond Wagner
Description A python script that provides a list of recordings matching given search parameters, allowing them to be deleted in bulk.
Supports Version24.png  


This python script provides a command line tool to search and delete recordings. Accepts any command line argument that can be passed to MythDB.searchRecorded(). As with anything that can mass delete recordings, please exercise caution during use.

Supports the following keywords:
    title,      subtitle,   chanid,     starttime,  progstart,
    category,   hostname,   autoexpire, commflagged,
    stars,      recgroup,   playgroup,  duplicate,  transcoded,
    watched,    storagegroup,           category_type,
    airdate,    stereo,     subtitled,  hdtv,       closecaptioned,
    partnumber, parttotal,  seriesid,   showtype,   programid,
    manualid,   generic,    cast,       livetv,     basename,
    syndicatedepisodenumber,            olderthan,  newerthan

Example of use:

>./delete_recordings.py --title=NOVA --olderthan=2010-10-25T00:00:00
Below is a list of matching recordings:
  0. [2010-10-03T18:00:00] NOVA - The Four-Winged Dinosaur
  1. [2010-10-05T20:00:00] NOVA - AstroSpies
> help
'ok' or 'yes' to confirm, and delete all
     recordings in the current list.
'list' to reprint the list.
<int> to remove that recording from the list.


PythonIcon.png delete_recordings.py

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#---------------------------
# Name: delete_recordings.py
# Python Script
# Author: Raymond Wagner
# Purpose
#   This python script provides a command line tool to search and
#   delete recordings.
#---------------------------

from MythTV import MythDB, MythLog
import sys

def list_recs(recs):
    print 'Below is a list of matching recordings:'
    recs = dict(enumerate(recs.values()))
    for i,rec in recs.items():
        print '  %d. [%s] %s - %s' % \
                (i, rec.starttime.isoformat(), rec.title, rec.subtitle)
    return recs

param = {}

temp = list(sys.argv[1:])
while len(temp):
    a = temp.pop(0)
    if a[:2] == '--':
        a = a[2:]
        if '=' in a:
            a = a.split('=',1)
            param[a[0]] = a[1]
        else:
            if len(temp):
                b = temp.pop(0)
                if (b[:2] == '--') or (b[:1] == '-'):
                    temp.insert(0,b)
                    param[a] = ''
                else:
                    param[a] = b
            else:
                param[a] = ''

MythLog._setlevel(param.get('verbose','none'))
try:
    param.pop('verbose')
except: pass

force = False
if 'force' in param:
    force = True
    param.pop('force')

if len(a) == 0:
    sys.exit(0)

recs = list(MythDB().searchRecorded(**param))
if len(recs) == 0:
    print 'no matching recordings found'
    sys.exit(0)
if force:
    for rec in recs:
        #print 'deleting ',str(rec)
        rec.delete()
    sys.exit(0)

recs = dict(enumerate(recs))

try:
    list_recs(recs)
    while len(recs) > 0:
        inp = raw_input("> ")
        if inp == 'help':
            print "'ok' or 'yes' to confirm, and delete all"
            print "     recordings in the current list."
            print "'list' to reprint the list."
            print "<int> to remove that recording from the list."
        elif inp in ('yes','ok'):
            for rec in recs.values():
                #print 'deleting ',str(rec)
                rec.delete()
            break
        elif inp in ('list',''):
            recs = list_recs(recs)
        else:
            try:
                recs.pop(int(inp))
            except:
                print 'invalid input'
except KeyboardInterrupt:
    pass
except EOFError:
    pass


ALternate Version

The following is a version revised by Hugh. It isn't well tested.

Changes:

  • option --force to allow deletion of metadata even when file is missing
  • option --yes to pre-approve each deletion
  • option --rerecord to allow the deleted program to be re-recorded
  • checks that each keyword used is known and that none is repeated
  • simplified some internal logic

PythonIcon.png delete_recordings_alt.py

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#---------------------------
# Name: delete_recordings.py
# Python Script
# Author: Raymond Wagner, revised by D. Hugh Redelmeier
# Purpose
#   This python script provides a command line tool to search and
#   delete recordings.
# see <https://www.mythtv.org/wiki/Delete_recordings.py>
#---------------------------
# Supports the following options:
#    force: delete metadata even if the actual files are missing
#    rerecord: allow the program to be rerecorded
#    verbose: verbose logging?
#    yes: don't ask, just do
#
# Supports the following keywords:
#    title,	 subtitle,   chanid,	 starttime,  progstart,
#    category,	 hostname,   autoexpire, commflagged,
#    stars,	 recgroup,   playgroup,	 duplicate,  transcoded,
#    watched,	 storagegroup,		 category_type,
#    airdate,	 stereo,     subtitled,	 hdtv,	     closecaptioned,
#    partnumber, parttotal,  seriesid,	 showtype,   programid,
#    manualid,	 generic,    cast,	 livetv,     basename,
#    syndicatedepisodenumber,		 olderthan,  newerthan
#
# Hint:
#    To delete the metadata for a file that is missing for some reason
#    (like a disk crash), if the filename is 1006_20150810000000.mpg, use:
#    delete_recordings --force --rerecord --chanid 1006 --starttime 20150810000000

from MythTV import MythDB, MythLog
import sys

def list_recs(recs):
    print 'Below is a list of matching recordings:'
    recs = dict(enumerate(recs.values()))
    for i,rec in recs.items():
	print '	 %d. [%s] %s - %s' % \
		(i, rec.starttime.isoformat(), rec.title, rec.subtitle)
    return recs

keywords = {  'force', 'rerecord', 'yes', 'verbose',
    'title',  'subtitle',  'chanid',  'starttime',  'progstart',
    'category',	 'hostname',  'autoexpire',  'commflagged',
    'stars',  'recgroup',  'playgroup',	 'duplicate',  'transcoded',
    'watched',	'storagegroup',	 'category',  'type',
    'airdate',	'stereo',  'subtitled',	 'hdtv',  'closecaptioned',
    'partnumber',  'parttotal',	 'seriesid',  'showtype',  'programid',
    'manualid',	 'generic',  'cast',  'livetv',	 'basename',
    'syndicatedepisodenumber',	'olderthan',  'newerthan'  }

temp = list(sys.argv[1:])
param = {}
while len(temp):
    a = temp.pop(0)
    if a[:2] != '--':
	print 'not-an-option:', a
	sys.exit(2)

    key = a[2:]

    if '=' in key:
        t = key.split('=',1)
	key = t[0]
	value = t[1]
    elif len(temp) and temp[0][:2] != '--':
	value = temp.pop(0)
    else:
        value = ''

    if not key in keywords:
	print 'unknown option:', key
	sys.exit(3)

    if key in param:
	print 'duplicated option:', key
	sys.exit(4)

    param[key] = value

MythLog._setlevel(param.get('verbose','none'))
try:
    param.pop('verbose')
except: pass

force = False
if 'force' in param:
    force = True
    param.pop('force')

rerecord = False
if 'rerecord' in param:
    rerecord = True
    param.pop('rerecord')

yes = False
if 'yes' in param:
    yes = True
    param.pop('yes')

if not param:
    print 'no selectors specified'
    sys.exit(5)

recs = list(MythDB().searchRecorded(**param))
if len(recs) == 0:
    print 'no matching recordings found'
    sys.exit(6)

recs = dict(enumerate(recs))

try:
    list_recs(recs)
    while len(recs) > 0:
	if yes:
            inp = 'yes'
	else:
            inp = raw_input("> ")

	if inp in ('help', '?'):
            print "'no' or 'n' to stop"
            print "'yes' or 'y' to confirm, and delete all"
            print "	recordings in the current list."
            print "'list' or 'l' to reprint the list."
            print "<int> to remove that recording from the list."
	elif inp in ('no', 'n'):
            break;
	elif inp in ('ok', 'yes','y'):
            for rec in recs.values():
		print 'deleting',str(rec)
		rec.delete(force=force, rerecord=rerecord)
            break
	elif inp in ('list','l',''):
            recs = list_recs(recs)
	else:
            try:
                recs.pop(int(inp))
            except:
                print 'invalid input'
except KeyboardInterrupt:
    pass
except EOFError:
    pass