repl.it
@ShadowCypher/

Smartify

Django

Smartify comes up with the most consistent album ordering by matching songs up by similarity.

fork
loading
Files
  • main
    • views.py
    • __pycache__
    • migrations
    • forms.py
    • manage.py
    • models.py
    • music.py
    • settings.py
    • urls.py
    • wsgi.py
  • src
  • static
  • templates
  • db.sqlite3
  • Packager files
  • requirements.txt
main/views.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import uuid
from django.shortcuts import render, redirect
from main.music import Track, Album
from .forms import URLForm, PlaylistForm
albums = {}

def generate_id():
  return str(uuid.uuid1())


def get_next(request):
    try:
        album = albums.get(request.session["token"])
        album.get_similar()
        score = album.scores[album.play_index]
        playlist = album.get_queue()
        if score["instrumentalness"] < 0.6:
          lyrics = album.get_lyrics()["lyrics"].split("\n") # Gets lyrics from my own Lyrics API
        else:
          lyrics = []
        return render(request, template_name="index.html", context={
            "name": "Player",
            "description": "Smartify's song player powered by the Spotify API",
            "album_page": False,
            "song": album.current_song_properties,
            "art": album.get_album_art(),
            "token": album.token,
            "album": album.is_album,
            "danceability": score["danceability"],
            "energy": score["energy"],
            "valence": score["valence"],
            "speechiness": score["speechiness"],
            "key": score["key"],
            "instrumentalness": score["instrumentalness"],
            "deviation": round(album.std_dev, 2),
            "lyrics": lyrics,
            "queue": playlist
            #"lyrics": genius.search_song(album.current_song_properties["name"], album.current_song_properties["artists"][0]["name"])
        })
    except Exception as e:
        print(e)
        return redirect("/")

def get_previous(request):
  ""
  album = albums.get(request.session["token"])
  album.move_back()
  score = album.scores[album.play_index] # Sets features to the new song's index
  playlist = album.get_queue()
  return render(request, template_name="index.html", context={
      "name": "Player",
      "description": "Smartify's song player powered by the Spotify API",
      "album_page": False,
      "song": album.current_song_properties,
      "art": album.get_album_art(),
      "token": album.token,
      "album": album.is_album,
      "danceability": score["danceability"],
      "energy": score["energy"],
      "valence": score["valence"],
      "speechiness": score["speechiness"],
      "key": score["key"],
      "instrumentalness": score["instrumentalness"],
      "deviation": round(album.std_dev, 2),
      "lyrics": [],
      "queue": playlist
      #"lyrics": genius.search_song(album.current_song_properties["name"], album.current_song_properties["artists"][0]["name"])
  })

def index(request):
    form = URLForm()
    if request.method == "POST":
        form = URLForm(request.POST)
        if form.is_valid():
            album = Album(ID=form.cleaned_data["id"], album=True)
            albums[request.session['token']] = album
            album.get_song_properties()
            album.compute_scores()
            score = album.scores[album.play_index]
            if score["instrumentalness"] < 0.6:
              lyrics = album.get_lyrics()["lyrics"].split("\n")
            else:
              lyrics = []
            playlist = album.get_queue()
            ctx = {
                "name": "Homepage",
                "description": "Smartify - The Smart Way to Listen to Music",
                "album_page": not request.method == "POST",
                "song": album.current_song_properties,
                "art": album.get_album_art(),
                "token": album.token,
                "album": True,
                "danceability": score["danceability"],
                "energy": score["energy"],
                "valence": score["valence"],
                "speechiness": score["speechiness"],
                "key": score["key"],
                "instrumentalness": score["instrumentalness"],
                "deviation": round(album.std_dev, 2),
                "lyrics": lyrics,
                "queue": playlist
            }
            return render(request, template_name="index.html", context=ctx)
    else:
        ctx = {
            "name": "Homepage",
            "description": "Welcome to Smartify - the smart way to listen to music",
            "album_page": not request.method == "POST",
            "form": form
        }
        request.session["token"] = generate_id()
        return render(request, template_name="index.html", context=ctx)

