72 lines
2.7 KiB
Python
72 lines
2.7 KiB
Python
import sp
|
|
import yt
|
|
import ts3ab
|
|
|
|
import re
|
|
import time
|
|
|
|
|
|
def comparelists(sppl, ts3abpl):
|
|
newitems = []
|
|
delitems = []
|
|
for spplitem in sppl:
|
|
found = False
|
|
for ts3abplitem in ts3abpl.items:
|
|
if (ts3abplitem['Title'] == "{artist} - {name}".format(artist=' & '.join(spplitem['artists']), name=spplitem['name'])):
|
|
found = True
|
|
if (found == False):
|
|
newitems.append({'spitem': spplitem, 'string': "{artist} - {name}".format(artist=' & '.join(spplitem['artists']), name=spplitem['name'])})
|
|
for i, ts3abplitem in enumerate(ts3abpl.items):
|
|
found = False
|
|
for spplitem in sppl:
|
|
if (ts3abplitem['Title'] == "{artist} - {name}".format(artist=' & '.join(spplitem['artists']), name=spplitem['name'])):
|
|
found = True
|
|
if (found == False):
|
|
delitems.append({'listid': ts3abpl.id, 'index': i})
|
|
return newitems, delitems
|
|
|
|
def synclist(spobj, ts3abobj, sp_pl_id, ts3ab_listid=None, debug=False):
|
|
if debug: print("fetching spotify pl")
|
|
|
|
spplname, sppl = sp.getspotifyplitems(spobj, sp_pl_id)
|
|
|
|
if debug: print("spotifyplname is {spplname}".format(spplname=spplname))
|
|
|
|
if debug: print("fetching ts3ab pl")
|
|
|
|
if (ts3ab_listid == None): ts3ab_listid = re.sub(r"[\"\/\'\(\)\s\t]", "_", spplname)
|
|
|
|
ts3abpl = ts3abobj.getplaylist(ts3ab_listid)
|
|
|
|
if debug: print("comparing pls")
|
|
|
|
newitems, delitems = comparelists(sppl, ts3abpl)
|
|
|
|
if debug: print("deleting old ts3ab items")
|
|
|
|
for delitem in delitems:
|
|
if debug: print("deleting {listid}, {index}, {songname}".format(listid=ts3abpl.id, index=delitem['index'], songname=ts3abpl.items[delitem['index']]))
|
|
ts3abpl.delitem(delitem['index'])
|
|
time.sleep(0.5)
|
|
|
|
if debug: print("adding new items to ts3ab")
|
|
|
|
for newitem in newitems:
|
|
tries = 0
|
|
found = False
|
|
while (not found):
|
|
if debug: print("getting ytvid {string} try {i}".format(string=newitem['string'], i=tries))
|
|
ytvid = yt.getytvideo(newitem['string'], tries)
|
|
if (ytvid['duration_s']*1000 < newitem['spitem']['duration_ms']+5000 and ytvid['duration_s']*1000 > newitem['spitem']['duration_ms']-5000):
|
|
found = True
|
|
else:
|
|
if debug: print("video does not match song length {yturl}".format(yturl=ytvid['url']))
|
|
tries += 1
|
|
if (tries > 5):
|
|
raise Exception("unable to find {string}".format(string=newitem['string']))
|
|
if debug: print("adding {yturl}".format(yturl=ytvid['url']))
|
|
ts3abpl.additem(ytvid['url'], newitem['string'])
|
|
time.sleep(0.5)
|
|
|
|
#if debug: print("renaming items after adding")
|
|
#ts3abpl.clearrenamestash() |