Permalink
Browse files

added video_source, video_sourceid, slides_source, slides_sourceid to…

… video table
  • Loading branch information...
1 parent 5fe8906 commit 317443ee1ac68a07c4bd1634d528cc77e0dcb9a2 @kracekumar kracekumar committed Aug 4, 2012
Showing with 88 additions and 31 deletions.
  1. +30 −8 hgtv/models/video.py
  2. +4 −3 hgtv/templates/video.html
  3. +12 −2 hgtv/templates/videoedit.html
  4. +42 −18 hgtv/views/video.py
View
@@ -1,6 +1,11 @@
# -*- coding: utf-8 -*-
+import socket
@jace

jace Aug 4, 2012

Owner

What is socket being imported for?

+
from sqlalchemy.ext.associationproxy import association_proxy
+import requests
@jace

jace Aug 4, 2012

Owner

This import isn't needed either.

+
+from flask import Markup
from hgtv.models import db, TimestampMixin, BaseIdNameMixin
@@ -38,8 +43,13 @@ class Video(BaseIdNameMixin, db.Model):
slides_url = db.Column(db.Unicode(250), nullable=False, default=u'')
thumbnail_url = db.Column(db.Unicode(250), nullable=True, default=u'')
- video_html = db.Column(db.Unicode(250), nullable=False, default=u'')
- slides_html = db.Column(db.Unicode(250), nullable=False, default=u'')
+ slides_source = db.Column(db.Unicode(80), nullable=False, default=u'')
+ video_source = db.Column(db.Unicode(80), nullable=False, default=u'')
+
+ slides_sourceid = db.Column(db.Unicode(80), nullable=False, default=u'')
+ video_sourceid = db.Column(db.Unicode(80), nullable=False, default=u'')
+
+ slides_html = db.Column(db.Unicode(250), nullable=True, default=u'')
@jace

jace Aug 4, 2012

Owner

You don't need this column anymore.

@kracekumar

kracekumar Aug 4, 2012

Contributor

Issue I am facing slidedeck doesn't have slideid and for every presentation height and width is different.
E.G:
'<iframe style="border:0; padding:0; margin:0; background:transparent;" mozallowfullscreen="true" webkitallowfullscreen="true" frameBorder="0" allowTransparency="true" id="presentation_frame_4fd396d0d3d0d6002202b7c2" src="//speakerdeck.com/embed/4fd396d0d3d0d6002202b7c2" width="710" height="463"></iframe>\n'
<iframe style="border:0; padding:0; margin:0; background:transparent;" mozallowfullscreen="true" webkitallowfullscreen="true" frameBorder="0" allowTransparency="true" id="presentation_frame_501ce43b99a204000202d44e" src="//speakerdeck.com/embed/501ce43b99a204000202d44e" width="710" height="596"></iframe>\n'

@jace

jace Aug 4, 2012

Owner

501ce43b99a204000202d44e appears to be the embed id. Is it in any of the other fields? We do not use their height and width because we have our own stylesheets -- we should not be using their iframe code.

@kracekumar

kracekumar Aug 4, 2012

Contributor
r = requests.get("http://speakerdeck.com/oembed.json?url=%s"%"https://speakerdeck.com/u/kawanet/p/hello-world-engineers-blue-ocean-number-lldecade")


In [14]: 

In [14]: r.json
Out[14]: 
{u'author_name': u'Yusuke Kawasaki',
 u'author_url': u'https://speakerdeck.com/u/kawanet',
 u'height': 596,
 u'html': u'<iframe style="border:0; padding:0; margin:0; background:transparent;" mozallowfullscreen="true" webkitallowfullscreen="true" frameBorder="0" allowTransparency="true" id="presentation_frame_501ce43b99a204000202d44e" src="//speakerdeck.com/embed/501ce43b99a204000202d44e" width="710" height="596"></iframe>\n',
 u'provider_name': u'Speaker Deck',
 u'provider_url': u'https://speakerdeck.com/',
 u'title': u'Hello, world! Engineers Blue Ocean #lldecade',
 u'type': u'rich',
 u'version': 1.0,
 u'width': 710}
