Created
March 16, 2025 23:24
-
-
Save azrdev/b810ee37208b41d36c71662e58975f96 to your computer and use it in GitHub Desktop.
print spotify URLs in QR-codes for https://github.com/azrdev/phy2music
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
INPUT="$(readlink -f "$1")" | |
test -n "$INPUT" || { echo "Missing input text file"; exit 1; } | |
pushd "$(mktemp -d)" | |
function create_single_qr() { | |
name="$(echo "$1" | sed -n 's_^\(.*\)\s*\t.*$_\1_p')" | |
mediatype="$(echo "$1" | sed -n 's_^.*/\(\w\+\)/\w\+$_\1_p')" | |
uri="$(echo "$1" | sed -n 's_^.*/\(\w\+\)$_\1_p')" | |
if [[ "$mediatype" == "albums" ]] ; then | |
mediatype=album | |
fi | |
echo -e "$uri\t$mediatype\t$name" >&2 | |
name_mangled="$PARALLEL_SEQ $(echo "$name" | sed 's_[^a-zA-Z0-9_-]__g')" | |
echo "spotify:$mediatype:$uri" | qrencode --output="$name_mangled.png" | |
echo "<div><img src=\"$name_mangled.png\"><br/>$name</div>" | |
} | |
export -f create_single_qr | |
cat >output.html <<EOF | |
<head><title>$INPUT</title></head> | |
<body> | |
<style> | |
div { | |
text-align: center; | |
//float: left; | |
display: inline-grid; | |
margin: 1em; | |
max-width: 111px; // TODO: don't hardcode width of qr code png | |
font-size: small; | |
break-inside: avoid; // for printing | |
} | |
img { | |
display: inline-block; | |
} | |
</style> | |
EOF | |
parallel --group --keep-order --arg-file="$INPUT" create_single_qr |tee -a output.html | |
echo "Wrote to "$(readlink -f output.html)"" | |
#ls -1 | sed -e 's_^\(.*\)\.png$__' | pandoc -o test.pdf |
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
import requests | |
import argparse | |
import sys | |
import time | |
argparser = argparse.ArgumentParser(description='get all album name,href tuples from a spotify playlist') | |
argparser.add_argument('-t', '--spotify-auth-token', help='spotify auth token', nargs='?', required=True) | |
argparser.add_argument('-o', '--output-file', nargs='?', required=True) | |
argparser.add_argument('playlist_id', help="e.g. '25dIxWREnlY6a37FMj1sST'") | |
args = argparser.parse_args() | |
def get_albums(response_json): | |
last = None | |
for it in response_json['items']: | |
name = it['track']['album']['name'] | |
url = it['track']['album']['href'] | |
if (name, url) != last: | |
last = (name, url) | |
yield last | |
session = requests.Session() | |
session.headers.update({'Authorization': f'Bearer {args.spotify_auth_token}'}) | |
albums = list() | |
next_url = f'https://api.spotify.com/v1/playlists/{ args.playlist_id }/tracks?fields=total%2Coffset%2Climit%2Cnext%2Citems%28track%28name%2Calbum%28name%2Chref%29%29%29' | |
#next_url = query_url = f"https://api.spotify.com/v1/playlists/{args.playlist_id}?fields=tracks.total%2Ctracks.offset%2Ctracks.limit%2Ctracks.next%2Ctracks.items%28track%28name%2Calbum%28name%2Chref%29%29%29" | |
while next_url: | |
r = session.get(next_url) | |
rj = r.json() | |
albs = list(get_albums(rj)) | |
albums.extend( albs ) | |
for name, url in albs: | |
print(f"{name}\t{url}", file=sys.stderr) | |
if not rj.get('next', None): | |
break | |
# NOTE: `next_url = rj['tracks'].get('next', None)` API broken, will only return empty dict | |
#next_url = query_url + "&offset=" + str(rj['tracks']['limit'] + rj['tracks']['offset']) | |
next_url = rj.get('next', None) | |
time.sleep(10) | |
with open(args.output_file, 'w') as f: | |
for name,url in albums: | |
f.write(f"{name}\t{url}\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment