Advertisement
faubiguy

updatepl.py

Jul 29th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1. #!/usr/bin/python3
  2. import pymysql, argparse, sys
  3.  
  4. parser = argparse.ArgumentParser()
  5. parser.add_argument('playlist_name')
  6. parser.add_argument('playlist_file')
  7. parser.add_argument('-c', '--config', default='/home/zenith/ampache/config/ampache.cfg.php')
  8. parser.add_argument('-q', '--quiet', action='store_true')
  9. args = parser.parse_args()
  10. playlist = args.playlist_name
  11. playlist_path = args.playlist_file
  12.  
  13. def log(text):
  14.     if not args.quiet:
  15.         print(text, file=sys.stderr)
  16.  
  17. try:
  18.     try:
  19.  
  20.         #TODO get db values from ampache cfg file instead of hardcoding
  21.         connection = pymysql.connect(
  22.             host='localhost',
  23.             user='ampache',
  24.             password='ampasswd',
  25.             db='ampache',
  26.             charset='utf8mb4',
  27.             cursorclass=pymysql.cursors.DictCursor
  28.         )
  29.     except pymysql.err.OperationalError as exc:
  30.         sys.exit('Unable to connect to database\nReason: {0}'.format(exc.args[1]))
  31.        
  32.     log('Connected to the database')
  33.  
  34.     try:
  35.         with open(playlist_path) as playlist_file:
  36.             songs = set(line.strip() for line in playlist_file)
  37.     except IOError as exc:
  38.         sys.exit('Unable to open playlist file\nReason: {0}'.format(exc.args[1]))
  39.        
  40.     log('Read playlist file\nBeginning update operation')
  41.  
  42.     with connection.cursor() as cursor:
  43.         cursor.execute('SELECT id FROM playlist WHERE name=%s', (playlist,))
  44.         playlist_row = cursor.fetchone()
  45.         if not playlist_row:
  46.             sys.exit('No such playlist in database!')
  47.         playlist_id = playlist_row['id']
  48.         #TODO allow create new playlist instead of overwriting old one via cmdline arg
  49.        
  50.         cursor.execute('DELETE FROM playlist_data WHERE playlist=%s', (playlist_id,))
  51.        
  52.         track = 1
  53.         for song in songs:
  54.             cursor.execute('SELECT id FROM song WHERE file=%s', (song,))
  55.             song_row = cursor.fetchone()
  56.             if not song_row:
  57.                 continue
  58.             cursor.execute('INSERT INTO playlist_data (id, playlist, object_id, object_type, track) VALUES (NULL, %s, %s, "song", %s)', (playlist_id, song_row['id'], track))
  59.             track += 1
  60.        
  61.     connection.commit()
  62.     log('Database successfully updated. Playlist \'{0}\' now contains {1} songs'.format(playlist, track-1))
  63.    
  64. except Exception as exc:
  65.     sys.exit('An unknown error occured')
  66. finally:
  67.     connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement