From 6688dad512226c80af3a2856b87409e537a97def Mon Sep 17 00:00:00 2001 From: Lasse Edfast <8794658+lasseedfast@users.noreply.github.com> Date: Thu, 4 Mar 2021 10:34:26 +0100 Subject: [PATCH] Add files via upload --- musicreport.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 musicreport.py diff --git a/musicreport.py b/musicreport.py new file mode 100644 index 0000000..4f69d04 --- /dev/null +++ b/musicreport.py @@ -0,0 +1,36 @@ +#! python3 +import pandas as pd + +# Import clipboard to dataframe +## There's something off with the format when copied frmo Hindenburg, +## therefore the extra column +df = pd.read_clipboard( + sep="\t", names=["offset", "duration", "title", "artist", "album", ""] +) +df.drop(["album", "", "offset"], axis=1, inplace=True) + +# Translate duration for each clip to seconds +df["seconds"] = df.duration.apply( + lambda x: int(x[: x.find(":")]) * 60 + int(x[x.find(":") + 1 :]) +) + +# Open and prepare file +with open("Music Report.csv", "a+") as f: + f.truncate(0) + f.write("title\tartist\tduration\n") + + # Sum the duration for all clips with the same title + for title in df.groupby("title"): + minutes = str(int(title[1].seconds.sum() / 60)) + seconds = str(int(title[1].seconds.sum() % 60)) + if len(seconds) == 1: + seconds = seconds + str(0) + + # Write to file + f.write( + "{title}\t{artist}\t{duration}\n".format( + title=title[0], + artist=title[1].artist.tolist()[0], + duration=minutes + ":" + seconds, + ) + )