reworking to run from main

This commit is contained in:
Xevion
2019-10-27 16:52:20 -05:00
parent 48798de547
commit 9108ce3259
4 changed files with 22 additions and 15 deletions

1
.gitignore vendored
View File

@@ -3,6 +3,7 @@ tracks/**
cache_xevioni/**
.cache-*
auth.py
export/**
# Byte-compiled / optimized / DLL files
__pycache__/

View File

@@ -11,6 +11,7 @@ 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,
@@ -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()

View File

@@ -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,27 +53,33 @@ 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')
@@ -100,5 +105,3 @@ def main():
logging.info(f'File combined with {len(data)} items')
logging.info('Processing file...')
process_data(data)
main()

View File

@@ -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)))