-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrobbler.py
executable file
·145 lines (116 loc) · 4.52 KB
/
scrobbler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from datetime import datetime, timedelta
import math
import requests
import argparse
import sys
import discogs_client
version = 1.0
# SET YOUR KEYS HERE
DISCOGS_TOKEN="TOKEN"
MALOJA_URL="https://MALOJAINSTANCE.COM"
MALOJA_API_KEY="KEY"
# color codes for printing in terminal
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
NUMBER = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
# parse parameters
parser = argparse.ArgumentParser(
prog='MalojaManualScrobbler',
description='Script to manually scrobble albums to your maloja instance',
epilog='Please refer to the github repo for more information')
parser.add_argument("-ar", "--artist", type=str)
parser.add_argument("-al", "--album", type=str)
parser.add_argument("-r","--release",type=str)
parser.add_argument("-d","--date",type=str)
args = parser.parse_args()
artist = args.artist
album = args.album
release_id = args.release
date_argument = args.date
client = discogs_client.Client("MalojaManualScrobbler/{}".format(version), user_token=DISCOGS_TOKEN)
if release_id and release_id.isdigit():
release_id = int(release_id)
else: # get release manually if not defined by parameters
# check if enough parameters passed
if ((not album or not artist) and not release_id):
print(bcolors.FAIL + "Artist or Album not defined" + bcolors.ENDC)
sys.exit()
result_count = 0
# search for passed parameters
try:
results = client.search(album,artist=artist,type='release')
except:
print(bcolors.FAIL + "Search failed" + bcolors.ENDC)
sys.exit();
# display search results
print("------ " + bcolors.BOLD + "RESULTS" + bcolors.ENDC + " ------")
result_count = 0
max_iterations = 20 # maximum number of results to show
for entry in results:
if result_count == max_iterations:
break
result_count += 1
print("[" + str(result_count) + "] " + entry.title)
print("---------------------\n")
chosen_entry = int(input("Choose result (1-" + str(result_count) + "):"))
# check user input
# !!yet to implement
release_id = results[chosen_entry-1].id
# fetch release
try:
release = client.release(release_id)
except Exception as e:
print(bcolors.FAIL + "Could not fetch release nr. " + str(release_id) + "[ " + e + "]" + bcolors.ENDC)
sys.exit()
if date_argument:
datetime_str = date_argument
else:
datetime_str = input("When did you start listening to this release? (" + bcolors.OKBLUE + "DD/MM/YY HH:MM" + bcolors.ENDC + "), (or " + bcolors.OKBLUE + "ENTER" + bcolors.ENDC + " for now):")
# check user input
if datetime_str.strip() == "":
start_time = datetime.now()
else:
try:
start_time = datetime.strptime(datetime_str, '%d/%m/%y %H:%M')
except ValueError:
print(bcolors.FAIL + "Please input a valid date in the given format or press enter" + str(release_id) + bcolors.ENDC)
sys.exit()
# !! yet to do
endpoint = f"{MALOJA_URL}/apis/mlj_1/newscrobble?key={MALOJA_API_KEY}"
headers = {"Content-Type": "application/json"}
tracknum = 0
print("Scrobbling release nr. " + bcolors.OKBLUE + str(release_id) + bcolors.ENDC)
for track in release.tracklist:
tracknum += 1
if track.duration: # check if track even has a duration listed
# Split the duration string into minutes and seconds
minutes, seconds = map(int, track.duration.split(':'))
# Create a timedelta object with the given minutes and seconds
delta = timedelta(minutes=minutes, seconds=seconds)
total_seconds = delta.total_seconds() # for scrobbling
total_seconds = math.ceil(total_seconds) # round up for error correction
else:
delta = timedelta(seconds=10)
total_seconds = 10
# scrobbling
scrobble_data = {
"artists": [artist.name for artist in release.artists],
"title": track.title,
"album": release.title,
"duration": total_seconds,
"time": int(start_time.timestamp())
}
start_time = start_time + delta # create new starttime for next track
# scrobble
response = requests.post(endpoint, json=scrobble_data, headers=headers)
if response.status_code == 200:
print(bcolors.OKGREEN + "[" + str(tracknum) + "] SUCCEED (" + track.title + ")" + bcolors.ENDC)
else:
print(bcolors.FAIL + "[" + str(tracknum) + "9 FAILED ("+ track.title + ")" + bcolors.ENDC)