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