# Path: video/helpers.py
# def get_youtube_data(video):
#     """
#     Helper to extract video and thumbnail from youtube
#     """
#     video.source = 'youtube'
#     if 'youtube.com/watch' in video.url:
#         parsed = urlsplit(video.url)
#         query  = parse_qs(parsed.query)
#         try:
#             video.key  = query.get('v')[0]
#         except IndexError:
#             video.key = None
#     else:
#         video.key = video.url.rsplit('/', 1)[1]
#     video.embed_src = 'http://www.youtube.com/embed/'
# 
#     # api docs
#     #          http://gdata.youtube.com/feeds/api/videos/hNRHHRjep3E
#     # https://www.googleapis.com/youtube/v3/videos?id=hNRHHRjep3E&part=snippet,contentDetails,statistics
#     api_url = 'http://gdata.youtube.com/feeds/api/videos/{}'.format(video.key)
#     video_data = urlopen(api_url).read()
#     xml = untangle.parse(video_data)
# 
#     video.title = xml.title
#     video.slug = slugify(video.title)
#     video.summary = xml.content
#     #video.thumb_url = xml[xml_media.group][xml_media.thumbnail:][1]('url')
#     return video
# 
# def get_vimeo_data(video):
#     """
#     Helper to extract video and thumbnail from vimeo.
#     """
#     #http://vimeo.com/67325705 -- Tumbleweed Tango
#     video.source = 'vimeo'
#     video.key = video.url.rsplit('/', 1)[1]
#     video.embed_src = 'http://player.vimeo.com/video/'
# 
#     api_url = 'http://vimeo.com/api/v2/video/{}.xml'.format(video.key)
#     #video_data = urlopen(api_url).read()
#     xml = untangle.parse(api_url)
#     xmlvideo = xml.videos.video
#     video.title = xmlvideo.title.cdata
#     video.slug = slugify(video.title)
#     video.summary = xmlvideo.description.cdata
#     video.thumb_url = xmlvideo.thumbnail_large.cdata
#     return video
# 
# def get_ustream_data(video):
#     """
#     Helper to extract video and thumbnail from ustream.
#     """
# 
#     video.source = 'ustream'
#     video.key = video.url.rsplit('/', 1)[1]
#     video.embed_src = 'http://www.ustream.tv/embed/'
#     if 'recorded' in video.url:
#         video.embed_src += '/recorded/'
#     video.title = "Ustream video"
#     video.slug = 'ustream-{}'.format(video.key)
#     return video
# 
# Path: video/managers.py
# class VideoManager(models.Manager):
#     """
#     If RESTRICT_CONTENT_TO_SITE is True in settings,
#     will limit video to current site.
# 
#     Usage is simply video.objects.all()
#     """
#     def get_queryset(self):
#         videos = super(VideoManager, self).get_queryset()
#         if RESTRICT_CONTENT_TO_SITE:
#             videos.filter(sites__id__exact=settings.SITE_ID)
#         return videos
# 
# class PublishedVideoManager(VideoManager):
#     """
#     Extends VideoManager to only return videos
#     - That are published
#     - and have a created date greater than or equal to now.
# 
#     Usage is gallery.published.all()
#     """
#     def get_queryset(self):
#         videos = super(PublishedVideoManager, self).get_queryset()
#         videos.filter(published=True, created__lte=now)
#         return videos


