Fix n-click behaviour on poster. Fix unit tests. Fix handler for non_en lang for bumper. Add more tests. Fix docstrings. Fix pep8. Fix static redirection with bumper. Fix button in IE11. Add video_bumper field in bok_choy. Fix pylink violations. Update docstrings and some clean up. Rename edx_video_id in bumper tests. Fix too long lines in help text. Address ui comments. Fix bumper events. Refactor bumper-transcripts code, fix bugs, address comments. Squashed commits: Fix download transcript button. [74e0c8c] Fix quality [a759f33] Fix error, when sub contains extension. [b30755c] Revert "Add video files to host for transcripts." This reverts commit cf8a96bf84346e17b6ad57ad4cc6a27d7a9118cd. [36f038a] Add video files to host for transcripts. [23f1655] Fix pep8 and pyling issues. [0f1f9d2] Update acceptance test. [765a27d] Wait for ajax in captions. [8ae72a3] Fix logic. [063450f] Fix unit tests. [d1075fc] Fix handlers tests. [25d31ad] Update bumper_utils. [cb5f9df] Remove maxDiff. [8738b1a] Code cleanup. [87dbcb7] Fix issues with transcripts. [ec899de] Fix transcripts in serializers. [444b1fc] Fix transcripts typo. [d524cb5] Fix bumper. [f62cf22] Fix video mongo tests. [8f1b55a] Fix dispatches. [53bc308] Add more fixes. [d5e3723] Fix test_video_handlers and rename the method. [93efc23] Fix mobile tests. [740e2ae] Fix pep8 and pylint. [47cfb66] Address comments, add fixes. [4e499d9] Add fixes. [8353553] Add improvements. Updated dispatch values) . Use ddt in bumper handler tests. Move common metadata to single place. Fix style. Update docstring. Fix poster button. Improve bumper events. Fix test after rebase. Address comments. Download transcript: use def video lang, not bump. Renamed date_last_view_bumper to bumper_last_view_date. Rename do_not_show_again_bumper to bumper_... Address comments. Fix tests for download for en lang. Fix bumper logic. Update strings. Update resizer. Remove resizer. Fix unit tests. Add tests. Fix bumper events. Clean up tests. Fix pylint violations. Fix pep8 and pylint violations. Update docs and method names. Update events. Make /static/ prefix a must. Fix wrong code.
116 lines
3.4 KiB
Python
116 lines
3.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
Module contains utils specific for video_module but not for transcripts.
|
|
"""
|
|
import json
|
|
from collections import OrderedDict
|
|
import logging
|
|
import urllib
|
|
import requests
|
|
from urllib import urlencode
|
|
from urlparse import parse_qs, urlsplit, urlunsplit
|
|
|
|
from django.conf import settings
|
|
|
|
from requests.exceptions import RequestException
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
def create_youtube_string(module):
|
|
"""
|
|
Create a string of Youtube IDs from `module`'s metadata
|
|
attributes. Only writes a speed if an ID is present in the
|
|
module. Necessary for backwards compatibility with XML-based
|
|
courses.
|
|
"""
|
|
youtube_ids = [
|
|
module.youtube_id_0_75,
|
|
module.youtube_id_1_0,
|
|
module.youtube_id_1_25,
|
|
module.youtube_id_1_5
|
|
]
|
|
youtube_speeds = ['0.75', '1.00', '1.25', '1.50']
|
|
return ','.join([
|
|
':'.join(pair)
|
|
for pair
|
|
in zip(youtube_speeds, youtube_ids)
|
|
if pair[1]
|
|
])
|
|
|
|
|
|
# def get_video_from_cdn(cdn_base_url, original_video_url, cdn_branding_logo_url):
|
|
# Not sure if this third variable is necessary...
|
|
def get_video_from_cdn(cdn_base_url, original_video_url):
|
|
"""
|
|
Get video URL from CDN.
|
|
|
|
`original_video_url` is the existing video url.
|
|
Currently `cdn_base_url` equals 'http://api.xuetangx.com/edx/video?s3_url='
|
|
Example of CDN outcome:
|
|
{
|
|
"sources":
|
|
[
|
|
"http://cm12.c110.play.bokecc.com/flvs/ca/QxcVl/u39EQbA0Ra-20.mp4",
|
|
"http://bm1.42.play.bokecc.com/flvs/ca/QxcVl/u39EQbA0Ra-20.mp4"
|
|
],
|
|
"s3_url": "http://s3.amazonaws.com/BESTech/CS169/download/CS169_v13_w5l2s3.mp4",
|
|
}
|
|
where `s3_url` is requested original video url and `sources` is the list of
|
|
alternative links.
|
|
"""
|
|
|
|
if not cdn_base_url:
|
|
return None
|
|
|
|
request_url = cdn_base_url + urllib.quote(original_video_url)
|
|
|
|
try:
|
|
cdn_response = requests.get(request_url, timeout=0.5)
|
|
except RequestException as err:
|
|
log.info("Request timed out to CDN server: %s", request_url, exc_info=True)
|
|
return None
|
|
|
|
if cdn_response.status_code == 200:
|
|
cdn_content = json.loads(cdn_response.content)
|
|
return cdn_content['sources'][0]
|
|
else:
|
|
return None
|
|
|
|
|
|
def get_poster(video):
|
|
"""
|
|
Generate poster metadata.
|
|
|
|
youtube_streams is string that contains '1.00:youtube_id'
|
|
|
|
Poster metadata is dict of youtube url for image thumbnail and edx logo
|
|
"""
|
|
if not video.bumper.get("enabled"):
|
|
return
|
|
|
|
poster = OrderedDict({"url": "", "type": ""})
|
|
|
|
if video.youtube_streams:
|
|
youtube_id = video.youtube_streams.split('1.00:')[1].split(',')[0]
|
|
poster["url"] = settings.YOUTUBE['IMAGE_API'].format(youtube_id=youtube_id)
|
|
poster["type"] = "youtube"
|
|
else:
|
|
poster["url"] = "https://www.edx.org/sites/default/files/theme/edx-logo-header.png"
|
|
poster["type"] = "html5"
|
|
|
|
return poster
|
|
|
|
|
|
def set_query_parameter(url, param_name, param_value):
|
|
"""
|
|
Given a URL, set or replace a query parameter and return the
|
|
modified URL.
|
|
"""
|
|
scheme, netloc, path, query_string, fragment = urlsplit(url)
|
|
query_params = parse_qs(query_string)
|
|
query_params[param_name] = [param_value]
|
|
new_query_string = urlencode(query_params, doseq=True)
|
|
|
|
return urlunsplit((scheme, netloc, path, new_query_string, fragment))
|