From 4848fbb6856ee7f8a892d546712e98c11ee8bda5 Mon Sep 17 00:00:00 2001 From: Oscar Cortez Date: Mon, 3 Aug 2015 16:16:59 -0600 Subject: [PATCH 1/3] KTA-1 Fix Embedly error, Vimeo error, SimpleJson erro --- .gitignore | 1 + README.md | 13 ++++++----- magicembed/providers.py | 26 ++++++++++++++++------ magicembed/templatetags/magicembed_tags.py | 4 +++- setup.py | 2 +- 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 87d36da..72fddea 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.swp dist/ magicembed.egg-info/ +build diff --git a/README.md b/README.md index 061a14e..16e22db 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Django Magic Embed What is it? ------------ -Magic Embed is an easy and simple Django template tag and tool to embed +Magic Embed is an easy and simple Django template tag and tool to embed video and get thumbnails from video providers. Demo @@ -28,17 +28,17 @@ You can download it from PyPI here How to install it? ------------------- -If you have a requeriments list add this to your requeriments +If you have a requeriments list add this to your requeriments 1. magicembed==(version) 2. pip install -r requirements.txt -Or if you use setup.py +3. add magicembed to **INSTALLED_APPS** -1. add magicembed to **INSTALLED_APPS** +Or if you use setup.py -2. run python setup.py install +1. run python setup.py install How to use --------------- @@ -47,7 +47,7 @@ First add this in the template to load the template tags {% load magicembed_tags %} -Now if you need to embed a video, add this template tag to video url +Now if you need to embed a video, add this template tag to video url field {{ video|magicembed:"width x height" }} @@ -68,3 +68,4 @@ How to contrib Licence -------------- Licensed under [MIT](http://opensource.org/licenses/mit-license.php) + diff --git a/magicembed/providers.py b/magicembed/providers.py index c5deff3..ecc2444 100644 --- a/magicembed/providers.py +++ b/magicembed/providers.py @@ -1,7 +1,11 @@ -from urlparse import parse_qs +# -*- coding: utf-8 -*- +import json import re import urllib -from django.utils import simplejson + +from urlparse import parse_qs + +from django.conf import settings class Provider(object): @@ -37,8 +41,8 @@ class Vimeo(Provider): def __init__(self, url, size=(640, 480)): super(Vimeo, self).__init__(url, size) - pattern = re.compile('http://(?:www\.)?vimeo\.com/([0-9]{1,12})') - self.video_id = pattern.match(url).groups()[0] + pattern = re.compile('(http|https)://(?:www\.)?vimeo\.com/([0-9]{1,12})') + self.video_id = pattern.match(url).groups()[1] self.api_url = 'http://vimeo.com/api/v2/video/%s.json' % self.video_id def render_video(self): @@ -47,14 +51,22 @@ def render_video(self): self.size[1], self.video_id) def render_thumbnail(self, link_to="#"): - api_response = simplejson.loads(urllib.urlopen(self.api_url).read()) + api_response = json.loads(urllib.urlopen(self.api_url).read()) return api_response[0]['thumbnail_medium'] class Embedly(Provider): def __init__(self, url, size=(640, 480)): super(Embedly, self).__init__(url, size) - self.api_url = 'http://api.embed.ly/1/oembed?url=%s&maxwidth=%s&format=json' % (url, size[0]) + key = getattr(settings, "EMBEDLY_KEY", None) + if key != None: + try: + self.api_url = 'http://api.embed.ly/1/oembed?key=%s&url=%s&maxwidth=%s&format=json' % (key, url, size[0]) + except IOError: + raise IOError("Please set the Embedly api key correctly") + else: + raise ValueError("If you want to use this please set the Embedly api key") + def render_video(self): return self._call_api()['html'] @@ -63,7 +75,7 @@ def render_thumbnail(self): return self._call_api()['thumbnail_url'] def _call_api(self): - data = simplejson.loads(urllib.urlopen(self.api_url).read()) + data = json.loads(urllib.urlopen(self.api_url).read()) return data def get_provider(url, size=None): diff --git a/magicembed/templatetags/magicembed_tags.py b/magicembed/templatetags/magicembed_tags.py index dd2fb7f..4beb850 100644 --- a/magicembed/templatetags/magicembed_tags.py +++ b/magicembed/templatetags/magicembed_tags.py @@ -1,7 +1,9 @@ +# -*- coding: utf-8 -*- from django import template -from magicembed.providers import get_provider from django.utils.safestring import mark_safe +from magicembed.providers import get_provider + register = template.Library() @register.filter(is_safe=True) diff --git a/setup.py b/setup.py index 5815afd..961ed3f 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ setup( name = "magicembed", - version = "0.3", + version = "1.0.0", url = "http://github.com/kronoscode/django-magicembed", license = 'MIT', description = 'Django template filter utils to render videos an thumbnails.', From 85f2bb64afb742240062275e330f02b444b7337e Mon Sep 17 00:00:00 2001 From: Oscar Cortez Date: Mon, 3 Aug 2015 16:37:01 -0600 Subject: [PATCH 2/3] KTA-1 remove unused try, add pypi badge --- README.md | 2 ++ magicembed/providers.py | 10 +++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 16e22db..1a6b39c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +[![PyPI version](https://badge.fury.io/py/magicembed.svg)](http://badge.fury.io/py/magicembed) + Django Magic Embed ================== diff --git a/magicembed/providers.py b/magicembed/providers.py index ecc2444..efe992f 100644 --- a/magicembed/providers.py +++ b/magicembed/providers.py @@ -60,10 +60,7 @@ def __init__(self, url, size=(640, 480)): super(Embedly, self).__init__(url, size) key = getattr(settings, "EMBEDLY_KEY", None) if key != None: - try: - self.api_url = 'http://api.embed.ly/1/oembed?key=%s&url=%s&maxwidth=%s&format=json' % (key, url, size[0]) - except IOError: - raise IOError("Please set the Embedly api key correctly") + self.api_url = 'http://api.embed.ly/1/oembed?key=%s&url=%s&maxwidth=%s&format=json' % (key, url, size[0]) else: raise ValueError("If you want to use this please set the Embedly api key") @@ -75,7 +72,10 @@ def render_thumbnail(self): return self._call_api()['thumbnail_url'] def _call_api(self): - data = json.loads(urllib.urlopen(self.api_url).read()) + try: + data = json.loads(urllib.urlopen(self.api_url).read()) + except IOError: + raise IOError("Please set the Embedly api key correctly") return data def get_provider(url, size=None): From 9764efb6e57d8f02655f167bf25b56360ee24c41 Mon Sep 17 00:00:00 2001 From: Oscar Cortez Date: Mon, 3 Aug 2015 16:51:50 -0600 Subject: [PATCH 3/3] KTA-1 Add docs about Embedly API key --- README.md | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1a6b39c..a73053e 100644 --- a/README.md +++ b/README.md @@ -24,8 +24,17 @@ Screenshots Downloading --------------- -You can download it from PyPI here - [PyPI-Magic Embed](https://pypi.python.org/pypi/magicembed/0.2) +You can download it from [PyPI](https://pypi.python.org/pypi/magicembed/) + +Embedly API key +------------------ + +If you want to use [Embedly](http://embed.ly/) please create a new +account and [generate the key](https://app.embed.ly/signup) + +When you have the API key, add this in your settings.py: + + EMBEDLY_KEY='YourAwesomeAPIKey' How to install it? -------------------