Last active
October 30, 2024 09:07
-
-
Save nicola-lunghi/e0ce30a5a2f405359b4e95a7d7a4e1a0 to your computer and use it in GitHub Desktop.
Print Songbook Pro Song Lists python script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Copyright (C) 2024 Nicola Lunghi | |
# | |
# This program is free software: you can redistribute it and/or modify it under the terms of the | |
# GNU General Public License as published by the Free Software Foundation, version 3. | |
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | |
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
# See the GNU General Public License for more details. | |
# You should have received a copy of the GNU General Public License along with this program. | |
# If not, see https://www.gnu.org/licenses/. | |
import argparse | |
import csv | |
import json | |
import os | |
import zipfile | |
import logging | |
logging.basicConfig(level=logging.DEBUG) | |
logger = logging.getLogger(__name__) | |
def parse_songs(zip_file_path): | |
datafile_name = "dataFile.txt" | |
songs = [] | |
logger.info(f"Opening ZIP file: {zip_file_path}") | |
with zipfile.ZipFile(zip_file_path, "r") as zip_file: | |
with zip_file.open(datafile_name) as file: | |
# Read the first line to get the version | |
version = file.readline().decode("utf-8").strip() | |
logger.info(f"Version: {version}") | |
# Read the rest of the file and parse it as JSON | |
json_data = json.load(file) | |
# Access the songs array | |
songs = json_data.get("songs", []) | |
return songs | |
def write_songs_to_csv(songs, csv_file_name): | |
# Specify the fields to print | |
fields_to_print = [ | |
"author", | |
"name", | |
"_tags", | |
# 'subTitle', | |
# "key", | |
] | |
# Print a list of all fields present in the songs dict | |
all_fields = set(field for song in songs for field in song.keys()) | |
logger.debug("All fields present in the songs dict: %s", all_fields) | |
logger.debug("Fields to print: %s", fields_to_print) | |
logger.debug("Opening CSV file name: %s", csv_file_name) | |
# Open the CSV file for writing | |
with open(csv_file_name, mode="w", newline="", encoding="utf-8") as csv_file: | |
writer = csv.DictWriter(csv_file, fieldnames=fields_to_print) | |
writer.writeheader() | |
# Write each song's details to the CSV file | |
for song in songs: | |
fields = {field: song.get(field, "N/A") for field in fields_to_print} | |
logger.debug("%s", fields) | |
writer.writerow(fields) | |
logger.info("Songs have been written to %s", csv_file_name) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Parse and print songs from SongbookPro backups") | |
parser.add_argument("sbp_files", nargs="+", help="The paths to the SongBookPro backup files") | |
parser.add_argument("-o", "--output", default="all_songs.csv", help="The output CSV file name") | |
args = parser.parse_args() | |
all_songs = [] | |
for sbp_file in args.sbp_files: | |
all_songs.extend(parse_songs(sbp_file)) | |
output_csv_file = "all_songs.csv" | |
write_songs_to_csv(all_songs, args.output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
version 4