RotateMusic/rotateMusic.py

48 lines
1.2 KiB
Python
Raw Normal View History

2023-10-22 20:55:35 +02:00
import os
import random
import shutil
import sys
def get_dir_size(path):
total = 0
with os.scandir(path) as it:
for entry in it:
if entry.is_file():
total += entry.stat().st_size
elif entry.is_dir():
total += get_dir_size(entry.path)
return total
def main():
if len(sys.argv) != 4:
return
library = sys.argv[1]
player = sys.argv[2]
amount = int(sys.argv[3])
libraryArtists = os.listdir(library)
playerArtists = os.listdir(player)
librarySpace = {}
for item in libraryArtists:
librarySpace[item] = get_dir_size(library+"/"+item)
shutil.rmtree(player)
os.mkdir(player)
cumulativeTotal = 0
chosenArtists = []
while True:
artist = random.choice(libraryArtists)
libraryArtists.remove(artist)
if artist in playerArtists:
continue
if cumulativeTotal + librarySpace[artist] >= amount:
break
cumulativeTotal += librarySpace[artist]
shutil.copytree(library+"/"+artist, player+"/"+artist)
chosenArtists.append(artist)
print(100 * cumulativeTotal / amount)
if __name__ == "__main__":
main()