Skip to content

Commit 7c661a9

Browse files
committed
Merge pull request #4 from kronoscode/KTA-1
KTA-1 Fix Embedly error, Vimeo error, SimpleJson error
2 parents 302f2f5 + 9764efb commit 7c661a9

5 files changed

Lines changed: 44 additions & 17 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
*.swp
33
dist/
44
magicembed.egg-info/
5+
build

README.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
[![PyPI version](https://badge.fury.io/py/magicembed.svg)](http://badge.fury.io/py/magicembed)
2+
13
Django Magic Embed
24
==================
35

46
What is it?
57
------------
68

7-
Magic Embed is an easy and simple Django template tag and tool to embed
9+
Magic Embed is an easy and simple Django template tag and tool to embed
810
video and get thumbnails from video providers.
911

1012
Demo
@@ -22,23 +24,32 @@ Screenshots
2224
Downloading
2325
---------------
2426

25-
You can download it from PyPI here
26-
[PyPI-Magic Embed](https://pypi.python.org/pypi/magicembed/0.2)
27+
You can download it from [PyPI](https://pypi.python.org/pypi/magicembed/)
28+
29+
Embedly API key
30+
------------------
31+
32+
If you want to use [Embedly](http://embed.ly/) please create a new
33+
account and [generate the key](https://app.embed.ly/signup)
34+
35+
When you have the API key, add this in your settings.py:
36+
37+
EMBEDLY_KEY='YourAwesomeAPIKey'
2738

2839
How to install it?
2940
-------------------
3041

31-
If you have a requeriments list add this to your requeriments
42+
If you have a requeriments list add this to your requeriments
3243

3344
1. <code>magicembed==(version)</code>
3445

3546
2. <code>pip install -r requirements.txt</code>
3647

37-
Or if you use setup.py
48+
3. <code>add magicembed to **INSTALLED_APPS**</code>
3849

39-
1. add magicembed to **INSTALLED_APPS**
50+
Or if you use setup.py
4051

41-
2. run <code>python setup.py install</code>
52+
1. run <code>python setup.py install</code>
4253

4354
How to use
4455
---------------
@@ -47,7 +58,7 @@ First add this in the template to load the template tags
4758

4859
<code>{% load magicembed_tags %}</code>
4960

50-
Now if you need to embed a video, add this template tag to video url
61+
Now if you need to embed a video, add this template tag to video url
5162
field
5263

5364
<code>{{ video|magicembed:"width x height" }}</code>
@@ -68,3 +79,4 @@ How to contrib
6879
Licence
6980
--------------
7081
Licensed under [MIT](http://opensource.org/licenses/mit-license.php)
82+

magicembed/providers.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
from urlparse import parse_qs
1+
# -*- coding: utf-8 -*-
2+
import json
23
import re
34
import urllib
4-
from django.utils import simplejson
5+
6+
from urlparse import parse_qs
7+
8+
from django.conf import settings
59

610
class Provider(object):
711

@@ -37,8 +41,8 @@ class Vimeo(Provider):
3741

3842
def __init__(self, url, size=(640, 480)):
3943
super(Vimeo, self).__init__(url, size)
40-
pattern = re.compile('http://(?:www\.)?vimeo\.com/([0-9]{1,12})')
41-
self.video_id = pattern.match(url).groups()[0]
44+
pattern = re.compile('(http|https)://(?:www\.)?vimeo\.com/([0-9]{1,12})')
45+
self.video_id = pattern.match(url).groups()[1]
4246
self.api_url = 'http://vimeo.com/api/v2/video/%s.json' % self.video_id
4347

4448
def render_video(self):
@@ -47,14 +51,19 @@ def render_video(self):
4751
self.size[1], self.video_id)
4852

4953
def render_thumbnail(self, link_to="#"):
50-
api_response = simplejson.loads(urllib.urlopen(self.api_url).read())
54+
api_response = json.loads(urllib.urlopen(self.api_url).read())
5155
return api_response[0]['thumbnail_medium']
5256

5357
class Embedly(Provider):
5458

5559
def __init__(self, url, size=(640, 480)):
5660
super(Embedly, self).__init__(url, size)
57-
self.api_url = 'http://api.embed.ly/1/oembed?url=%s&maxwidth=%s&format=json' % (url, size[0])
61+
key = getattr(settings, "EMBEDLY_KEY", None)
62+
if key != None:
63+
self.api_url = 'http://api.embed.ly/1/oembed?key=%s&url=%s&maxwidth=%s&format=json' % (key, url, size[0])
64+
else:
65+
raise ValueError("If you want to use this please set the Embedly api key")
66+
5867

5968
def render_video(self):
6069
return self._call_api()['html']
@@ -63,7 +72,10 @@ def render_thumbnail(self):
6372
return self._call_api()['thumbnail_url']
6473

6574
def _call_api(self):
66-
data = simplejson.loads(urllib.urlopen(self.api_url).read())
75+
try:
76+
data = json.loads(urllib.urlopen(self.api_url).read())
77+
except IOError:
78+
raise IOError("Please set the Embedly api key correctly")
6779
return data
6880

6981
def get_provider(url, size=None):

magicembed/templatetags/magicembed_tags.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
# -*- coding: utf-8 -*-
12
from django import template
2-
from magicembed.providers import get_provider
33
from django.utils.safestring import mark_safe
44

5+
from magicembed.providers import get_provider
6+
57
register = template.Library()
68

79
@register.filter(is_safe=True)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
setup(
1111
name = "magicembed",
12-
version = "0.3",
12+
version = "1.0.0",
1313
url = "http://github.com/kronoscode/django-magicembed",
1414
license = 'MIT',
1515
description = 'Django template filter utils to render videos an thumbnails.',

0 commit comments

Comments
 (0)