diff --git a/README.md b/README.md index 2d5c66c..623fb42 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ This browser script allows you to scrape and download your saved UltimateGuitar tabs to JSON format. -There are two ways to use this script: +There are two ways to use the `ug.js` script: 1. Install it as a Greasemonkey script (or whatever extension you use for custom UserScript). Every time you browse to your UltimateGuitar page, the Download button will appear next to @@ -18,3 +18,13 @@ Note that the script will download all the tabs on the current page. If you want of your tabs, then select _All_ from the top filter. The current order of the tabs on the page is also preserved in the downloaded JSON. +## Convert to CSV + +If you want to convert the downloaded JSON to CSV, you can do so by using the `json2csv.py` script. +Usage: + +```shell +$ python json2csv.py -i tabs.json -o tabs.csv +# Or +$ python json2csv.py < tabs.json > tabs.csv +``` diff --git a/json2csv.py b/json2csv.py new file mode 100644 index 0000000..9a800f5 --- /dev/null +++ b/json2csv.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python + +import argparse +import csv +import json +import sys + +from typing import IO, Iterable + +parser = argparse.ArgumentParser('Convert UltimateGuitar tabs JSON to CSV') +parser.add_argument('-i', '--input', help='Input JSON file (default: read from stdin)') +parser.add_argument('-o', '--output', help='Output CSV file (default: write to stdout)') +args = parser.parse_args(sys.argv[1:]) + +def read_tabs_json(f: IO) -> Iterable[dict]: + return json.load(f) + + +def write_tabs_csv(f: IO, tabs: Iterable[dict]): + writer = csv.writer(f, delimiter=',') + writer.writerow(['Artist', 'Title', 'Link']) + writer.writerows([[tab['artist'], tab['title'], tab['link']] for tab in tabs]) + + +if args.input: + with open(args.input) as f: + tabs = read_tabs_json(f) +else: + tabs = read_tabs_json(sys.stdin) + + +if args.output: + with open(args.output, 'w') as f: + write_tabs_csv(f, tabs) +else: + write_tabs_csv(sys.stdout, tabs) + + +# vim:sw=4:ts=4:et: