Skip to content

Instantly share code, notes, and snippets.

View makispl's full-sized avatar

Plegas Gerasimos makispl

View GitHub Profile
@makispl
makispl / README-Template.md
Created January 15, 2020 16:37 — forked from PurpleBooth/README-Template.md
A template to make good README.md

Project Title

One Paragraph of project description goes here

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Prerequisites

@makispl
makispl / auth.py
Last active March 6, 2020 11:00
Authorization Flow
# Declare the credentials
cid = 'XXXX'
secret = 'XXXX'
redirect_uri='http://localhost:7777/callback'
username = 'XXXX'
# Authorization flow
scope = 'user-top-read'
token = util.prompt_for_user_token(username, scope, client_id=cid, client_secret=secret, redirect_uri=redirect_uri)
pip install spotipy
pip install requests
# Import the libraries
import os
import pandas as pd
import numpy as np
import json
import matplotlib.pyplot as plt
import seaborn as sns
import spotipy
import spotipy.util as util
from spotipy.oauth2 import SpotifyClientCredentials
export SPOTIPY_CLIENT_ID="XXXX"
export SPOTIPY_CLIENT_SECRET="XXXX"
export SPOTIPY_REDIRECT_URI="http://localhost:7777/callback"
def fetch_playlists(sp, username):
"""
Returns the user's playlists.
"""
id = []
name = []
num_tracks = []
# Make the API request
def fetch_playlist_tracks(sp, username, playlist_id):
"""
Returns the tracks for the given playlist.
"""
offset = 0
tracks = []
# Make the API request
while True:
def fetch_audio_features(sp, username, playlist_id):
"""
Returns the selected audio features of every track,
for the given playlist.
"""
# Use the fetch_playlist_tracks function to fetch all of the tracks
playlist = fetch_playlist_tracks(sp, username, playlist_id)
index = 0
audio_features = []
def create_playlist(sp, username, playlist_name, playlist_description):
playlists = sp.user_playlist_create(username, playlist_name, description = playlist_description)
def enrich_playlist(sp, username, playlist_id, playlist_tracks):
index = 0
results = []
while index < len(playlist_tracks):
results += sp.user_playlist_add_tracks(username, playlist_id, tracks = playlist_tracks[index:index + 100])
index += 100