Skip to content
This repository has been archived by the owner on Apr 29, 2022. It is now read-only.

WIP Add video URL to talk. Add script to fetch video urls from youtube. #1176

Open
wants to merge 1 commit into
base: dev/ep2020
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 68 additions & 0 deletions conference/management/commands/get_video_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import os

from django.core.management.base import BaseCommand, CommandError

import googleapiclient.discovery
import googleapiclient.errors


class Command(BaseCommand):
"""
Get details of the videos corresponding to the provided playlist.
"""
def add_arguments(self, parser):
parser.add_argument('--playlist-id', type=str, required=True)

def handle(self, *args, **options):
# Setup the API client
playlist_id = options['playlist_id']
api_service_name = "youtube"
api_version = "v3"
api_key = os.getenv("GOOGLE_API_KEY")

youtube_client = googleapiclient.discovery.build(
serviceName=api_service_name,
version=api_version,
developerKey=api_key,
)

# Fetch the video data from the API
playlist_items = []
items_per_page = 50 # max allowed by the api
default_request_args = dict(
part="snippet,contentDetails",
maxResults=items_per_page,
playlistId=playlist_id,
)

request = youtube_client.playlistItems().list(**default_request_args)
response = request.execute()

page_count = 1
total_results = response["pageInfo"]["totalResults"]
playlist_items += response["items"]

while page_count * items_per_page < total_results:
next_page_token = response["nextPageToken"]

request = youtube_client.playlistItems().list(**default_request_args, pageToken=next_page_token)
response = request.execute()

page_count += 1
playlist_items += response["items"]

# Process the results
processed_items = []
for item in playlist_items:
video_id = item['contentDetails']['videoId']
youtube_title = item['snippet']['title']
title_separator = ' - '
title_separator_ix = youtube_title.find(title_separator)

processed_items.append({
'speaker': youtube_title[:title_separator_ix],
'title': youtube_title[title_separator_ix + len(title_separator):],
'url': f'https://www.youtube.com/watch?v={video_id}',
})

print(processed_items)
1 change: 1 addition & 0 deletions conference/talks.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def dump_relevant_talk_information_to_dict(talk: Talk):
"schedule_url": talk.get_schedule_url(),
"slides_file_url": talk.slides,
"slides_remote_url": talk.slides_url,
"video_url": talk.video_url,
}

for speaker in talk.get_all_speakers():
Expand Down
3 changes: 3 additions & 0 deletions templates/ep19/bs/talks/talk.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ <h5>{% for speaker in talk_as_dict.speakers %}
{% if talk_as_dict.slides_remote_url %}
<a class="btn btn-primary" href="{{ talk_as_dict.slides_remote_url }}">View Slides</a>
{% endif %}
{% if talk_as_dict.video_url %}
<a class="btn btn-primary" href="{{ talk_as_dict.video_url }}">View Recording</a>
{% endif %}
<p>{{ talk_as_dict.abstract|urlize|linebreaks }}</p>
<p>
<code>Type: {{ talk.get_type_display }}; Python level: {{ talk.get_level_display }}; Domain level: {{ talk.get_domain_level_display }}</code>
Expand Down
23 changes: 23 additions & 0 deletions tests/test_talks.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,26 @@ def test_view_slides_remote_url_on_talk_detail_page(client):
response = client.get(url)

assert 'view slides' in response.content.decode().lower()


def test_video_url_on_talk_detail_page(client):
"""
The view recording button only appears if the video url has been updated.
"""
setup_conference_with_typical_fares()
talk = TalkFactory(status=TALK_STATUS.accepted)
url = talk.get_absolute_url()

# Slides URL does not appear when the slides haven't been uploaded
response = client.get(url)

assert not talk.slides
assert 'view recording' not in response.content.decode().lower()

# Slides URL does appear when the slides have been uploaded
talk.video_url = 'https://ep2019.europython.eu/video'
talk.save()

response = client.get(url)

assert 'view recording' in response.content.decode().lower()