Added json2csv script

This commit is contained in:
Fabio Manganiello 2022-02-23 23:08:20 +01:00
parent 003b4e8218
commit f1f1faffbe
2 changed files with 50 additions and 1 deletions

View File

@ -2,7 +2,7 @@
This browser script allows you to scrape and download your saved UltimateGuitar tabs to JSON format. 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). 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 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 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. 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
```

39
json2csv.py Normal file
View File

@ -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: