Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*.swp
dist/
magicembed.egg-info/
build
28 changes: 20 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
[![PyPI version](https://badge.fury.io/py/magicembed.svg)](http://badge.fury.io/py/magicembed)

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
Expand All @@ -22,23 +24,32 @@ 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?
-------------------

If you have a requeriments list add this to your requeriments
If you have a requeriments list add this to your requeriments

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

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

Or if you use setup.py
3. <code>add magicembed to **INSTALLED_APPS**</code>

1. add magicembed to **INSTALLED_APPS**
Or if you use setup.py

2. run <code>python setup.py install</code>
1. run <code>python setup.py install</code>

How to use
---------------
Expand All @@ -47,7 +58,7 @@ First add this in the template to load the template tags

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

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

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

26 changes: 19 additions & 7 deletions magicembed/providers.py
Original file line number Diff line number Diff line change
@@ -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):

Expand Down Expand Up @@ -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):
Expand All @@ -47,14 +51,19 @@ 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:
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")


def render_video(self):
return self._call_api()['html']
Expand All @@ -63,7 +72,10 @@ def render_thumbnail(self):
return self._call_api()['thumbnail_url']

def _call_api(self):
data = simplejson.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):
Expand Down
4 changes: 3 additions & 1 deletion magicembed/templatetags/magicembed_tags.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down