uho/mk_show.py

303 lines
11 KiB
Python
Raw Normal View History

#!/usr/bin/python3
2024-01-11 00:20:43 +01:00
import os, fnmatch, glob, random, time, pathlib, re
from os.path import join
from tinytag import TinyTag
import datetime
2024-01-11 00:20:43 +01:00
#import skimage.uti
#import skimage.util
2024-01-07 03:07:30 +01:00
from datetime import timedelta
from random import shuffle
DATE = datetime.datetime.now().strftime("%Y-%m-%d")
2024-01-08 02:06:18 +01:00
total_show_duration = 0
2024-01-08 18:01:22 +01:00
complete_playlist = []
show_array = []
2024-01-11 00:20:43 +01:00
show_cover = ""
2024-01-08 02:06:18 +01:00
archive = []
2024-01-11 00:20:43 +01:00
artist_played = []
artist_abreviated = []
episode_number = "0"
2024-01-08 18:01:22 +01:00
show_name = "THE AND IF"
path = "/home/rob/antena/"
# /////////////////////////////////////////////////
2024-01-08 18:01:22 +01:00
def create_intro(show_array):
intropath = path + "texts/clips/this_is"
2024-01-08 02:06:18 +01:00
intro = random.choice(os.listdir(intropath))
2024-01-08 18:01:22 +01:00
show_array.insert(0, str(os.path.abspath(intropath)) + "/" + str(intro))
2024-01-08 02:06:18 +01:00
def add_to_tracks_played(add_to_played: str):
#global complete_tracks_played
2024-01-11 00:20:43 +01:00
with open('playlists/track_playout_history.txt', "a") as tracks_played_file:
2024-01-08 02:06:18 +01:00
tracks_played_file.write(add_to_played + "\n")
2024-01-08 18:01:22 +01:00
def check_archive(track):
2024-01-08 02:06:18 +01:00
global archive
2024-01-11 00:20:43 +01:00
with open('playlists/track_playout_history.txt') as archive_file:
2024-01-08 02:06:18 +01:00
for line in archive_file:
archive.append(line)
2024-01-08 18:01:22 +01:00
if track not in archive:
print("____ TRACK NOT YET PLAYED ... ADDING _____")
return True
else:
print("____ TRACK ALREADY PLAYED _____")
return False
def load_all_music():
2024-01-11 00:20:43 +01:00
with open('playlists/complete_music_archive.pls') as playlist_file:
2024-01-08 18:01:22 +01:00
for line in playlist_file:
complete_playlist.append(line)
print("loaded %s".format(str(len(complete_playlist))))
return complete_playlist
def create_show_playlist(show_array: list, complete_playlist:list):
load_all_music()
2024-01-08 02:06:18 +01:00
global total_show_duration
global archive
track_count = 0
2024-01-11 00:20:43 +01:00
global artist_played
2024-01-08 18:01:22 +01:00
max_track_dur = 8
min_track_dur = 3
2024-01-07 03:07:30 +01:00
2024-01-08 18:01:22 +01:00
while total_show_duration < 60 * 55:
song = random.choice(random.sample(\
complete_playlist, len(complete_playlist) )).rstrip() # pick a song
2024-01-08 02:06:18 +01:00
track = TinyTag.get(song) # get its metadata
# check if the artist not in the played list and the song is less than 8mins
2024-01-08 18:01:22 +01:00
if track.artist not in artist_played:
if check_archive(track.title) is True:
if int(track.duration) > min_track_dur * 60:
if int(track.duration) < max_track_dur * 60:
show_array.append(song.rstrip()) # if 'not in' is true then add the song
2024-01-11 00:20:43 +01:00
art = string=re.sub("\(.*?\)","",track.artist)
art = string=re.sub("and","&",art)
artist_abreviated.append(art) # and add the artist to the played list
2024-01-08 18:01:22 +01:00
artist_played.append(track.artist) # and add the artist to the played list
add_to_tracks_played(track.title) # and write entry to archive file
track_count += 1
total_show_duration = total_show_duration + track.duration
2024-01-11 00:20:43 +01:00
else: print("TRACK TOO SHORT..........." )
else: print("TRACK TOO LONG..........." )
else: print("SONG PLAYED IN PREVIOUS EPISODE" )
else: print("ARTIST ALREADY IN PODCAST")
2024-01-08 18:01:22 +01:00
2024-01-08 02:06:18 +01:00
total_show_duration = timedelta(seconds=round(total_show_duration))
show_array = list(set(show_array))
print("total tracks = " + str(track_count))
print("total duration = " + str(total_show_duration))
2024-01-11 00:20:43 +01:00
print(' \n\n\n SHOW ARRAY: \n {0} \n '.format)
2024-01-08 02:06:18 +01:00
return show_array, total_show_duration
2024-01-11 00:20:43 +01:00
def combine_images(columns, space, images):
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
2024-01-08 02:06:18 +01:00
global show_cover
2024-01-11 00:20:43 +01:00
rows = len(images) // columns
if len(images) % columns:
rows += 1
2024-01-08 02:06:18 +01:00
2024-01-11 00:20:43 +01:00
width_max = 500 #max([Image.open(image).width for image in images])
height_max = 500# max([Image.open(image).height for image in images])
background_width = width_max*columns + (space*columns)-space
background_height = height_max*rows + (space*rows)-space
#background = Image.new('RGBA', (background_width, background_height), (0, 0, 0, 255))
background = Image.new('RGBA', (width_max*columns , height_max*columns), (0, 0, 0, 255))
2024-01-08 18:01:22 +01:00
2024-01-11 00:20:43 +01:00
x = 0
y = 0
for i, image in enumerate(images):
imga = Image.open(image)
size = (500,500)
img = imga.resize(size)
x_offset = int((width_max-img.width)/2)
y_offset = int((height_max-img.height)/2)
background.paste(img, (x+x_offset, y+y_offset+100))
x += width_max + space
if (i+1) % columns == 0:
y += height_max + space
x = 0
im = ImageDraw.Draw(background)
mf_h1 = ImageFont.truetype('fonts/Antonio-Light.ttf', 280)
mf_h2 = ImageFont.truetype('fonts/Antonio-Light.ttf', 70)
mf_h3 = ImageFont.truetype('fonts/Antonio-Regular.ttf', 50)
mf_h4 = ImageFont.truetype('fonts/Antonio-Light.ttf', 45)
h2_spc = 85
h2_baseline = 1530
# Add Text to the image ----------------------------------------
im.text((30,10), '''an eclectic selection of contemporary independent music from slovenia: {0} - E P I S O D E #{1}
'''\
.format(DATE,episode_number), fill="white", font=mf_h3)
im.text((30,280), ''' THIS WEEK ON \n EPISODE #{0} of \n| {1} | SHOW'''\
.format(episode_number, show_name), fill="white", font=mf_h1, stroke_width=2, stroke_fill='black')
im.text((30, h2_baseline + (h2_spc*1) ), '''m u s i c _ f r o m _ : {0}'''\
.format(' | '.join(artist_abreviated[0:3])), (255,255,255), font=mf_h2)
im.text((30, h2_baseline + (h2_spc*2) ), "{0}"\
.format(' | '.join(artist_abreviated[3:6])), (255,255,255), font=mf_h2)
im.text((30, h2_baseline + (h2_spc*3)), "{0}"\
.format(' | '.join(artist_abreviated[6:9])), (255,255,255), font=mf_h2)
im.text((30, h2_baseline + (h2_spc*4)), "{0}"\
.format(' | '.join(artist_abreviated[9:12])), (255,255,255), font=mf_h2)
im.text((1500,1905), ''' http://showname.rizom.si '''\
.format(DATE,episode_number), fill="white", font=mf_h4)
# shorten verbose artist names such as trojnik Trojnik (Cene Resnik, Tomaž Grom, Vid Drašler)
# TiTiTi (Jure Boršič, Jošt Drašler, Vid Drašler)
show_cover = 'html/img/show_cover_{0}.png'.format(DATE)
background.save(show_cover)
def create_show_coverart():
global show_array
print(show_array)
show_cover_jpgs = []
for dir in show_array:
path = pathlib.Path(dir).parent
for file in os.listdir(path):
for i in ["cover", "COVER"]:
if i in file:
show_cover_jpgs.append(str(path) + "/" + file)
print(file)
print("\n ++++ show jpgs: +++++ {0} +++++++++number of jpgs: {1} +++++++\n"\
.format(show_cover_jpgs, len(show_cover_jpgs) ) )
#slice first 12 images of array
# do an if logic here based on track number to set colums
# if len(show_cover_jpgs) <= 9:
# combine_images(columns=3, space=0, images=show_cover_jpgs[:9])
if len(show_cover_jpgs) > 0: # duplicate this for variations of geometry
combine_images(columns=4, space=3, images=show_cover_jpgs[:12])
return show_cover
# create a HTML file containing show information
2024-01-08 18:01:22 +01:00
2024-01-11 00:20:43 +01:00
def create_web_page(show_array:list):
global show_cover
global total_show_duration
dir_path = "html/"
2024-01-08 18:01:22 +01:00
pattern = "*.html"
show_count = len(fnmatch.filter(os.listdir(dir_path), pattern))
2024-01-08 02:06:18 +01:00
show_info = []
for i in show_array:
track = TinyTag.get(i)
detail = str(track.artist) + " | " + str(track.album) + \
" | " + str(track.title) + " | " + str(track.year) + \
" | " + str(timedelta(seconds=round(track.duration)))
show_info.append("" + detail)
2024-01-11 00:20:43 +01:00
with open("html/showinfo_" + DATE + ".html","w") as file:
2024-01-08 02:06:18 +01:00
file.write('''
<html>
<head>
<title> SHOW INFO </title>
</head>
<body> <h1>Inside the with open: %s </h1>\n
\n ''' % DATE)
2024-01-11 00:20:43 +01:00
file.writelines('''<h2>"Show info for: {0} #{1} release date: {2} </h2>
'''.format(show_name, show_count, DATE))
file.writelines('''<img src="../{0}" alt="podcast cover of the week" width="700" height="700">
2024-01-08 02:06:18 +01:00
<h3>Total Playtime excluding fills: {1} </h3>
ARTIST | ALBUM | TRACK | YEAR | DURATION
2024-01-08 18:01:22 +01:00
'''.format(show_cover, total_show_duration) )
2024-01-08 02:06:18 +01:00
for x in show_info:
file.writelines("<p>" + x + "</p>" +" \n " )
2024-01-08 18:01:22 +01:00
2024-01-11 00:20:43 +01:00
file.writelines(''' \n\n <hr>
<p>For more information on the artists or to buy thier albums visit their bandcamp page. </p>" +" \n ''' )
2024-01-08 18:01:22 +01:00
file.writelines("</body></html>")
2024-01-08 02:06:18 +01:00
file.close()
2024-01-07 03:07:30 +01:00
2024-01-11 00:20:43 +01:00
2024-01-08 18:01:22 +01:00
def create_pls_file():
# write the selection as a playlist file
2024-01-08 02:06:18 +01:00
with open("shows/antena_playlist_" + DATE + ".pls","w") as file:
2024-01-08 18:01:22 +01:00
file.writelines("\n".join(show_array))
2024-01-11 00:20:43 +01:00
2024-01-08 18:01:22 +01:00
def create_podcast(show_array: list):
from glob import glob
from pydub import AudioSegment
playlist_songs = [AudioSegment.from_file(flac_file) for flac_file in show_array]
show_intro = playlist_songs.pop(0)
first_song = playlist_songs[0].fade_in(3000)
intro_and_first = first_song.overlay(show_intro)
first_three_blurb = playlist_songs.pop(0)
second_three_blurb = playlist_songs.pop(0)
final_songs_blurb = playlist_songs.pop(0)
final_show_outro = playlist_songs.pop(0)
playlist = intro_and_first
for song in playlist_songs[2:3]: # first three songs (first added with intro)
# We don't want an abrupt stop at the end, so let's do a 1 second crossfades
playlist = playlist.append(song, crossfade=(10 * 1000))
2024-01-08 02:06:18 +01:00
2024-01-08 18:01:22 +01:00
# blurb about first three tracks
playlist = playlist.append(first_three_blurb) # <--------------BLURB INSERT
for song in playlist_songs[4:6]: # second three songs
# We don't want an abrupt stop at the end, so let's do a 1 second crossfades
playlist = playlist.append(song, crossfade=(10 * 1000))
playlist = playlist.append(second_three_blurb) # <--------------BLURB INSERT
2024-01-11 00:20:43 +01:00
for song in playlist_songs[7:]: # second three song # We don't want an abrupt stop at the end, so let's do a 1 second crossfades
2024-01-08 18:01:22 +01:00
playlist = playlist.append(song, crossfade=(10 * 1000))
playlist = playlist.append(final_songs_blurb) # <--------------BLURB INSERT
playlist = playlist.append(final_show_outro) # <-------------- OUTRO SEQUENCE
# get length of final show / podcast
playlist_length = len(playlist) / (1000*60)
# save the entire poidcast
with open("%s_minute_playlist.flac" % playlist_length, 'wb') as out_f:
playlist.export(out_f, format='flac')
create_show_playlist(show_array, complete_playlist)
2024-01-11 00:20:43 +01:00
create_show_coverart() #total_show_duration = 100
2024-01-08 18:01:22 +01:00
#create_intro(show_array)
create_pls_file()
2024-01-08 02:06:18 +01:00
create_web_page(show_array)
2024-01-11 00:20:43 +01:00
#create_podcast(show_array)