-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdemaster.py
85 lines (64 loc) · 2.45 KB
/
demaster.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
# this is the demaster script which removes nonsense like "Remastered" and "Live at" from track names
# latest can be downloaded from https://github.com/hankhank10/demaster
import requests
# application settings
api_base_url = 'http://demaster.hankapi.com/demaster?format=simple&long_track_name='
# set user settings
offline_only_mode = False
# script
def strip_name_offline(full_song_name):
text_to_parse = full_song_name
lower_text_to_parse = text_to_parse.lower
offending_text = [
'- Remast',
'(Remast',
'- Live ',
'(Live ',
'(Live at',
'- Mono / Remast',
'- From ',
'(From',
'- With',
'(With '
]
for x in range (1990,2025):
new_offending_text = '- ' + str(x) + ' Remast'
offending_text.append (new_offending_text)
for x in range (1990,2025):
new_offending_text = '(' + str(x) + ' Remast'
offending_text.append (new_offending_text)
for item in offending_text:
if text_to_parse.find(item) >=0:
split_out_text = text_to_parse.partition (item)
return split_out_text[0]
return full_song_name
def strip_name_api(full_song_name):
request_error = False
api_url = api_base_url + full_song_name
print ("Checking API at "+ api_url)
try:
r = requests.get(api_url, timeout=5)
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as err:
print ("Error contacting demaster host: " + err)
short_song_name = "##Error##"
request_error = True
if r.status_code != 200:
request_error = True
short_song_name = "##Error##"
if request_error is False:
short_song_name = r.text
print ("Returning text")
return short_song_name
def strip_name(full_song_name, offline_only_mode=False):
if offline_only_mode == True:
# If we are running in offline-only mode then just parse locally
print ("Just using offline as we are in offline_only_mode")
return strip_name_offline(full_song_name)
# If not, try online API
online_api_response = strip_name_api(full_song_name)
# if there is a connection error, try offline instead
if online_api_response == "##Error##":
print ("Online API failed, returning offline version")
online_api_response = strip_name_offline(full_song_name)
# return whatever we have generated
return online_api_response