diff --git a/hgtv/views/video.py b/hgtv/views/video.py index ab5d5c64..28850cfe 100644 --- a/hgtv/views/video.py +++ b/hgtv/views/video.py @@ -5,11 +5,10 @@ from urlparse import urlparse, parse_qs from socket import gaierror import requests -import urllib2 import bleach from werkzeug import secure_filename -from flask import abort, redirect, request, jsonify, g, json, url_for, make_response +from flask import abort, redirect, request, jsonify, json, url_for from coaster.auth import current_auth from coaster.views import load_models, render_with from coaster.gfm import markdown @@ -155,9 +154,8 @@ def process_slides(video): raise DataProcessingError("Unable to resolve the URL") elif parsed.netloc in ['speakerdeck.com', 'www.speakerdeck.com']: try: - r = urllib2.urlopen('https://speakerdeck.com/oembed.json?url=%s' % video.slides_url) - jsondata = json.loads(r.read()) - r.close() + r = requests.get('https://speakerdeck.com/oembed.json?url=%s' % video.slides_url) + jsondata = r.json() if jsondata: video.slides_source = u'speakerdeck' pattern = u'\Wsrc="//speakerdeck.com/player/([^\s^"]+)' # pattern to extract slideid from speakerdeck diff --git a/tests/fixtures.py b/tests/fixtures.py index a6a41f5b..11691817 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -29,9 +29,17 @@ def setUp(self): db.session.add(playlist1) video1 = Video(playlist=playlist1, name=u"test-video-1", title=u"Test Video 1", - video_url=u"https://www.youtube.com/watch?v=testvideo1") + video_url=u"https://www.youtube.com/watch?v=testvideo1", + slides_url=u"https://speakerdeck.com/jackerhack/understanding-open-source") playlist1.videos.append(video1) + + video2 = Video(playlist=playlist1, name=u"test-video-2", title=u"Test Video 2", + video_url=u"https://www.youtube.com/watch?v=testvideo2", + slides_url=u"https://www.slideshare.net/kwhinnery/write-better-javascript") + playlist1.videos.append(video2) + db.session.add(video1) + db.session.add(video2) # Commit objects db.session.commit() diff --git a/tests/unit/test_video.py b/tests/unit/test_video.py new file mode 100644 index 00000000..701ddd5e --- /dev/null +++ b/tests/unit/test_video.py @@ -0,0 +1,25 @@ +from hgtv.models import Video +from hgtv.views.video import process_slides +from ..fixtures import TestCaseBase + + +class VideoUnitTest(TestCaseBase): + def test_new_video(self): + test_video1 = Video.query.filter_by(name=u"test-video-1").first() + self.assertEqual(test_video1.title, u"Test Video 1") + + def test_slides_process_speakerdeck(self): + test_video1 = Video.query.filter_by(name=u"test-video-1").first() + self.assertEqual(test_video1.slides_source, u'') + self.assertEqual(test_video1.slides_sourceid, u'') + process_slides(test_video1) + self.assertEqual(test_video1.slides_source, u'speakerdeck') + self.assertNotEqual(test_video1.slides_sourceid, u'') + + def test_slides_process_slideshare(self): + test_video2 = Video.query.filter_by(name=u"test-video-2").first() + self.assertEqual(test_video2.slides_source, u'') + self.assertEqual(test_video2.slides_sourceid, u'') + process_slides(test_video2) + self.assertEqual(test_video2.slides_source, u'slideshare') + self.assertNotEqual(test_video2.slides_sourceid, u'')