def faq(request):
  ctx = {
    "name": "Frequently Asked Questions",
    "description": "If you don't understand what Smartify is, we get it; it can be hard to understand. This page should answer your questions."
  }
  return render(request, template_name="faq.html", context=ctx)

def album(request):
  form = PlaylistForm()
  if request.method == "POST":
    form = PlaylistForm(request.POST)
    if form.is_valid():
      results = Album.find(name=form.cleaned_data["id"], is_album=True)
      ctx = {
        "name": "Results",
        "description": "Search Results | Album",
        "results": results
      }
      return render(request, template_name="album_results.html", context=ctx)
  ctx = {
    "name": "Albums",
    "description": "Find an Album on spotify",
    "type": "Album",
    "form": form
  }
  return render(request, template_name="playlists.html", context=ctx)

def search_album(request, album_id):
  album = Album(ID=album_id, album=True)
  albums[request.session['token']] = album
  album.get_song_properties()
  album.compute_scores()
  score = album.scores[album.play_index]
  if score["instrumentalness"] < 0.6:
    lyrics = album.get_lyrics()["lyrics"].split("\n") # Gets lyrics from my own Lyrics API
  else:
    lyrics = []
  playlist = album.get_queue()
  ctx = {
      "name": "Album",
      "description": "Album Autoplay (ft. Smartify)",
      "album_page": False,
      "song": album.current_song_properties,
      "art": album.get_album_art(),
      "token": album.token,
      "album": album.is_album,
      "danceability": score["danceability"],
      "energy": score["energy"],
      "valence": score["valence"],
      "speechiness": score["speechiness"],
      "key": score["key"],
      "instrumentalness": score["instrumentalness"],
      "lyrics": lyrics,
      "queue": playlist
  }
  return render(request, template_name="index.html", context=ctx)

def search_playlist(request, playlist_id):
  album = Album(ID=playlist_id, album=False)
  albums[request.session['token']] = album
  album.get_song_properties()
  album.compute_scores()
  score = album.scores[album.play_index]
  lyrics = []
  if score["instrumentalness"] < 0.6:
    lyrics = album.get_lyrics()["lyrics"].split("\n")
  playlist = album.get_queue()
  ctx = {
      "name": "Playlist",
      "description": "Playlist Autoplay (ft. Smartify)",
      "album_page": False,
      "song": album.current_song_properties,
      "art": album.get_album_art(),
      "token": album.token,
      "album": album.is_album,
      "danceability": score["danceability"],
      "energy": score["energy"],
      "valence": score["valence"],
      "speechiness": score["speechiness"],
      "key": score["key"],
      "instrumentalness": score["instrumentalness"],
      "lyrics": lyrics,
      "queue": playlist
  }
  return render(request, template_name="index.html", context=ctx)

def playlist(request):
  form = PlaylistForm()
  if request.method == "POST":
    form = PlaylistForm(request.POST)
    if form.is_valid():
      results = Album.find(name=form.cleaned_data["id"])
      ctx = {
        "name": "Results",
        "description": "Search Results | Playlist",
        "results": results
      }
      return render(request, template_name="playlist_results.html", context=ctx)
  ctx = {
    "name": "Playlists",
    "description": "Find a Playlist on Spotify",
    "type": "Playlist",
    "form": form
  }
  return render(request, template_name="playlists.html", context=ctx)

def register_user(request):
  album = albums.get(request.session["token"])
  album.regenerate_token(code=request.GET["code"])
  playlist_id = (album.get_playlist()) # Create playlist
  return redirect("/success/"+playlist_id)

def success(request, id):
  # album = albums.get(request.session["token"])
  form = URLForm()
  ctx = {
      "name": "Homepage",
      "description": "Welcome to Smartify - the smart way to listen to music",
      "album_page": not request.method == "POST",
      "form": form,
      "playlist_id": id
  }
  return render(request, "index.html", context=ctx)
Fetching token
?