Advertisement
Guest User

Change Stars Example Solution

a guest
Aug 5th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.54 KB | None | 0 0
  1. #!/usr/bin/python2
  2.  
  3. '''
  4. Proof of concept. Change the stars rating for a single recording
  5.  
  6. Save this in /usr/local/bin/change_stars.py. chmod 755 on that filename .
  7.  
  8. First:
  9.    change_stars.py --help
  10.  
  11. List all recordings (to find the recordedid in the 4th column):
  12.    change_stars.py
  13.  
  14. Then:
  15.    change_stars.py --recordedid=12345 --stars=6
  16.  
  17. To print three specific titles, using regular expressions/or not:
  18.    change_stars.py --titles 'madam sec' '^ncis$' element
  19. '''
  20.  
  21. from __future__ import print_function
  22. import argparse
  23. import re
  24. import sre_constants
  25. import sys
  26. from MythTV import (exceptions, MythBE, Recorded)
  27.  
  28. BEG = '\n\033[92m'
  29. END = '\033[0m'
  30.  
  31.  
  32. def main():
  33.     '''
  34.    Old tool modified to print the starttime, title, subtitle,
  35.    recordedid and stars. Optionally, allow changing a single
  36.    recording's stars value.
  37.    '''
  38.  
  39.     choices = range(11)
  40.  
  41.     parser = argparse.ArgumentParser(description='MythTV "Stars" tool',
  42.                                      epilog='No arguments = all recordings')
  43.  
  44.     parser.add_argument('--recordedid', type=int, metavar='<n>',
  45.                         help='value that identifies a single recording')
  46.  
  47.     parser.add_argument('--titles', type=str, nargs='+', metavar='<title>',
  48.                         help='one or more titles, regular expressions ' +
  49.                         'are allowed')
  50.  
  51.     parser.add_argument('--stars', type=int, metavar='<n>',
  52.                         choices=choices,
  53.                         help='the number of stars {}'.format(choices))
  54.  
  55.     args = parser.parse_args()
  56.  
  57.     if (args.recordedid and (args.stars is None)) or \
  58.             ((args.stars) and args.recordedid is None):
  59.         sys.exit('\n--recordedid and --stars must both be used to change data')
  60.  
  61.     recs = Recorded.getAllEntries()
  62.     sorted_recordings = sorted(recs, key=lambda recs: recs.starttime)
  63.  
  64.     title_re_lst = list()
  65.  
  66.     if args.titles:
  67.         for one_title in args.titles:
  68.             try:
  69.                 title_re_lst.append(re.compile(one_title, flags=re.IGNORECASE))
  70.             except sre_constants.error:
  71.                 sys.exit('\nCan\'t compile regular expression "{}", maybe ".*?'
  72.                          .format(one_title))
  73.  
  74.     for recording in sorted_recordings:
  75.  
  76.         title_match = False
  77.  
  78.         for title_re in title_re_lst:
  79.             if args.titles and not title_re.search(recording.title):
  80.                 continue
  81.             else:
  82.                 title_match = True
  83.  
  84.         if title_match or (args.titles is None):
  85.             if args.recordedid and (args.stars is not None):
  86.                 db_stars_value = float(args.stars) / 10
  87.                 if recording.recordedid == args.recordedid:
  88.                     recording.update({'stars': db_stars_value})
  89.                     print('\n"{}" stars value set to {}\n'
  90.                           .format(recording.title, args.stars))
  91.                     break
  92.  
  93.             else:
  94.                 print('  {}   {:<30.30}\t{:<16.16}  {:06} {}'
  95.                       .format(recording.starttime.strftime("%b %d %H:%M"),
  96.                               recording.title.encode('utf-8'),
  97.                               recording.subtitle.encode('utf-8'),
  98.                               recording.recordedid,
  99.                               int(recording.stars * 10)))
  100.  
  101.  
  102. if __name__ == '__main__':
  103.  
  104.     try:
  105.         MythBE()
  106.     except exceptions.MythError:
  107.         sys.exit('Unable to connect to the backend.')
  108.  
  109.     main()
  110.  
  111. # vim: set expandtab tabstop=4 shiftwidth=4 smartindent colorcolumn=80:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement