Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: Retrieve PyCast data from Spotify and serve the data through API #1044

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ services:
EMAIL_URL: ${EMAIL_URL}
DSN_URL: ${DSN_URL}
GTM_TRACK_ID: ${GTM_TRACK_ID}
SPOTIPY_CLIENT_ID: ${SPOTIPY_CLIENT_ID}
SPOTIPY_CLIENT_SECRET: ${SPOTIPY_CLIENT_SECRET}
SLACK_WEBHOOK_URL: ${SLACK_WEBHOOK_URL}

volumes:
Expand Down
2 changes: 2 additions & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ sorl-thumbnail==12.6.2
# http://www.grantjenks.com/docs/sortedcontainers/
sortedcontainers==2.1.0

spotipy

# Tabulate, an utility for pretty-print tabular data
# https://bitbucket.org/astanin/python-tabulate
tabulate==0.8.6
Expand Down
8 changes: 8 additions & 0 deletions src/events/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,11 @@ class Meta:
"youtube_id",
"social_item"
]


class PyCastAPISerializer(serializers.Serializer):
id = serializers.CharField()
title = serializers.CharField()
duration = serializers.IntegerField()
description = serializers.CharField()
release_date = serializers.CharField()
1 change: 1 addition & 0 deletions src/events/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
path('keynotes/', views.KeynoteEventListAPIView.as_view()),
path('speeches/', views.SpeechListAPIView.as_view()),
path('speeches/<str:event_type>/<int:pk>/', views.SpeechDetailAPIView.as_view()),
path('pycast/', views.PyCastAPIView.as_view()),
]
44 changes: 44 additions & 0 deletions src/events/api/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import collections
from typing import List, Union
import time

from rest_framework.generics import RetrieveAPIView, ListAPIView
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

from django.conf import settings
from django.db.models import Count
Expand Down Expand Up @@ -308,3 +311,44 @@ class KeynoteEventListAPIView(ListAPIView):

queryset = KeynoteEvent.objects.all()
serializer_class = serializers.KeynoteEventSerializer


class PyCastAPIView(APIView):
sp_client = None
show = {}
updated_at = time.time()
pycast_show_id = '63C4CNtJywIKizNFHRrIGv'

def get_sp_client(self):
if self.sp_client:
return self.sp_client
try:
auth_manager = SpotifyClientCredentials()
self.sp_client = spotipy.Spotify(auth_manager=auth_manager)
except spotipy.oauth2.SpotifyOauthError:
raise Http404
return self.sp_client

def get(self, request):
sp_client = self.get_sp_client()

now = time.time()
if not self.show or (now - self.updated_at) > 6 * 60 * 60:
self.show = sp_client.show(self.pycast_show_id, market="TW")
self.updated_at = now

if not self.show.get('episodes') or \
not self.show['episodes'].get('items'):
raise Http404

episodes = []
for episode in self.show['episodes']['items']:
episodes.append({
'id': episode['id'],
'title': episode['name'],
'description': episode['html_description'],
'duration': episode['duration_ms'],
'release_date': episode['release_date'],
})
results = serializers.PyCastAPISerializer(episodes, many=True).data
return Response(results)
4 changes: 4 additions & 0 deletions src/pycontw2016/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,7 @@ def node_bin(name):
# (see the repo at https://github.com/pycontw/pycontw-2021) and this config
# provides the url hosting the frontend.
FRONTEND_HOST = 'https://staging.pycon.tw'

# For calling spotify web API using spotipy
SPOTIPY_CLIENT_ID = env('SPOTIPY_CLIENT_ID', default='dummy')
SPOTIPY_CLIENT_SECRET = env('SPOTIPY_CLIENT_SECRET', default='dummy')