diff options
| author | Dharmraj Jhala <dja@odoo.com> | 2019-04-11 12:11:48 +0000 |
|---|---|---|
| committer | Dharmraj Jhala <dja@odoo.com> | 2019-04-16 10:34:49 +0000 |
| commit | 33e17775e159376da13cb89220a18c7c2545ab9d (patch) | |
| tree | 16191dda067d5a4aef7d3730ad632a6648762b32 /addons/website/tools.py | |
| parent | 378d7478b65df0fe5cc309fe5de4364960d61d43 (diff) | |
[ADD] website: added tools file for utility methods
This commit adds a new file 'tools.py' that can contain
utility methods which can be used at more than one places.
For now it contains a method 'get_video_embed_url', that
will accept a URL for the video and return an iframe snippet
that can be embedded if the provided URL is valid (returns
False otherwise).
task-1945483
Diffstat (limited to 'addons/website/tools.py')
| -rw-r--r-- | addons/website/tools.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/addons/website/tools.py b/addons/website/tools.py new file mode 100644 index 00000000000..48ee9aec53f --- /dev/null +++ b/addons/website/tools.py @@ -0,0 +1,51 @@ +# -*- encoding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +import re + + +def get_video_embed_url(video_url): + ''' Computes the valid iframe from given URL that can be embedded + (or False in case of invalid URL). + ''' + + if not video_url: + return False + + # To detect if we have a valid URL or not + validURLRegex = r'^(http:\/\/|https:\/\/|\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$' + + # Regex for few of the widely used video hosting services + ytRegex = r'^(?:(?:https?:)?\/\/)?(?:www\.)?(?:youtu\.be\/|youtube(-nocookie)?\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((?:\w|-){11})(?:\S+)?$' + vimeoRegex = r'\/\/(player.)?vimeo.com\/([a-z]*\/)*([0-9]{6,11})[?]?.*' + dmRegex = r'.+dailymotion.com\/(video|hub|embed)\/([^_]+)[^#]*(#video=([^_&]+))?' + igRegex = r'(.*)instagram.com\/p\/(.[a-zA-Z0-9]*)' + ykuRegex = r'(.*).youku\.com\/(v_show\/id_|embed\/)(.+)' + + if not re.search(validURLRegex, video_url): + return False + else: + embedUrl = False + ytMatch = re.search(ytRegex, video_url) + vimeoMatch = re.search(vimeoRegex, video_url) + dmMatch = re.search(dmRegex, video_url) + igMatch = re.search(igRegex, video_url) + ykuMatch = re.search(ykuRegex, video_url) + + if ytMatch and len(ytMatch.groups()[1]) == 11: + embedUrl = '//www.youtube%s.com/embed/%s?rel=0' % (ytMatch.groups()[0] or '', ytMatch.groups()[1]) + elif vimeoMatch: + embedUrl = '//player.vimeo.com/video/%s' % (vimeoMatch.groups()[2]) + elif dmMatch: + embedUrl = '//www.dailymotion.com/embed/video/%s' % (dmMatch.groups()[1]) + elif igMatch: + embedUrl = '//www.instagram.com/p/%s/embed/' % (igMatch.groups()[1]) + elif ykuMatch: + ykuLink = ykuMatch.groups()[2] + if '.html?' in ykuLink: + ykuLink = ykuLink.split('.html?')[0] + embedUrl = '//player.youku.com/embed/%s' % (ykuLink) + else: + # We directly use the provided URL as it is + embedUrl = video_url + return '<iframe class="embed-responsive-item" src="%s" allowFullScreen="true" frameborder="0"/>' % (embedUrl) |
