Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
pmhalvor committed Feb 17, 2021
2 parents 3c9676b + 65a74d0 commit 7e3a0e7
Show file tree
Hide file tree
Showing 28 changed files with 5,390 additions and 76 deletions.
Binary file modified home/__pycache__/models.cpython-38.pyc
Binary file not shown.
Binary file modified home/__pycache__/views.cpython-38.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion home/templates/home/cv.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ <h2 style="color:#0000f2">Languages</h2>
{% if item.sub != " " %}
<em>{{ item.sub }}</em><br/>
{% endif %}
{{ now.year|sub:item.start.year }} yr {{ now.month|sub:item.start.month }} m
{{ now.year|sub:item.start.year }} yr {{ item.start.month }} m
</p>
{% endfor %}
</div>
Expand Down
77 changes: 19 additions & 58 deletions home/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
from django.views import generic
from home.models import Cv, Update, Notes, Code
from django.utils import timezone
from django import template
from datetime import timedelta

register = template.Library()

class CvView(generic.ListView):
template_name = 'home/cv.html'
Expand All @@ -12,11 +16,25 @@ def get_context_data(self, **kwargs):
context['cv_list'] = Cv.objects.all()
context['work_list'] = Cv.objects.filter(cat='Work')
context['edu_list'] = Cv.objects.filter(cat='Education')
context['lang_list'] = Cv.objects.filter(cat='Language').order_by('start')
context['lang_list'] = self.get_lang_list_correct_time()
context['now'] = timezone.now()

return context

def get_lang_list_correct_time(self):
lang_list = []
now = timezone.now()
for item in Cv.objects.filter(cat='Language').order_by('start'):
if item.start.month > now.month:
year = item.start.year + 1
month = 12 - abs(now.month - item.start.month)
kwargs = {'year':year, 'month':month}
item.start = item.start.replace(**kwargs)

lang_list.append(item)
return lang_list



class UpdateView(generic.ListView):
template_name = 'home/index.html'
Expand Down Expand Up @@ -67,63 +85,6 @@ def visuals(request):



# class IndexView(generic.ListView):
# template_name = 'old_site/index.html'
# context_object_name = 'latest_question_list'

# def get_queryset(self):
# """Return the last fine puclished questions."""
# return Question.objects.filter(
# pub_date__lte=timezone.now() # lte: less than equal
# ).order_by('-pub_date')[:5]


# class AboutView(generic.DetailView):
# model = About
# template_name = 'old_site/about.html'

# def get_queryset(self):
# """
# Excludes any questions that aren't published yet.
# """
# return About.objects.filter(pub_date__lte=timezone.now()) #less than equal to

# class CodeView(generic.DetailView):
# model = Question
# template_name = 'old_site/code.html'

# def get_queryset(self):
# return Question.objects.filter(pub_date__lte=timezone.now())


# class CvView(generic.DetailView):
# model = Question
# template_name = 'old_site/cv.html'

# def get_queryset(self):
# return Question.objects.filter(pub_date__lte=timezone.now())


# class NotesView(generic.DetailView):
# model = Question
# template_name = 'old_site/notes.html'

# def get_queryset(self):
# """
# Excludes any questions that aren't published yet.
# """
# return Question.objects.filter(pub_date__lte=timezone.now()) #less than equal to


# class VisualsView(generic.DetailView):
# model = Question
# template_name = 'polls/results.html'

# def get_queryset(self):
# return Question.objects.filter(pub_date__lte=timezone.now())






Expand Down
Binary file added media/notes/MLPNetwork.pdf
Binary file not shown.
Binary file modified phsite/__pycache__/settings.cpython-38.pyc
Binary file not shown.
Binary file modified phsite/__pycache__/urls.cpython-38.pyc
Binary file not shown.
7 changes: 5 additions & 2 deletions phsite/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static
from django.views.static import serve

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('home.urls')),
path('radio/', include('radio.urls')),
path(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
path(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
]


urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
# urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Binary file modified radio/__pycache__/models.cpython-38.pyc
Binary file not shown.
Binary file modified radio/__pycache__/urls.cpython-38.pyc
Binary file not shown.
Binary file modified radio/__pycache__/views.cpython-38.pyc
Binary file not shown.
10 changes: 10 additions & 0 deletions radio/templates/includes/plots.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1>favorites</h1>
<div>
top artists past 6 months
{{ plot_artists.plot | safe}}
</div>
<br><br>
<div>
top songs past 6 months
{{ plot_songs.plot | safe}}
</div>
5 changes: 4 additions & 1 deletion radio/templates/radio/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ <h1 style="text-align: right; padding-right:10px;">recently played</h1>
{% include "includes/recents.html" %}
</ul>
</div>

<br>
<div class=plots>
{% include "includes/plots.html" %}
</div>
</div>


Expand Down
4 changes: 2 additions & 2 deletions radio/urls.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from django.contrib import admin
from django.urls import include, path
from . import views
from .worker import search, submit
from .worker import search, submit, plot

urlpatterns = [
path('', views.radio, name='radio'),
path('current/', views.Http_current, name='current'),
path('recents/', views.Http_recents, name='recents'),
path('search/', search.Http_search, name='search'),
path('submit/', submit.submit_suggestions , name='submit'),
]
]
13 changes: 13 additions & 0 deletions radio/urls.py.save
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.contrib import admin
from django.urls import include, path
from . import views
from .worker import search, submit

