# Path: build/lib/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 = urlparse.urlsplit(video.url)
#         query  = urlparse.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/'
#     #http://gdata.youtube.com/feeds/api/videos/Agdvt9M3NJA
#     api_url = 'http://gdata.youtube.com/feeds/api/videos/{}'.format(video.key)
#     video_data = urlopen(api_url).read()
#     xml = xmltramp.parse(video_data)
# 
#     video.title = unicode(xml.title)
#     video.slug = slugify(video.title)
#     video.summary = unicode(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 = xmltramp.parse(video_data)
#     video.title = unicode(xml.video.title)
#     video.slug = slugify(video.title)
#     video.summary = unicode(xml.video.description)
#     video.thumb_url = unicode(xml.video.thumbnail_large)
#     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


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 .helpers import get_youtube_data, get_vimeo_data, get_ustream_data
from .managers import VideoManager, PublishedVideoManager
from tango_shared.models import BaseContentModel




class Video(BaseContentModel):
    video_at_top = models.BooleanField(
        "Put video at top",
        default=False,
        help_text="""
            If checked, the video will appear preceding the body

# Path: build/lib/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 = urlparse.urlsplit(video.url)
#         query  = urlparse.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/'
#     #http://gdata.youtube.com/feeds/api/videos/Agdvt9M3NJA
#     api_url = 'http://gdata.youtube.com/feeds/api/videos/{}'.format(video.key)
#     video_data = urlopen(api_url).read()
#     xml = xmltramp.parse(video_data)
# 
#     video.title = unicode(xml.title)
#     video.slug = slugify(video.title)
#     video.summary = unicode(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 = xmltramp.parse(video_data)
#     video.title = unicode(xml.video.title)
#     video.slug = slugify(video.title)
#     video.summary = unicode(xml.video.description)
#     video.thumb_url = unicode(xml.video.thumbnail_large)
#     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: build/lib/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 .helpers import get_youtube_data, get_vimeo_data, get_ustream_data
from .managers import VideoManager, PublishedVideoManager
from tango_shared.models import BaseContentModel





class Video(BaseContentModel):
    video_at_top = models.BooleanField(
        "Put video at top",
        default=False,
        help_text="""
            If checked, the video will appear preceding the body

            of any related content instead of following.