From af1de29ebb638927d388583c2080f8d85ede25ab Mon Sep 17 00:00:00 2001 From: Xevion Date: Sun, 27 Oct 2019 17:49:30 -0500 Subject: [PATCH] rework plot figure size to properly space labels --- main.py | 3 ++- process.py | 21 ++++++++++++++++----- pull.py | 28 +++++++++++++++++++++++----- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/main.py b/main.py index ec58269..7fc7675 100644 --- a/main.py +++ b/main.py @@ -25,5 +25,6 @@ def refresh(): pull.main() else: logging.info('Spotify data deemed to be recent enough (under {} seconds old)'.format(cache['expires_in'])) - pull.main() + else: + pull.main() main() \ No newline at end of file diff --git a/process.py b/process.py index bf94a4d..66806a5 100644 --- a/process.py +++ b/process.py @@ -58,11 +58,13 @@ def process_data(data): n = len(scores.values()) ind = np.arange(n) width = 0.55 - # Bars + # Resizer figuresize to be 2.0 wider + plt.figure(figsize=(10.0, 6.0)) + # Stacked Bars p1 = plt.bar(ind, explicit, width) p2 = plt.bar(ind, safe, width, bottom=explicit) # bottom= just has the bar sit on top of the explicit # Plot labeling - plt.title('Song by Safe/Explicit') + plt.title('Song Count by Safe/Explicit') plt.ylabel('Song Count') plt.xlabel('Month') plt.xticks(ind, months, rotation=270) # Rotation 90 will have the @@ -74,8 +76,15 @@ def process_data(data): export_folder = os.path.join(sys.path[0], 'export') if not os.path.exists(export_folder): os.makedirs(export_folder) - plt.draw() - fig.savefig(os.path.join(export_folder, datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S'))) + plt.tight_layout() + fig.savefig( + os.path.join( + export_folder, + datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S') + ), + dpi=100, + quality=95 + ) # Finally show the figure to logging.info('Showing plot to User') @@ -104,4 +113,6 @@ def main(): 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('Processing file...') - process_data(data) \ No newline at end of file + process_data(data) + +main() \ No newline at end of file diff --git a/pull.py b/pull.py index ce649ff..d833001 100644 --- a/pull.py +++ b/pull.py @@ -6,6 +6,7 @@ import shutil import pprint import spotipy import logging +from hurry.filesize import size, alternative import spotipy.util as util def main(): @@ -27,21 +28,38 @@ def main(): logging.warning('Clearing all files in tracks folder for new files') if os.path.exists(tracks_folder): shutil.rmtree(tracks_folder) # Delete folder and all contents (old track files) - os.path.makedirs(tracks_folder) # Recreate the folder just deleted + os.makedirs(tracks_folder) # Recreate the folder just deleted logging.info('Cleared folder, ready to download new track files') curoffset, curlimit = 0, 50 while curoffset >= 0: - logging.info('Requesting tracks {} to {}'.format(curoffset, curoffset + curlimit)) + # Request and identify what was received + logging.info('Requesting {} to {}'.format(curoffset, curoffset + curlimit)) response = sp.current_user_saved_tracks(limit=curlimit, offset=curoffset) received = len(response['items']) - logging.info('Received tracks {} to {}'.format(curoffset, curoffset + received)) + logging.info('Received {} to {}'.format(curoffset, curoffset + received)) + # Create path/filename filename = f'saved-tracks-{curoffset}-{curoffset + received}.json' filepath = os.path.join(tracks_folder, filename) + # Save track file with open(filepath, 'w+') as file: json.dump(response, file) - logging.info('Saved at "{}" ({}KB)'.format(f'\\tracks\\{filename}', round(os.path.getsize(filepath) / 1024, 2))) + logging.info('Saved at "{}" ({})'.format( + f'\\tracks\\{filename}', + size(os.path.getsize(filepath))) + ) + # Decide whether we have received all possible tracks if received < curlimit: - logging.info('Done requesting/saving tracks after {} tracks'.format(curoffset + received)) + logging.info('Requested and saved {} tracks split over {} files ({})'.format( + curoffset + received, + len(os.listdir(tracks_folder)), + size( + sum( + os.path.getsize(os.path.join(tracks_folder, file)) for file in os.listdir(tracks_folder) + ), + system=alternative + ) + )) break + # Continuing, so increment offset curoffset += curlimit \ No newline at end of file