diff --git a/.gitignore b/.gitignore index e3e27f2..15e38b0 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ tracks/** cache_xevioni/** .cache-* auth.py +export/** # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/main.py b/main.py index 1281a47..ec58269 100644 --- a/main.py +++ b/main.py @@ -11,8 +11,9 @@ def main(): logging.basicConfig(level=logging.INFO) logging.info('Pulling data from Spotify') refresh() + process.main() + - # Refreshes tracks from files if the token from Spotipy has expired, # thus keeping us up to date in most cases while keeping rate limits def refresh(): @@ -24,5 +25,5 @@ def refresh(): pull.main() else: logging.info('Spotify data deemed to be recent enough (under {} seconds old)'.format(cache['expires_in'])) - + pull.main() main() \ No newline at end of file diff --git a/process.py b/process.py index 26d7bd0..bf94a4d 100644 --- a/process.py +++ b/process.py @@ -13,7 +13,6 @@ import matplotlib.pyplot as plt def get_files(): folder = os.path.join(sys.path[0], 'tracks') files = [] - print(os.listdir(folder)) for file in os.listdir(folder): with open(os.path.join(os.path.join(folder, file))) as file: files.append( @@ -54,28 +53,34 @@ def process_data(data): explicit.append(item[1]) # Done processing date properly, start plotting work + logging.info('Processed data, creating plot from data') + # Weird numpy stuff n = len(scores.values()) ind = np.arange(n) width = 0.55 - + # 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.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')) - logging.info('Showing plot to User') - plt.show() - + fig = plt.gcf() # Magic to save to image and then show + # Save the figure, overwriting anything in your way logging.info('Saving the figure to the \'export\' folder') export_folder = os.path.join(sys.path[0], 'export') if not os.path.exists(export_folder): os.makedirs(export_folder) - plt.savefig(os.path.join(export_folder, datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S'))) + plt.draw() + fig.savefig(os.path.join(export_folder, datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S'))) + # Finally show the figure to + logging.info('Showing plot to User') + plt.show() + # Copy the figure to your clipboard to paste in Excel # logging.info('Copying the plot data to clipboard') # copy(months, safe, explicit) @@ -99,6 +104,4 @@ 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) - -main() \ No newline at end of file + process_data(data) \ No newline at end of file diff --git a/pull.py b/pull.py index 3e40e29..ce649ff 100644 --- a/pull.py +++ b/pull.py @@ -23,11 +23,13 @@ def main(): sp = spotipy.Spotify(auth=token) logging.info('Authorized with Spotify via Spotipy') + tracks_folder = os.path.join(sys.path[0], 'tracks') logging.warning('Clearing all files in tracks folder for new files') - shutil.rmtree(os.path.join(sys.path[0], 'tracks')) + 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 logging.info('Cleared folder, ready to download new track files') - root = sys.path[0] curoffset, curlimit = 0, 50 while curoffset >= 0: logging.info('Requesting tracks {} to {}'.format(curoffset, curoffset + curlimit)) @@ -35,7 +37,7 @@ def main(): received = len(response['items']) logging.info('Received tracks {} to {}'.format(curoffset, curoffset + received)) filename = f'saved-tracks-{curoffset}-{curoffset + received}.json' - filepath = os.path.join(root, 'tracks', filename) + filepath = os.path.join(tracks_folder, filename) 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)))