urlpatterns = [
path('', views.radio, name='radio'),
path('current/', views.Http_current, name='current'),
path('recents/', views.Http_recents, name='recents'),
path('search/', search.Http_search, name='search'),
path('submit/', submit.submit_suggestions , name='submit'),
path('play/', views.play, name='play'),
]
15 changes: 12 additions & 3 deletions radio/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
from .worker.authorize import get_token
from datetime import datetime, timedelta
import json


from .worker.plot import artist_duration, song_plays
from django.views.generic.base import TemplateView
import plotly.offline as opy
import plotly.graph_objs as go

def index(request):
return HttpResponse("Welcome to Per's Radio!")
Expand All @@ -23,6 +25,8 @@ def radio(request):
# """
context['recents'] = recents()
context['current'] = current()
context['plot_artists'] = artist_duration()
context['plot_songs'] = song_plays()
return render(request, 'radio/index.html', context)


Expand Down Expand Up @@ -78,7 +82,11 @@ def recents():
if data:
for song in data[:20]:
track_url = song['track']['external_urls']['spotify']
played_at = str(datetime.strptime(song['played_at'], '%Y-%m-%dT%H:%M:%S.%fZ')
try:
played_at = str(datetime.strptime(song['played_at'], '%Y-%m-%dT%H:%M:%S.%fZ')
+ timedelta(hours=1))[:-3]
except:
played_at = str(datetime.strptime(song['played_at'], '%Y-%m-%dT%H:%M:%SZ')
+ timedelta(hours=1))[:-3]
name = song['track']['name'].replace(',','')
song_id = song['track']['id']
Expand All @@ -97,3 +105,4 @@ def Http_current(request):

def Http_recents(request):
return render(None, 'includes/recents.html', {'recents': recents()})

Binary file modified radio/worker/__pycache__/authorize.cpython-38.pyc
Binary file not shown.
Binary file added radio/worker/__pycache__/plot.cpython-38.pyc
Binary file not shown.
Binary file modified radio/worker/__pycache__/search.cpython-38.pyc
Binary file not shown.
Binary file modified radio/worker/__pycache__/song_history.cpython-38.pyc
Binary file not shown.
Binary file modified radio/worker/__pycache__/submit.cpython-38.pyc
Binary file not shown.
96 changes: 96 additions & 0 deletions radio/worker/plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
from datetime import datetime, timedelta
try:
from .song_history import download_to_df, get_durations
except:
from song_history import download_to_df, get_durations
import pandas as pd
import plotly.offline as opy
pd.options.plotting.backend = "plotly"

####### being used
def to_html(figure):
context = {}
div = opy.plot(figure, auto_open=False, output_type='div')
context['plot'] = div
return context

def artist_duration(n=37):
df, mdf = download_to_df()

durations = get_durations(df.id.unique())
df = df.merge(durations, on='id', how='left')

df_artist_track = df.groupby(['artist', 'id'])
duration = df_artist_track['duration'].sum()
count = df_artist_track['count'].sum()

total_time_artist = (count*duration).groupby('artist').sum()
total_time_artist.sort_values(ascending=True, inplace=True)

top_artists_ms = total_time_artist.tail(n)

top_artists = top_artists_ms//(1000*60*60)

top_artists = pd.DataFrame(top_artists).reset_index()
top_artists.columns=['artist', 'time']
top_artists.index = [f'Rank: {n-i}' for i in range(n)]
print(top_artists)

figure = top_artists.plot.barh(
x = 'time',
y = 'artist',
color='time',
hover_name = top_artists.index,
template='plotly_dark')
figure.update(layout_showlegend=False)

return to_html(figure)

def song_plays(n=37):
print('Downloading to df()...')
df, mdf = download_to_df()

df.rename(columns={'name':'track'}, inplace=True)
name_artist = df.groupby(['track', 'artist'])
_counts = name_artist.size().reset_index(name='count')
sorted_counts = _counts.sort_values('count', ascending=True)
top_songs = sorted_counts.tail(n)

top_songs.index = ['Rank: '+str(n-i) for i in range(n)]
print(top_songs)

figure = top_songs.plot.barh(
y = 'track',
x = 'count',
custom_data = ['artist'],
color='count',
hover_name = top_songs.index,
hover_data=['artist', 'track'],
template='plotly_dark')
figure.update_layout(
showlegend=False
)

return to_html(figure)

######################

'''
Checklist of things to do:
X hours per artist
X songs plays with color
- genre plots?
'''

if __name__=='__main__':
# print('Downloading to df()...')
# df, mdf = download_to_df()

# song_plays()
artist_duration()

print('end')




Loading

0 comments on commit 7e3a0e7

Please sign in to comment.