channels = association_proxy('_channels', 'channel', creator=lambda x: ChannelVideo(channel=x))
playlists = association_proxy('_playlists', 'playlist', creator=lambda x: PlaylistVideo(playlist=x))
@@ -58,10 +68,22 @@ def permissions(self, user, inherited=None):
return perms
# FIXME: Use proper urlparse library and contsruct the url
- def embed_for(self, action='view'):
- video_html = self.video_html
- if action == 'edit':
- edit_video_html = video_html.replace("autoplay=1", "autoplay=0")
- return edit_video_html
+ def embed_video_for(self, action='view'):
+ if self.video_source == u'youtube':
+ video_html = '<iframe src="http://www.youtube.com/embed/%s?wmode=transparent&autoplay=1" frameborder="0" allowfullscreen></iframe>' % self.video_sourceid
+ if action == 'edit':
+ return Markup(video_html.replace("autoplay=1", "autoplay=0"))
@jace

jace Aug 4, 2012

Owner

WTH? Just generate separate strings for view and edit actions. Don't do a string replace on a string you generated one line above.

+ else:
+ return Markup(video_html)
+ else:
+ pass
@jace

jace Aug 4, 2012

Owner

Remove the else: and make this return u''

+
+ def embed_slides_for(self, action='view'):
+ if self.slides_source in [u'speakerdeck', u'slideshare']:
+ if action == 'view':
+ return Markup(self.slides_html)
+ elif action == 'edit':
+ return Markup(self.slides_html)
else:
- return video_html
+ # FIXME: this is hack, if I return None or False template displays it as such.
@jace

jace Aug 4, 2012

Owner

It's not a hack, you're returning an empty string, which is different from None or False. The FIXME isn't needed.

+ return u''
@@ -10,13 +10,14 @@
<div class="row">
<div class="span6">
<div class="video169">
- {{ video.video_html|safe }}
+ {{ video.embed_video_for('view')|safe }}
@jace

jace Aug 4, 2012

Owner

You don't need |safe for Markup objects. Never use |safe unless you're very, very sure of the data source.

</div>
</div>
<div class="span6">
- {%- if video.slides_html %}
+ {% set slides_html = video.embed_slides_for('view')|safe %}
@jace

jace Aug 4, 2012

Owner

Use with instead of set and close with endwith. Remove the |safe.

+ {%- if slides_html %}
<div class="video169">
- {{ video.slides_html|safe }}
+ {{ slides_html }}
</div>
{%- else -%}
<div class="video169 placeholder">
@@ -58,7 +58,7 @@
</div>
</div>
<div class="video video169">
- {{ video.embed_for('edit')|safe }}
+ {{ video.embed_video_for('edit')|safe }}
</div>
</form>
</div>
@@ -74,7 +74,17 @@
</div>
</div>
<div class="video video169">
- {{ video.slides_html|safe }}
+ {% set slides_html = video.embed_slides_for('edit')|safe %}
+ {{ raise }}
+ {%- if slides_html %}
+ <div class="video169">
+ {{ slides_html }}
+ </div>
+ {%- else -%}
+ <div class="video169 placeholder">
+ No slides available
+ </div>
+ {%- endif %}
</div>
</form>
</div>
View
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from urlparse import urlparse, parse_qs
+import socket
import requests
from flask import render_template, url_for, g, flash, abort, redirect, Markup, request, json, escape
from coaster.views import load_models
@@ -23,17 +24,26 @@ def process_video(video, new=False):
# Check video source and get corresponding data
if parsed.netloc in ['youtube.com', 'www.youtube.com']:
video_id = parse_qs(parsed.query)['v'][0]
- r = requests.get('https://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=json' % video_id)
- data = r.json
- if new:
- video.title = data['entry']['title']['$t']
- video.description = escape(data['entry']['media$group']['media$description']['$t'])
- for item in data['entry']['media$group']['media$thumbnail']:
- if item['yt$name'] == 'mqdefault':
- video.thumbnail_url = item['url'] # .replace('hqdefault', 'mqdefault')
- video.video_html = '<iframe src="http://www.youtube.com/embed/%s?wmode=transparent&autoplay=1" frameborder="0" allowfullscreen></iframe>' % video_id
+ try:
+ r = requests.get('https://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=json' % video_id)
+ if r.json is None:
+ raise RuntimeError("Unable to fectch data")
+ else:
+ if new:
+ video.title = r.json['entry']['title']['$t']
+ video.description = escape(r.json['entry']['media$group']['media$description']['$t'])
+ for item in r.json['entry']['media$group']['media$thumbnail']:
+ if item['yt$name'] == 'mqdefault':
+ video.thumbnail_url = item['url'] # .replace('hqdefault', 'mqdefault')
+ video.video_sourceid = video_id
+ video.video_source = u"youtube"
+ except requests.ConnectionError:
+ raise RuntimeError("Unable to establish connection")
@jace

