Added json2csv script

Fabio Manganiello 1 year ago
parent 003b4e8218
commit f1f1faffbe

@ -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 `` script.
$ python -i tabs.json -o tabs.csv
# Or
$ python < tabs.json > tabs.csv

@ -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)
tabs = read_tabs_json(sys.stdin)
if args.output:
with open(args.output, 'w') as f:
write_tabs_csv(f, tabs)
write_tabs_csv(sys.stdout, tabs)
# vim:sw=4:ts=4:et: