-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaylist.py
executable file
·80 lines (65 loc) · 2.92 KB
/
playlist.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
import sqlite3
import sys
import datetime
import csv
# TODO: use click or argparse
playlist = sys.argv[1]
# TODO perhaps make this into a class
db = sqlite3.connect(
'/Users/username/Library/Application Support/Swinsian/Library.sqlite')
cursor = db.cursor()
# TODO make this into a function
playlist_num = cursor.execute(
"SELECT playlist_id FROM playlist WHERE name IS ?", (playlist,)).fetchall()
# TODO make this a function
if not playlist_num:
print("playlist doesn't exist")
exit()
# TODO check length of playlist_num and if > 1, enter a for loop to ask user which playlist to convert
#TODO make this into a function
playlist_to_convert = cursor.execute(
"SELECT track.artist,track.title,track.album,track.length,playlisttrack.tindex FROM "
"track INNER JOIN playlisttrack on track.track_id = playlisttrack.track_id WHERE "
"playlisttrack.playlist_id IS ? ORDER BY playlisttrack.tindex",
(playlist_num[0][0],)).fetchall()
# TODO print to cue sheet in a function
with open(f'{playlist}.cue', "w") as cue, open(f'{playlist}.csv', "w", newline='') as csvfile:
now = datetime.datetime.now()
show_date = playlist[0:2] + "-" + playlist[2:4] + "-" + str(now.year)[2:4]
cue.write('PERFORMER "DJ name"\n')
cue.write(f'TITLE \"showname {show_date}\"\n')
cue.write(f'FILE \"filename-{show_date}.mp3\"\n')
fieldnames = ["start time", "end time", "duration", "title", "artist",
"album"]
csvwriter = csv.DictWriter(csvfile, fieldnames=fieldnames)
csvwriter.writeheader()
for track_info in playlist_to_convert:
artist = track_info[0]
title = track_info[1]
album = track_info[2]
track_length = int(track_info[3])
tindex = track_info[4]
# In CUE Format, the first track is at timestamp 00:00:00(MM:SS:FF
# where F is frames and don't matter to me). Subsequent tracks have
# a timestamp equal to the accumulated lengths of all previous tracks
if tindex == 0:
begin_seconds = 0
playlist_length = track_length
else:
begin_seconds = playlist_length
playlist_length += track_length
length_formatted = str(datetime.timedelta(
seconds=track_length)).split(':', maxsplit=1)[1]
timestamp_begin = str(datetime.timedelta(
seconds=begin_seconds)).split(':', maxsplit=1)[1]
timestamp_end = str(datetime.timedelta(
seconds=playlist_length)).split(':', maxsplit=1)[1]
csvwriter.writerow(
{f'start time': f'{show_date} 00:{timestamp_begin}', 'end time': f'{show_date} 00:{timestamp_end}',
'duration': f'{length_formatted}', 'title': f'{title}', 'artist': f'{artist}', 'album': f'{album}'})
cue.write(
f'TRACK {str(tindex).zfill(2)} AUDIO\n'
f' PERFORMER "{artist}"\n'
f' TITLE "{title}"\n'
f' INDEX 01 {timestamp_begin}:00\n')
db.close()