finish work for plotting with copy function

This commit is contained in:
Xevion
2019-10-27 16:32:53 -05:00
parent cff38fbaa9
commit 2c7417cb5a

View File

@@ -5,11 +5,14 @@ import logging
import collections import collections
import numpy as np import numpy as np
import dateutil.parser import dateutil.parser
import PIL.Image as Image
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
# Gets all files in tracks folder, returns them in parsed JSON
def get_files(): def get_files():
folder = os.path.join(sys.path[0], 'tracks') folder = os.path.join(sys.path[0], 'tracks')
files = [] files = []
print(os.listdir(folder))
for file in os.listdir(folder): for file in os.listdir(folder):
with open(os.path.join(os.path.join(folder, file))) as file: with open(os.path.join(os.path.join(folder, file))) as file:
files.append( files.append(
@@ -17,12 +20,14 @@ def get_files():
) )
return files return files
# Simple function to combine a bunch of items from different files
def combine_files(files): def combine_files(files):
items = [] items = []
for file in files: for file in files:
items.extend(file['items']) items.extend(file['items'])
return items return items
# Prints the data in a interesting format
def print_data(data): def print_data(data):
for i, item in enumerate(data): for i, item in enumerate(data):
date = dateutil.parser.parse(item['added_at']) date = dateutil.parser.parse(item['added_at'])
@@ -32,26 +37,45 @@ def print_data(data):
print('[{}] {} "{}" by {}'.format(date, explicit, track_name, artists)) print('[{}] {} "{}" by {}'.format(date, explicit, track_name, artists))
def process_data(data): def process_data(data):
explicit = collections.defaultdict(int) # Process the data by Month/Year, then by Safe/Explicit
safe = collections.defaultdict(int) scores = {}
for item in data: for item in data:
date = dateutil.parser.parse(item['added_at']) date = dateutil.parser.parse(item['added_at']).strftime('%b %Y')
date = date.strftime('%b %Y') if date not in scores.keys():
if item['track']['explicit']: scores[date] = [0, 0]
explicit[date] += 1 scores[date][1 if item['track']['explicit'] else 0] += 1
else:
safe[date] += 1
times = [dateutil.parser.parse(item['added_at']) for item in data]
sort_func = lambda x : dateutil.parser.parse(x[0]).epoch
# Create simplified arrays for each piece of data
months = list(scores.keys())[::-1]
safe, explicit = [], []
for item in list(scores.values())[::-1]:
safe.append(item[0])
explicit.append(item[1])
n = len(data) # Done processing date properly, start plotting work
n = len(scores.values())
ind = np.arange(n) ind = np.arange(n)
width = 0.35 width = 0.55
plt.ylabel('Month')
plt.xlabel('Song by Type') p1 = plt.bar(ind, explicit, width)
plt.xticks(ind, ) p2 = plt.bar(ind, safe, width, bottom=explicit) # bottom= just has the bar sit on top of the explicit
plt.title('Song by Safe/Explicit')
plt.ylabel('Song Count')
plt.xlabel('Month')
plt.xticks(ind, months, rotation=270) # Rotation 90 will have the
plt.legend((p1[0], p2[0]), ('Explicit', 'Safe'))
# plt.show() # plt.show()
copy(months, safe, explicit)
# Simple method for exporting data to a table like format
# Will paste into Excel very easily
def copy(months, safe, explicit):
from pyperclip import copy
top = 'period\tsafe\texplicit\n'
copy(top + '\n'.join([
f'{item[0]}\t{item[1]}\t{item[2]}' for item in zip(months, safe, explicit)
]))
def main(): def main():
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
@@ -60,7 +84,9 @@ def main():
logging.info(f"Read and parse {len(files)} track files") logging.info(f"Read and parse {len(files)} track files")
logging.info("Combining into single track file for ease of access") logging.info("Combining into single track file for ease of access")
data = combine_files(files) data = combine_files(files)
data.sort(key=lambda item : dateutil.parser.parse(item['added_at']).timestamp(), reverse=True)
logging.info(f'File combined with {len(data)} items') logging.info(f'File combined with {len(data)} items')
logging.info('Processing file...') logging.info('Processing file...')
process_data(data) process_data(data)
main() main()