2020-03-03 06:16:01 -06:00
|
|
|
#! /usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim:fenc=utf-8:ft=python
|
|
|
|
#
|
|
|
|
# Bad Witch
|
|
|
|
# Copyright © 2020 Vintage Salt <rehashedsalt@cock.li>
|
|
|
|
#
|
|
|
|
# Distributed under terms of the MIT license.
|
|
|
|
#
|
|
|
|
|
2020-03-03 18:39:04 -06:00
|
|
|
from appdirs import AppDirs
|
|
|
|
from pathlib import Path
|
2020-03-03 06:16:01 -06:00
|
|
|
import argparse
|
2020-03-03 18:39:04 -06:00
|
|
|
import json
|
2020-03-03 20:08:14 -06:00
|
|
|
import logging
|
2020-03-03 06:16:01 -06:00
|
|
|
import sys
|
|
|
|
|
2020-03-03 06:34:49 -06:00
|
|
|
class Library:
|
2020-03-03 18:39:04 -06:00
|
|
|
# A thing full of albums
|
|
|
|
def __init__(self, file, albums={}):
|
|
|
|
self.albums = albums
|
|
|
|
self.file = file
|
|
|
|
|
|
|
|
# Load from file
|
2020-03-03 19:14:38 -06:00
|
|
|
def load(self):
|
2020-03-03 18:39:04 -06:00
|
|
|
with open(self.file, 'r+') as libfd:
|
|
|
|
libfd.seek(0)
|
2020-03-03 19:14:38 -06:00
|
|
|
self.albums = json.load(libfd)
|
2020-03-03 06:34:49 -06:00
|
|
|
return
|
|
|
|
|
2020-03-03 18:39:04 -06:00
|
|
|
# Save to file
|
|
|
|
def save(self):
|
|
|
|
with open(self.file, 'w+') as libfd:
|
|
|
|
libfd.seek(0)
|
|
|
|
json.dump(self.albums, libfd, indent='\t')
|
|
|
|
return
|
|
|
|
|
2020-03-03 20:08:14 -06:00
|
|
|
def validate(self):
|
|
|
|
self.load()
|
|
|
|
|
|
|
|
# Download library
|
|
|
|
def download(self, album=None):
|
|
|
|
if album is not None:
|
|
|
|
print('Downloading album: ' + album)
|
|
|
|
else:
|
|
|
|
print('Downloading entire library')
|
|
|
|
for album, albumcontent in self.albums.items():
|
|
|
|
for song, songcontent in albumcontent.items():
|
|
|
|
logging.info('Downloaded song: ' + song + ' by ' + songcontent['artist'])
|
2020-03-03 06:34:49 -06:00
|
|
|
|
2020-03-03 06:16:01 -06:00
|
|
|
class BadWitch:
|
2020-03-03 06:34:49 -06:00
|
|
|
# Our program
|
2020-03-03 06:16:01 -06:00
|
|
|
def __init__(self):
|
2020-03-03 19:25:40 -06:00
|
|
|
# Flags and arguments
|
|
|
|
self.argparser = argparse.ArgumentParser(
|
|
|
|
description='Manage a declarative music library through YouTube scraping')
|
|
|
|
self.argparser.add_argument('-l', '--library', metavar='f', nargs='?',
|
2020-03-03 06:21:50 -06:00
|
|
|
help='Override default library file with this one')
|
2020-03-03 06:16:01 -06:00
|
|
|
self.argparser.add_argument('-v', '--verbose', action='store_true',
|
|
|
|
help='Show more status messages')
|
2020-03-03 20:08:14 -06:00
|
|
|
self.argparser.add_argument('-d', '--debug', action='store_true',
|
|
|
|
help='Show even more status messages')
|
2020-03-03 19:25:40 -06:00
|
|
|
self.argparser.add_argument('action', metavar='action', nargs='?',
|
2020-03-03 19:52:45 -06:00
|
|
|
choices=['download', 'list', 'test'],
|
2020-03-03 19:25:40 -06:00
|
|
|
help='Action to perform on the library')
|
2020-03-03 18:39:04 -06:00
|
|
|
# Set up appdirs
|
|
|
|
self.dirs = AppDirs('badwitch', 'rehashedsalt')
|
|
|
|
Path(self.dirs.user_data_dir).mkdir(parents=True, exist_ok=True)
|
2020-03-03 06:16:01 -06:00
|
|
|
|
|
|
|
def execute(self):
|
|
|
|
self.args = self.argparser.parse_args()
|
2020-03-03 20:08:14 -06:00
|
|
|
# Parse flags
|
|
|
|
if self.args.debug:
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
elif self.args.verbose:
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
2020-03-03 18:39:04 -06:00
|
|
|
# Initialize library
|
2020-03-03 19:25:40 -06:00
|
|
|
libfile = self.args.library or self.dirs.user_data_dir + '/lib.json'
|
2020-03-03 18:39:04 -06:00
|
|
|
lib = Library(file=libfile)
|
|
|
|
# Perform action
|
2020-03-03 19:25:40 -06:00
|
|
|
if self.args.action == 'download':
|
2020-03-03 20:08:14 -06:00
|
|
|
lib.load()
|
|
|
|
lib.download()
|
2020-03-03 19:25:40 -06:00
|
|
|
return
|
|
|
|
elif self.args.action == 'list':
|
|
|
|
lib.load()
|
|
|
|
for album, albumcontent in lib.albums.items():
|
|
|
|
for song, songcontent in albumcontent.items():
|
|
|
|
print(album + ' - ' + song + ' by ' + songcontent['artist'])
|
|
|
|
return
|
|
|
|
elif self.args.action == 'test':
|
|
|
|
# Set up a test album
|
|
|
|
lib.albums['Bad Witch'] = {
|
|
|
|
'Shit Mirror': {
|
|
|
|
'artist': 'Nine Inch Nails',
|
|
|
|
'source': 'https://www.youtube.com/watch?v=yeqjz5mXrLQ'
|
|
|
|
},
|
|
|
|
'Ahead of Ourselves': {
|
|
|
|
'artist': 'Nine Inch Nails',
|
|
|
|
'source': 'https://www.youtube.com/watch?v=4Ab1O-i4ep4'
|
|
|
|
},
|
|
|
|
'Play the Goddamned Part': {
|
|
|
|
'artist': 'Nine Inch Nails',
|
|
|
|
'source': 'https://www.youtube.com/watch?v=85UgvBkMfr8'
|
|
|
|
},
|
|
|
|
'God Break Down the Door': {
|
|
|
|
'artist': 'Nine Inch Nails',
|
|
|
|
'source': 'https://www.youtube.com/watch?v=eeJ_DzRJUI4'
|
|
|
|
},
|
|
|
|
'I\'m Not From This World': {
|
|
|
|
'artist': 'Nine Inch Nails',
|
|
|
|
'source': 'https://www.youtube.com/watch?v=9fjbcSUSt9w'
|
|
|
|
},
|
|
|
|
'Over and Out': {
|
|
|
|
'artist': 'Nine Inch Nails',
|
|
|
|
'source': 'https://www.youtube.com/watch?v=h-XlN3N2fis'
|
2020-03-03 18:39:04 -06:00
|
|
|
}
|
2020-03-03 19:25:40 -06:00
|
|
|
}
|
|
|
|
lib.save()
|
|
|
|
return
|
2020-03-03 06:34:49 -06:00
|
|
|
|
2020-03-03 06:16:01 -06:00
|
|
|
badwitch = BadWitch()
|
|
|
|
badwitch.execute()
|