from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
from django.db import models
from django.template.defaultfilters import slugify
from django.urls import reverse
from .helpers import get_youtube_data, get_vimeo_data, get_ustream_data
from .managers import VideoManager, PublishedVideoManager
from tango_shared.models import BaseContentModel
            Acceptable sources are Youtube, Vimeo and Ustream.
            Please give the link URL, not the embed code.
            """
    )
    hide_info = models.BooleanField(
        "Hide title and description",
        default=False,
        help_text="""
            If checked, the video title and description will not be shown.
            Left unchecked, the video will attempt to display the title and description
            from the video source.
            """
    )

    content_type = models.ForeignKey(
        ContentType,
        on_delete=models.CASCADE,
        blank=True,
        null=True
    )
    object_id = models.PositiveIntegerField(blank=True, null=True)
    content_object = GenericForeignKey()

    key = models.CharField(max_length=20, blank=True, editable=False)
    source = models.CharField(max_length=20, blank=True, editable=False)
    embed_src = models.CharField(max_length=200, blank=True, editable=False)
    thumb_url = models.CharField(max_length=200, blank=True, editable=False)

    # Managers
    objects = VideoManager()

# Path: video/helpers.py
# def get_youtube_data(video):
#     """
#     Helper to extract video and thumbnail from youtube
#     """
#     video.source = 'youtube'
#     if 'youtube.com/watch' in video.url:
#         parsed = urlsplit(video.url)
#         query  = parse_qs(parsed.query)
#         try:
#             video.key  = query.get('v')[0]
#         except IndexError:
#             video.key = None
#     else:
#         video.key = video.url.rsplit('/', 1)[1]
#     video.embed_src = 'http://www.youtube.com/embed/'
# 
#     # api docs
#     #          http://gdata.youtube.com/feeds/api/videos/hNRHHRjep3E
#     # https://www.googleapis.com/youtube/v3/videos?id=hNRHHRjep3E&part=snippet,contentDetails,statistics
#     api_url = 'http://gdata.youtube.com/feeds/api/videos/{}'.format(video.key)
#     video_data = urlopen(api_url).read()
#     xml = untangle.parse(video_data)
# 
#     video.title = xml.title
#     video.slug = slugify(video.title)
#     video.summary = xml.content
#     #video.thumb_url = xml[xml_media.group][xml_media.thumbnail:][1]('url')
#     return video
# 
# def get_vimeo_data(video):
#     """
#     Helper to extract video and thumbnail from vimeo.
#     """
#     #http://vimeo.com/67325705 -- Tumbleweed Tango
#     video.source = 'vimeo'
#     video.key = video.url.rsplit('/', 1)[1]
#     video.embed_src = 'http://player.vimeo.com/video/'
# 
#     api_url = 'http://vimeo.com/api/v2/video/{}.xml'.format(video.key)
#     #video_data = urlopen(api_url).read()
#     xml = untangle.parse(api_url)
#     xmlvideo = xml.videos.video
#     video.title = xmlvideo.title.cdata
#     video.slug = slugify(video.title)
#     video.summary = xmlvideo.description.cdata
#     video.thumb_url = xmlvideo.thumbnail_large.cdata
#     return video
# 
# def get_ustream_data(video):
#     """
#     Helper to extract video and thumbnail from ustream.
#     """
# 
#     video.source = 'ustream'
#     video.key = video.url.rsplit('/', 1)[1]
#     video.embed_src = 'http://www.ustream.tv/embed/'
#     if 'recorded' in video.url:
#         video.embed_src += '/recorded/'
#     video.title = "Ustream video"
#     video.slug = 'ustream-{}'.format(video.key)
#     return video
# 
# Path: video/managers.py
# class VideoManager(models.Manager):
#     """
#     If RESTRICT_CONTENT_TO_SITE is True in settings,
#     will limit video to current site.
# 
#     Usage is simply video.objects.all()
#     """
#     def get_queryset(self):
#         videos = super(VideoManager, self).get_queryset()
#         if RESTRICT_CONTENT_TO_SITE:
#             videos.filter(sites__id__exact=settings.SITE_ID)
#         return videos
# 
# class PublishedVideoManager(VideoManager):
#     """
#     Extends VideoManager to only return videos
#     - That are published
#     - and have a created date greater than or equal to now.
# 
#     Usage is gallery.published.all()
#     """
#     def get_queryset(self):
#         videos = super(PublishedVideoManager, self).get_queryset()
#         videos.filter(published=True, created__lte=now)
#         return videos


# Path: video/models.py
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
from django.db import models
from django.template.defaultfilters import slugify
from django.urls import reverse
from .helpers import get_youtube_data, get_vimeo_data, get_ustream_data
from .managers import VideoManager, PublishedVideoManager
from tango_shared.models import BaseContentModel

            Acceptable sources are Youtube, Vimeo and Ustream.
            Please give the link URL, not the embed code.
            """
    )
    hide_info = models.BooleanField(
        "Hide title and description",
        default=False,
        help_text="""
            If checked, the video title and description will not be shown.
            Left unchecked, the video will attempt to display the title and description
            from the video source.
            """
    )

    content_type = models.ForeignKey(
        ContentType,
        on_delete=models.CASCADE,
        blank=True,
        null=True
    )
    object_id = models.PositiveIntegerField(blank=True, null=True)
    content_object = GenericForeignKey()

    key = models.CharField(max_length=20, blank=True, editable=False)
    source = models.CharField(max_length=20, blank=True, editable=False)
    embed_src = models.CharField(max_length=200, blank=True, editable=False)
    thumb_url = models.CharField(max_length=200, blank=True, editable=False)

    # Managers
    objects = VideoManager()

    pub_objects = PublishedVideoManager()