Skip to content

Commit

Permalink
Merge pull request #23 from simplesconsultoria/issue_13
Browse files Browse the repository at this point in the history
Remove dependency on collective.oembed
  • Loading branch information
rodfersou committed Jun 15, 2015
2 parents 756000b + fd4daa7 commit 0c3f090
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 12 deletions.
14 changes: 9 additions & 5 deletions docs/CHANGES.rst → CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
There's a frood who really knows where his towel is
---------------------------------------------------
Changelog
---------

There's a frood who really knows where his towel is.

1.0b4 (unreleased)
^^^^^^^^^^^^^^^^^^

- Remove dependency on unittest2.
- Remove dependency on collective.oembed (closes `#3`_).
[hvelarde]

- Drop support for Plone 4.1.
- Drop support for Plone 4.1 and remove dependency on unittest2.
[hvelarde]


Expand All @@ -17,7 +19,7 @@ There's a frood who really knows where his towel is
- Fix a couple AJAX quoting/unquoting problems on the TinyMCE plugin.
[jsbueno]

- Add helper methods image_thumb and tag in order to be listed in
- Add helper methods image_thumb and tag in order to be listed in
folder_summary_view [ericof]

- Fix an UnicodeDecodeError with our plugin for TinyMCE [ericof]
Expand Down Expand Up @@ -74,3 +76,5 @@ There's a frood who really knows where his towel is
^^^^^^^^^^^^^^^^^^^

- Initial release.

.. _`#3`: https://github.com/collective/collective.cover/issues/3
File renamed without changes.
2 changes: 1 addition & 1 deletion buildout.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ flake8-extensions =
flake8-coding
flake8-debugger
flake8-quotes
flake8-ignore = E501
flake8-ignore = E501,B901
jshint = True
jshint-bin = bin/jshint
multiprocessing = True
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
'parties into your Plone site.'
long_description = (
open('README.rst').read() + '\n' +
open(os.path.join('docs', 'CREDITS.rst')).read() + '\n' +
open(os.path.join('docs', 'CHANGES.rst')).read()
open('CONTRIBUTORS.rst').read() + '\n' +
open('CHANGES.rst').read()
)

