#!/usr/bin/python from time import time import requests import argparse import csv import os.path parser = argparse.ArgumentParser( prog='Scrobbler', description='Scrobbles Tapes and Vinyl to a Listenbrainz API') parser.add_argument('list') requiredNamed = parser.add_argument_group('Required Arguments') requiredNamed.add_argument('-t', '--type', help='Whether the list is from a tape or vinyl', required=True) parser.add_argument('-s', '--side', help='Specify a side', nargs='+') args = parser.parse_args() # Set DEBUG to True to test local dev server. # API keys for local dev server and the real server are different. DEBUG = True ROOT = 'https://scrobbles.ceressees.dev/apis/listenbrainz' ROOT_FOLDER = '/home/ceres/Scrobbles' PATH = f'{ROOT_FOLDER}/{args.type.title()}/{args.list}' SONGS = [] song_num = 0 if not os.path.isfile(PATH): raise Exception("Please specify a valid list") with open(PATH) as list_file: list_data = csv.reader(list_file, delimiter=',') for row in list_data: if args.side == None or row[0] in args.side: SONGS.append([row[1], row[2], row[3]]) def submit_listen(listen_type, payload, token): response = requests.post( url="{0}/1/submit-listens".format(ROOT), json={ "listen_type": listen_type, "payload": payload, }, headers={ "Authorization": "Token {0}".format(token) } ) response.raise_for_status() return response.json() if __name__ == "__main__": for song in SONGS: PAYLOAD = [ { "listened_at": int(time()), "track_metadata": { "artist_name": f"{song[1]}", "track_name": f"{song[0]}", "release_name": f"{song[2]}" } } ] token = 'QAE42y-vs3pwpJex1tPRCIulCdbnSHpL4tIuYZrB2bevBHze' json_response = submit_listen(listen_type='single', payload=PAYLOAD, token=token) print("Response was: {0}".format(json_response)) print(f'Scrobbled {song[0]}')