jace Aug 4, 2012

Owner

RuntimeError indicates something is wrong in the Flask environment. Don't use it here. If you're not sure what error to raise, create a new one called DataProcessingError.

@kracekumar

kracekumar Aug 4, 2012

Contributor
class DataProcessingError(socket.gaierror):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return repr(self.value)


class NetworkError(requests.ConnectionError):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return repr(self.value)

try:
    r = requests.get(url)
except socket.gaierror:
   raise DataProcessingError("unable to process the data")
except requests.ConnectionError:
   raise NetworkError("unable to establish connection")

Is the above Custom Exception fine, do we need traceback ?

@jace

jace Aug 5, 2012

Owner

Heavens, no. You know what, I'll do this. It's faster that way.

+ except socket.gaierror:
+ raise RuntimeError("Unable to resolve the URL")
else:
raise ValueError("Unsupported video site")
+
else:
raise ValueError("Video URL is missing")
@@ -45,19 +55,33 @@ def process_slides(video):
if video.slides_url:
parsed = urlparse(video.slides_url)
if parsed.netloc in ['slideshare.net', 'www.slideshare.net']:
- r = requests.get('http://www.slideshare.net/api/oembed/2?url=%s&format=json' % video.slides_url)
- data = r.json
- slides_id = data['slideshow_id']
- video.slides_html = '<iframe src="http://www.slideshare.net/slideshow/embed_code/%s" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>' % slides_id
+ try:
+ r = requests.get('http://www.slideshare.net/api/oembed/2?url=%s&format=json' % video.slides_url)
+ if r.json:
+ video.slides_source = u'slideshare'
+ video.slides_sourceid = r.json['slideshow_id']
+ video.slides_html = r.json['html']
+ else:
+ raise RuntimeError("Unable to fetch data")
+ except requests.ConnectionError:
+ raise RuntimeError("Unable to establish connection")
+ except socket.gaierror:
+ raise RuntimeError("Unable to resolve the URL")
elif parsed.netloc in ['speakerdeck.com', 'www.speakerdeck.com']:
- r = requests.get('http://speakerdeck.com/oembed.json?url=%s' % video.slides_url)
- data = r.json
- video.slides_html = data['html']
+ try:
+ r = requests.get('http://speakerdeck.com/oembed.json?url=%s' % video.slides_url)
+ video.slides_source = u'speakerdeck'
+ video.slides_html = r.json['html']
+ except requests.ConnectionError:
+ raise RuntimeError("Unable to establish connection")
+ except socket.gaierror:
+ raise RuntimeError("Unable to resolve the URL")
else:
- video.slides_html = '<iframe src="%s" frameborder="0"></iframe>' % video.slides_url
raise ValueError("Unsupported slides site")
else:
- raise ValueError("Slides URL missing")
+ video.slides_url = u''
+ video.slides_source = u''
+ video.slides_html = u''
@app.route('/<channel>/<playlist>/new', methods=['GET', 'POST'])

0 comments on commit 317443e

Please sign in to comment.