setup(name='sc.embedder',
Expand Down Expand Up @@ -51,7 +51,6 @@
zip_safe=False,
install_requires=[
'collective.dexteritytextindexer',
'collective.oembed <2.0a1',
'five.grok',
'lxml',
'plone.api',
Expand All @@ -65,6 +64,7 @@
'Products.CMFPlone >=4.2',
'Products.GenericSetup',
'Products.TinyMCE',
'python-oembed',
'setuptools',
'z3c.form',
'zope.component',
Expand Down
4 changes: 3 additions & 1 deletion src/sc/embedder/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@
factory=".adapters.JSONDetails"
/>

</configure>
<utility factory="sc.embedder.utility.Consumer" />

</configure>
4 changes: 2 additions & 2 deletions src/sc/embedder/content/embedder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
from collective import dexteritytextindexer
from collective.oembed.interfaces import IConsumer
from five import grok
from lxml import cssselect
from lxml import etree
Expand All @@ -18,9 +17,10 @@
from plone.namedfile.file import NamedImage as ImageValueType
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from sc.embedder import MessageFactory as _
from sc.embedder.interfaces import IConsumer
from z3c.form import button
from z3c.form.interfaces import IFormLayer
from z3c.form.interfaces import IFieldWidget
from z3c.form.interfaces import IFormLayer
from z3c.form.widget import FieldWidget
from zope import schema, component
from zope.component import adapter
Expand Down
8 changes: 8 additions & 0 deletions src/sc/embedder/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@
class IEmbedderLayer(Interface):
""" Browser layer
"""


class IConsumer(Interface):

"""oEmbed consumer utility."""

def get_data(url, maxwidth=None, maxheight=None, format='json'):
"""Return the data provided by the endpoint."""
198 changes: 198 additions & 0 deletions src/sc/embedder/utility.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
# -*- coding: utf-8 -*-
"""This code was taken from collective.oembed and is included here just
to remove the dependency on that package.
See: https://github.com/simplesconsultoria/sc.embedder/issues/13
TODO: Refactor it to get the list of providers on an alternative way.
The Iframely project maintains a list of oEmbed providers updated.
See: https://github.com/itteco/iframely/blob/master/lib/plugins/system/oembed/providers.json
"""
from sc.embedder.config import PROJECTNAME
from sc.embedder.interfaces import IConsumer
from urllib2 import URLError
from zope.interface import implements

import logging
import oembed
import urllib2

logger = logging.getLogger(PROJECTNAME)


class Consumer(object):

"""oEmbed consumer utility."""

implements(IConsumer)

def __init__(self):
self.consumer = None

def get_data(self, url, maxwidth=None, maxheight=None, format='json'):
"""Return the data provided by the endpoint."""

self.initialize_consumer()
request = {}
if maxwidth is not None:
request['maxwidth'] = maxwidth
if maxheight is not None:
request['maxheight'] = maxheight
request['format'] = format

try:
response = self.consumer.embed(url, **request)
return response.getData()
except oembed.OEmbedNoEndpoint, e:
logger.info(e)
except oembed.OEmbedError, e:
# often a mimetype error
logger.info(e)
except urllib2.HTTPError, e:
logger.info(e)
except URLError, e:
# support offline mode
logger.info('offline mode')

def initialize_consumer(self):
if self.consumer is None:
consumer = oembed.OEmbedConsumer()
_enpoints = load_all_endpoints()
for endpoint in _enpoints:
consumer.addEndpoint(endpoint)
self.consumer = consumer


REGEX_PROVIDERS = [
# http://apiblog.youtube.com/2009/10/oembed-support.html
{u'regex': ['regex:.*youtube\.com/watch.*',
'regex:.*youtube\.com/playlist.*'],
u'endpoint': 'http://www.youtube.com/oembed'},

{u'regex': ['http://*.flickr.com/*'],
u'endpoint': 'http://www.flickr.com/services/oembed'},

{u'regex': ['http://qik.com/video/*', 'http://qik.com/*'],
u'endpoint': 'http://qik.com/api/oembed.{format}'},

{u'regex': ['http://*revision3.com/*'],
u'endpoint': 'http://revision3.com/api/oembed/'},

{u'regex': ['http://www.hulu.com/watch/*'],
u'endpoint': 'http://www.hulu.com/api/oembed.{format}'},

{u'regex': ['http://vimeo.com/*'],
u'endpoint': 'http://vimeo.com/api/oembed.{format}'},

{u'regex': ['http://www.collegehumor.com/video/*'],
u'endpoint': 'http://www.collegehumor.com/oembed.{format}'},

{u'regex': ['http://www.polleverywhere.com/polls/*',
'http://www.polleverywhere.com/multiple_choice_polls/*',
'http://www.polleverywhere.com/free_text_polls/*'],
u'endpoint': 'http://www.polleverywhere.com/services/oembed/'},

{u'regex': ['http://www.ifixit.com/*'],
u'endpoint': 'http://www.ifixit.com/Embed'},

{u'regex': ['http://*.smugmug.com/*'],
u'endpoint': 'http://api.smugmug.com/services/oembed/'},

{u'regex': ['http://www.slideshare.net/*/*'],
u'endpoint': 'http://www.slideshare.net/api/oembed/2'},

{u'regex': ['http://www.23hq.com/*/photo/*'],
u'endpoint': 'http://www.23hq.com/23/oembed'},

# http://www.5min.com/APIDocs/Embed.aspx
{u'regex': ['http://www.5min.com/Video/*'],
u'endpoint': 'http://api.5min.com/oembed.{format}'},

# https://dev.twitter.com/docs/embedded-tweets
{u'regex': ['https://twitter.com/*/status*/*'],
u'endpoint': 'https://api.twitter.com/1/statuses/oembed.{format}'},

# http://pic.pbsrc.com/dev_help/Metadata/Metadata_Discovery.htm
{u'regex': ['regex:.*photobucket\\.com/(albums|groups)/.+$'],
u'endpoint': 'http://photobucket.com/oembed'},

# http://pic.pbsrc.com/dev_help/Metadata/Metadata_Discovery.htm
{u'regex': ['http://*.kinomap.com/*'],
u'endpoint': 'http://www.kinomap.com/oembed'},

# http://www.dailymotion.com/doc/api/oembed.html
{u'regex': ['http://www.dailymotion.com/video/*'],
u'endpoint': 'http://www.dailymotion.com/services/oembed'},

{u'regex': ['http://*.clikthrough.com/theater/video/*'],
u'endpoint': 'http://clikthrough.com/services/oembed'},

# http://solutions.dotsub.com/oEmbed
{u'regex': ['http://dotsub.com/view/*'],
u'endpoint': 'http://dotsub.com/services/oembed'},

# blit.tv sends an invalid mime-type back
{u'regex': ['http://*blip.tv/*'],
u'endpoint': 'http://blip.tv/oembed/'},

# http://official.fm/developers/oembed
{u'regex': ['http://official.fm/tracks/*', 'http://official.fm/playlists/*'],
u'endpoint': 'http://official.fm/services/oembed.{format}'},

# http://dev.vhx.tv/oembed.html
{u'regex': ['http://vhx.tv/*'],
u'endpoint': 'http://vhx.tv/services/oembed.{format}'},

{u'regex': ['http://*.nfb.ca/film/*'],
u'endpoint': 'http://www.nfb.ca/remote/services/oembed/'},

# http://instagr.am/developer/embedding/
{u'regex': ['http://instagr.am/p/*', 'http://instagr.am/p/*'],
u'endpoint': 'http://api.instagram.com/oembed'},

{u'regex': ['http://wordpress.tv/*'],
u'endpoint': 'http://wordpress.tv/oembed/'},

{u'regex': ['http://soundcloud.com/*', 'http://soundcloud.com/*/*',
'http://soundcloud.com/*/sets/*', 'http://soundcloud.com/groups/*',
'http://snd.sc/*', 'https://soundcloud.com/*'],
u'endpoint': 'http://soundcloud.com/oembed'},

# http://blog.screenr.com/post/2145539209/screenr-now-supports-oembed
{u'regex': ['http://www.screenr.com/*', 'http://screenr.com/*'],
u'endpoint': 'http://www.screenr.com/api/oembed.{format}'},

{u'regex': ['https://itunes.apple.com/*'],
u'endpoint': 'http://www.screenr.com/api/oembed.{format}'},
]


class WordpressEndPoint(oembed.OEmbedEndpoint):

"""Wordpress wait a for in his query params."""

def __init__(self):
url = 'http://public-api.wordpress.com/oembed/1.0/'
urlSchemes = ['regex:.*.wordpress\.com/.*']
super(WordpressEndPoint, self).__init__(url, urlSchemes=urlSchemes)

def request(self, url, **opt):
query = opt
query['for'] = 'plone'
return super(WordpressEndPoint, self).request(url, **query)


def load_all_endpoints():
endpoints = []

endpoint = WordpressEndPoint()
endpoints.append(endpoint)

providers = REGEX_PROVIDERS

for provider in providers:
endpoint = oembed.OEmbedEndpoint(provider[u'endpoint'], provider[u'regex'])
endpoints.append(endpoint)

return endpoints

0 comments on commit 0c3f090

Please sign in to comment.