-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
123 lines (91 loc) · 3.63 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import streamlit as st
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
from dotenv import load_dotenv
from Code.Class.Search import Search
from Code.Utilities.download import convert_df
###### APP INFO AND LAYOUT ######
app_name = "Spotilist"
# Set general settings for app
st.set_page_config(
page_title=f"{app_name}",
initial_sidebar_state="expanded",
menu_items={
"About": f"# {app_name} \n Designed and engineered by Giacomo Piccinini"
},
)
st.title("Spotilist")
###### DESCRIPTION ######
st.markdown("## Introduction")
st.markdown(
"A direct way to be featured in a Spotify playlist is to contact the curator directly. If you are not yet famous, you may want to restrict yourself to playlists that:"
)
st.markdown("- Cover your genre;")
st.markdown("- Include emerging artists.")
st.markdown(
"The purpose of this app is to find, amongst all the Spotify playlists, those that meet your search criteria (i.e., your query)."
)
###### HOW IT WORKS ######
st.markdown("## How it works")
st.markdown(
"The app works like this: you are asked to enter a query for your search. This should contain a description of the type of playlist you are looking for. For example, '*modern progressive rock*'."
)
st.markdown(
"In addition, you will have to enter the total number of playlists to be displayed and the curators to be excluded."
)
st.markdown(
"This is to avoid considering curators who are too famous and whom you cannot reasonably address."
)
st.markdown(
"Finally, you might want to exclude playlists whose *least famous* artist has more than a certain number of followers. To do so, adjust the the 'Maximum number of followers' parameter."
)
###### IMPLEMENTATION ######
st.markdown("## Search")
# Load variables from .env file and export them
load_dotenv()
# Initialize Spotify Credential manager
authentication = SpotifyClientCredentials()
# Initialize Spotify client
spotify = spotipy.Spotify(auth_manager=authentication)
# Search query
query = st.text_input(
label="Search query", placeholder="Example: modern progressive rock"
)
# Number of results
n_results = st.number_input(
label="Number of playlists", min_value=1, max_value=100, value=10
)
# Curators to exclude
curators_to_exclude = st.text_input(
"Curators to exclude (separate with comma)",
placeholder="Spotify, Century Media Records, The Sounds of Spotify",
)
# Cutoff to be applied on followers
followers_cutoff = st.number_input(label="Maximum number of followers", value=100)
# Pre-process curators
curators_to_exclude = curators_to_exclude.split(",")
curators_to_exclude = [curator.lstrip() for curator in curators_to_exclude]
if query:
# Initialise search class
search = Search(n_results, spotify, query, curators_to_exclude, followers_cutoff)
# Button for actually running search
click = st.button(label="Run search", on_click=search.search)
if click:
###### RESULTS ######
if st.session_state["search"].error:
st.error("There was an error running the search. Please amend the parameters and try again.")
else:
st.markdown("## Results")
# Only select relevant fields
curated_df = st.session_state["search"].df[
["PlaylistName", "Followers", "CuratorName", "CuratorFollowers"]
]
# Show results
st.dataframe(curated_df)
# Download CSV with results
st.download_button(
"Download playlists details CSV",
convert_df(curated_df),
f"playlists.csv",
"test/csv",
)