/
scheduler.py
82 lines (67 loc) · 2.1 KB
/
scheduler.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
from random import shuffle
tracks_path = "/home/user/tracks_folder/"
separation_artist = 10
separation_title = 10
playlist_path = '/home/user/playlist_folder/playlist.m3u'
playlist_size = len(os.listdir(tracks_path))
nb_pl = 0
bac = []
playlist = []
artists = []
titles = []
error = []
class Track:
def __init__(self, artist, title, filename, duration=0):
self.artist = artist
self.title = title
self.filename = filename
self.duration = duration
def read_folder(folder):
for song_file in os.listdir(folder):
split = os.path.splitext(song_file)[0].split(' - ')
artist = split[0]
title = split[1]
filename = folder+song_file
bac.append(Track(artist, title, filename))
artists.append(artist)
titles.append(title)
def get_track(i):
j = i % len(bac)
print("i : %d" % i)
print("j : %d" % j)
track = bac[i]
print(track.artist)
playlist.append(track)
read_folder(tracks_path)
max_artist = len(list(set(artists)))/2
max_titles = len(list(set(titles)))/2
if separation_artist == 0:
error.append("Warning: separation_artist must be set and greater than 0.")
if separation_title == 0:
error.append("Warning: separation_title must be set and greater than 0.")
if playlist_size == 0:
error.append("Warning: playlist_size must be set and greater than 0.")
if max_artist < separation_artist:
error.append('Warning: separation_artist is too high: %d use a value between 1 and %d' % (separation_artist, max_artist))
if max_titles < separation_title:
error.append('Warning: separation_title is too high: %d use a value between 1 and %d' % (separation_title, max_titles))
if len(error) != 0:
for err in error:
print(err)
sys.exit(0)
shuffle(bac)
while True:
get_track(nb_pl)
nb_pl+=1
if playlist_size == nb_pl:
break
m3u = open(playlist_path, 'w')
m3u.write('#EXTM3U\n')
for song in playlist:
m3u.write('#EXTINF:%s,%s - %s\n' % ('-1',song.artist,song.title))
m3u.write(song.filename+'\n')
m3u.close()