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_xevioni/**
.cache-* .cache-*
auth.py auth.py
export/**
# Byte-compiled / optimized / DLL files # Byte-compiled / optimized / DLL files
__pycache__/ __pycache__/

View File

@@ -11,8 +11,9 @@ def main():
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
logging.info('Pulling data from Spotify') logging.info('Pulling data from Spotify')
refresh() refresh()
process.main()
# Refreshes tracks from files if the token from Spotipy has expired, # Refreshes tracks from files if the token from Spotipy has expired,
# thus keeping us up to date in most cases while keeping rate limits # thus keeping us up to date in most cases while keeping rate limits
def refresh(): def refresh():
@@ -24,5 +25,5 @@ def refresh():
pull.main() pull.main()
else: else:
logging.info('Spotify data deemed to be recent enough (under {} seconds old)'.format(cache['expires_in'])) logging.info('Spotify data deemed to be recent enough (under {} seconds old)'.format(cache['expires_in']))
pull.main()
main() main()

View File

@@ -13,7 +13,6 @@ import matplotlib.pyplot as plt
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(
@@ -54,28 +53,34 @@ def process_data(data):
explicit.append(item[1]) explicit.append(item[1])
# Done processing date properly, start plotting work # Done processing date properly, start plotting work
logging.info('Processed data, creating plot from data')
# Weird numpy stuff
n = len(scores.values()) n = len(scores.values())
ind = np.arange(n) ind = np.arange(n)
width = 0.55 width = 0.55
# Bars
p1 = plt.bar(ind, explicit, width) 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 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 by Safe/Explicit')
plt.ylabel('Song Count') plt.ylabel('Song Count')
plt.xlabel('Month') plt.xlabel('Month')
plt.xticks(ind, months, rotation=270) # Rotation 90 will have the plt.xticks(ind, months, rotation=270) # Rotation 90 will have the
plt.legend((p1[0], p2[0]), ('Explicit', 'Safe')) plt.legend((p1[0], p2[0]), ('Explicit', 'Safe'))
logging.info('Showing plot to User') fig = plt.gcf() # Magic to save to image and then show
plt.show()
# Save the figure, overwriting anything in your way # Save the figure, overwriting anything in your way
logging.info('Saving the figure to the \'export\' folder') logging.info('Saving the figure to the \'export\' folder')
export_folder = os.path.join(sys.path[0], 'export') export_folder = os.path.join(sys.path[0], 'export')
if not os.path.exists(export_folder): if not os.path.exists(export_folder):
os.makedirs(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 # Copy the figure to your clipboard to paste in Excel
# logging.info('Copying the plot data to clipboard') # logging.info('Copying the plot data to clipboard')
# copy(months, safe, explicit) # copy(months, safe, explicit)
@@ -99,6 +104,4 @@ def main():
data.sort(key=lambda item : dateutil.parser.parse(item['added_at']).timestamp(), reverse=True) 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()

View File

@@ -23,11 +23,13 @@ def main():
sp = spotipy.Spotify(auth=token) sp = spotipy.Spotify(auth=token)
logging.info('Authorized with Spotify via Spotipy') 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') 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') logging.info('Cleared folder, ready to download new track files')
root = sys.path[0]
curoffset, curlimit = 0, 50 curoffset, curlimit = 0, 50
while curoffset >= 0: while curoffset >= 0:
logging.info('Requesting tracks {} to {}'.format(curoffset, curoffset + curlimit)) logging.info('Requesting tracks {} to {}'.format(curoffset, curoffset + curlimit))
@@ -35,7 +37,7 @@ def main():
received = len(response['items']) received = len(response['items'])
logging.info('Received tracks {} to {}'.format(curoffset, curoffset + received)) logging.info('Received tracks {} to {}'.format(curoffset, curoffset + received))
filename = f'saved-tracks-{curoffset}-{curoffset + received}.json' 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: with open(filepath, 'w+') as file:
json.dump(response, 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 "{}" ({}KB)'.format(f'\\tracks\\{filename}', round(os.path.getsize(filepath) / 1024, 2)))