optional track replacement logic

main
Rob Canning 2024-01-26 11:54:33 +01:00
parent 7eaa8f5959
commit 2f133bba5b
2 changed files with 190 additions and 167 deletions

Binary file not shown.

View File

@ -44,13 +44,12 @@ archive = []
#artists_played = []
artist_abreviated = []
web_path = "/home/rob/antena/html/episode/{0}/img".format(episode_number)
# sqlite database connection
conn = sqlite3.connect("database/show.db")
if os.path.exists(web_path):
print("web path already exists .... doing_nothing")
else: os.makedirs(web_path)
web_path = "/home/rob/antena/html/episode/{0}/img".format(episode_number)
if not os.path.exists(web_path):
os.makedirs(web_path)
# /////////////////////////////////////////////////
@ -67,6 +66,72 @@ def create_intro():
intro = random.choice(os.listdir(intropath))
#final_playlist.insert(0, str(os.path.abspath(intropath)) + "/" + str(intro))
def choose_a_track(conn, episode_number):
print("adding a track")
global episode_duration
global track_count
# global archive
set_episode_date(input_date)
max_track_dur = 10
min_track_dur = 2
cursor = conn.cursor()
cursor.execute("SELECT * FROM MUSIC_LIBRARY ORDER BY RANDOM() LIMIT 1 ;")
r = cursor.fetchone() # FETCH ONE RANDOM TRACK FROM THE DATABASE
print(r)
# for t in r:
song = str(r[9])
track_label = str(r[1])
track_album = str(r[2])
track_title = str(r[3])
track_artist = str(r[4])
track_duration = float(r[6])
track_genre = str(r[5])
track_year = str(r[7])
track_path = song #'/'.join(song.split('/')[0:-1])
track_comment = str(r[11])
print(track_duration/60)
# SOME LOGIC TO SEE IF WE ALLOW THAT TRACK OR NOT
if check_album(track_album) is True \
and check_artist(track_artist) is True \
and check_for_KM_compilation(track_album) is True:
if check_archive(track_path) is True:
if track_duration > min_track_dur * 60:
if int(track_duration) < max_track_dur * 60:
# TODO put in a function
art = string=re.sub("\(.*?\)","",track_artist)
# shorten verbose artist names such as trojnik Trojnik (Cene Resnik, Tomaž Grom, Vid Drašler)
art = string=re.sub("and","&",art)
artist_abreviated.append(art)
if not track_year: # where missing metadata give a dummy value
track_year = "0000"
e = Table('EPISODES')
track_number = track_count
id = str(uuid.uuid4())
print("adding..." + str(track_count))
q = e.insert(id, episode_number, track_number, str(episode_date),\
track_album, track_title, track_artist, \
track_duration,track_genre, track_year, \
track_path, track_label, track_comment)
cursor.execute(str(q))
conn.commit()
track_count += 1;
episode_duration = episode_duration + track_duration
return track_count
# else: print("TRACK TOO SHORT..........." )
# else: print("TRACK TOO LONG..........." )
# else: print("SONG PLAYED IN PREVIOUS EPISODE" )
#else: print("ARTIST ALREADY IN PODCAST")
def check_archive(track_path):
cursor = conn.cursor()
@ -128,50 +193,13 @@ def check_artist(track_artist):
return False
def playlist_replace_track(conn, episode_number, track_to_replace):
cursor = conn.cursor()
cursor.execute("SELECT * FROM MUSIC_LIBRARY ORDER BY RANDOM() LIMIT 1 ;")
r = cursor.fetchone() # FETCH A NEW TRACK FROM THE DATABASE
print('new suggestion ================================' + str(r))
song = str(r[9])
track_label = str(r[1])
track_album = str(r[2])
track_title = str(r[3])
track_artist = str(r[4])
track_duration = float(r[6])
track_genre = str(r[5])
track_year = str(r[7])
track_path = song #'/'.join(song.split('/')[0:-1])
track_comment = str(r[11])
e = Table('EPISODES')
# TODO This is not replace at the desired index / row but appending to the end of the table
q = e.insert(1, episode_number, track_to_replace, episode_date,\
track_album, track_title, track_artist, \
track_duration,track_genre, track_year, \
track_path, track_label, track_comment).where('e.track_number'==1)('e.episode'==1)
cursor.execute(str(q))
conn.commit()
print("sqlite: Episode track successfully updated into SHOW table");
#cursor = conn.cursor()
#cursor.execute('INSERT OR REPLACE FROM EPISODES WHERE EPISODE=? and track_number=?', [episode_number, track_to_replace])
#preview = cursor.fetchall()
#for i in preview:
# print('{0} : {1} {2} {3} {4} \n'.format(i[2], i[4], i[5], i[6], i[7],))
# -----------------------------------------------------------------------------
track_count = 0
def create_episode_playlist(conn, episode_number):
global episode_duration
global archive
track_number = 0
track_count = 0
max_track_dur = 10
min_track_dur = 2
@ -182,62 +210,115 @@ def create_episode_playlist(conn, episode_number):
cursor.execute('DELETE FROM EPISODES WHERE episode = {0}'.format(episode_number))
# TODO what is most important 12 tracks or 60 minutes
while episode_duration < 60 * 60 and track_count < 12 :
cursor.execute("SELECT * FROM MUSIC_LIBRARY ORDER BY RANDOM() LIMIT 1 ;")
r = cursor.fetchone() # FETCH ONE RANDOM TRACK FROM THE DATABASE
# for t in r:
song = str(r[9])
track_label = str(r[1])
track_album = str(r[2])
track_title = str(r[3])
track_artist = str(r[4])
track_duration = float(r[6])
track_genre = str(r[5])
track_year = str(r[7])
track_path = song #'/'.join(song.split('/')[0:-1])
track_comment = str(r[11])
# SOME LOGIC TO SEE IF WE ALLOW THAT TRACK OR NOT
print(track_count)
choose_a_track(conn, episode_number)
if check_album(track_album) is True \
and check_artist(track_artist) is True \
and check_for_KM_compilation(track_album) is True:
if check_archive(track_path) is True:
if track_duration > min_track_dur * 60:
if int(track_duration) < max_track_dur * 60:
art = string=re.sub("\(.*?\)","",track_artist)
# shorten verbose artist names such as trojnik Trojnik (Cene Resnik, Tomaž Grom, Vid Drašler)
art = string=re.sub("and","&",art)
artist_abreviated.append(art)
if not track_year: # where missing metadata give a dummy value
track_year = "0000"
e = Table('EPISODES')
track_number = track_count
id = str(uuid.uuid4())
q = e.insert(id, episode_number, track_number, str(episode_date),\
track_album, track_title, track_artist, \
track_duration,track_genre, track_year, \
track_path, track_label, track_comment)
cursor.execute(str(q))
conn.commit()
track_count += 1;
episode_duration = episode_duration + track_duration
# else: print("TRACK TOO SHORT..........." )
# else: print("TRACK TOO LONG..........." )
# else: print("SONG PLAYED IN PREVIOUS EPISODE" )
#else: print("ARTIST ALREADY IN PODCAST")
modify_playlist(conn, episode_number, episode_duration)
#TODO maybe episode duration needs fixing after new track insertion
# -----------------------------------------------------------------------------
# TODO shuffle playlist order as an option
def playlist_replace_track(conn, episode_number, episode_duration):
global track_count
cursor = conn.cursor()
track_to_replace = int(input("which track number would you like to replace?"))
# get a random track to replace the removed
# TODO put it here
cursor.execute('DELETE FROM EPISODES WHERE episode = {0} AND track_number={1}'.format(episode_number, track_to_replace ))
conn.commit()
track_count = track_to_replace# this is where the index number comes from in choose a track funciont
choose_a_track(conn, episode_number)
# cursor.execute('SELECT EPISODE FROM EPISODES WHERE EPISODE = {0} ORDER BY track_number ASC'.format(episode_number, track_to_replace ))
conn.commit()
# TODO does broken track order effect playlist order.... check this - yes its a bit broken - nned to SORT
# TODO ABOVE ORDER QUERY IS NOT WORKING .... too tired... go to bed its 3:38
modify_playlist(conn, episode_number, episode_duration)
# q = 'SELECT * FROM MUSIC_LIBRARY ORDER BY random() LIMIT 1;'.format(episode_number)
# # TODO let me have a choice of specific file/folder or random file
# # a number of q choices through an input()
# cursor.execute(q)
# r = cursor.fetchall()
# print(r)
# # delete the old episode
# cursor.execute('DELETE FROM EPISODES WHERE episode = {0} AND track_number={1}'.format(episode_number, track_to_replace ))
# track_count = 0;
# # write in the shuffled version
# for i in r:
# print("...................................................................." )
# # print('{0} : {1} {2} {3} {4}'.format(i[2], i[4], i[5], i[6], i[7]))
# e = Table('EPISODES')
# track_number = track_count
# id = str(uuid.uuid4())
# q = e.insert(id, episode_number, track_to_replace, str(episode_date),\
# i[4], i[5], i[6], i[7], i[8], i[9], i[10], i[11], i[11])
# cursor.execute(str(q))
# track_count += 1;
# #episode_duration = episode_duration #+ track_duration
def playlist_shuffle_tracks(conn, episode_number, episode_duration):
os.system("clear")
# shuffle the episode
cursor = conn.cursor()
q = 'SELECT * FROM EPISODES WHERE EPISODE = {0} ORDER BY random();'.format(episode_number)
cursor.execute(q)
r = cursor.fetchall()
# delete the old episode
cursor.execute('DELETE FROM EPISODES WHERE episode = {0}'.format(episode_number))
track_count = 0;
# write in the shuffled version
print("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
print(">>NEW SHUFFLED PLAYLIST: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n")
for i in r:
print('{0} : {1} {2} {3} {4} '.format(i[2], i[4], i[5], i[6], i[7],))
e = Table('EPISODES')
track_number = track_count
id = str(uuid.uuid4())
q = e.insert(id, episode_number, track_number, str(episode_date),\
i[4], i[5], i[6], i[7], i[8], i[9], i[10], i[11], i[11]) #TODO fix last index
cursor.execute(str(q))
track_count += 1;
episode_duration = episode_duration
conn.commit()
print("\n>>>>>>>>>>>>>>TOTAL EPISODE DURATION:>>>> {0} >>>>>>>>>>>>>>>>>>>>>>>\n".format(episode_duration/60))
modify_playlist(conn, episode_number, episode_duration)
def playlist_preview_track(conn, episode_number, episode_duration):
cursor = conn.cursor()
# preview a track --------------------------------------
preview_track = input("which track would you like to preview [0-12]:")
cursor.execute('SELECT path FROM EPISODES WHERE EPISODE=? AND track_number=?', [episode_number, preview_track ])
preview_track_path = cursor.fetchone()[0]
print(preview_track_path)
#os.system("mplayer '{0}' &".format(preview_track_path))
subprocess.Popen(["nohup mplayer '{0}' &".format(preview_track_path)], shell=True)
#Popen.run("mplayer '{0}' &".format(preview_track_path))
track_previewed = input("track OK? :")
if track_previewed == 'y':
print("ok groovy choice then...")
return True
else:
return False
def modify_playlist(conn, episode_number, episode_duration):
#TODO some broken logic here - cant do multip shuffles or generate new playlist
@ -245,107 +326,49 @@ def modify_playlist(conn, episode_number, episode_duration):
cursor = conn.cursor()
cursor.execute('SELECT * FROM EPISODES WHERE EPISODE=?', [episode_number])
preview = cursor.fetchall()
#os.system("clear")
print("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
print(">>PROPOSED PLAYLIST: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n")
for i in preview:
print('{0} : {1} {2} {3} {4} '.format(i[2], i[4], i[5], i[6], i[7],))
#dj_is_unsure=True
print("\n>>>> TOTAL EPISODE DURATION:>>>> {0} >>>>>>>>>>>>>>>\n".format(episode_duration/60))
cursor = conn.cursor()
print("\n>>SELECT AN OPTION: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
#if dj_is_unsure:
user_input = input('''nice playlist?
user_input = input('''
p = audio preview of a track + replace or not
y = generate a new playlist,
s = shuffle the existing playlist
r = replace an index with random OR specific
OR PRESS ENTER TO PROCEED....''')
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> OR PRESS ENTER TO PROCEED....
''')
if user_input == 'y':
print("action")
create_episode_playlist(conn, episode_number)
create_episode_playlist(conn, episode_number, episode_duration)
elif user_input == 's':
# shuffle the episode
q = 'SELECT * FROM EPISODES WHERE EPISODE = {0} ORDER BY random();'.format(episode_number)
cursor.execute(q)
r = cursor.fetchall()
# delete the old episode
cursor.execute('DELETE FROM EPISODES WHERE episode = {0}'.format(episode_number))
track_count = 0;
# write in the shuffled version
for i in r:
print('{0} : {1} {2} {3} {4} '.format(i[2], i[4], i[5], i[6], i[7],))
playlist_shuffle_tracks(conn, episode_number, episode_duration)
e = Table('EPISODES')
track_number = track_count
id = str(uuid.uuid4())
q = e.insert(id, episode_number, track_number, str(episode_date),\
i[4], i[5], i[6], i[7], i[8], i[9], i[10], i[11], i[12])
cursor.execute(str(q))
# TODO put this shuffle in a function and sort out the logic flow so i can keep shuffling - remove
track_count += 1;
episode_duration = episode_duration #+ track_duration
conn.commit()
elif user_input == 'r':
to_replace = int(input("which track number would you like to replace?"))
# get a random track to replace the removed
q = 'SELECT * FROM EPISODES WHERE EPISODE = {0} ORDER BY random() LIMIT 1;'.format(episode_number)
# TODO let me have a choice of specific file/folder or random file
# a number of q choices through an input()
cursor.execute(q)
r = cursor.fetchall()
print(r)
# delete the old episode
cursor.execute('DELETE FROM EPISODES WHERE episode = {0} AND track_number={1}'.format(episode_number, to_replace ))
track_count = 0;
# write in the shuffled version
for i in r:
print('{0} : {1} {2} {3} {4} '.format(i[2], i[4], i[5], i[6], i[7],))
e = Table('EPISODES')
track_number = track_count
id = str(uuid.uuid4())
q = e.insert(id, episode_number, to_replace, str(episode_date),\
i[4], i[5], i[6], i[7], i[8], i[9], i[10], i[11], i[12])
cursor.execute(str(q))
track_count += 1;
episode_duration = episode_duration #+ track_duration
conn.commit()
playlist_replace_track(conn, episode_number, episode_duration)
elif user_input == 'p':
# preview a track --------------------------------------
preview_track = input("which track would you like to preview [0-12]:")
cursor.execute('SELECT path FROM EPISODES WHERE EPISODE=? AND track_number=?', [episode_number, preview_track ])
preview_track_path = cursor.fetchone()[0]
print(preview_track_path)
#os.system("mplayer '{0}' &".format(preview_track_path))
subprocess.Popen(["nohup mplayer '{0}' &".format(preview_track_path)], shell=True)
#Popen.run("mplayer '{0}' &".format(preview_track_path))
track_previewed = input("track OK? :")
if track_previewed == 'y':
print("ok groovy choice then...")
else:
input("do the db replace logic")
playlist_replace_track(conn, episode_number, preview_track)
elif user_input == 's':
print("the logic to shuffle the existing playlist order")
playlist_preview_track(conn, episode_number)
else:
print(">>>>>>>>>>>>>>>>>> Goodbye Happy DJ !!!!!!!!!!!!!!")
episode_duration = timedelta(seconds=round(episode_duration))
print("Total Duration = {0}".format(episode_duration))
return user_input
# ----------------------------------------------------------------------
# image processing section
# ----------------------------------------------------------------------
def combine_images(columns, space, images, variants:int):