# Path: oembed/providers.py
# class BaseProvider(object):
#     """
#     Base class for OEmbed resources.
#     """
#     regex = None # regex this provider will match
#     provides = True  # allow this provider to be accessed by third parties
#     
#     def request_resource(self, url, **kwargs):
#         """
#         Get an OEmbedResource from one of the providers configured in this 
#         provider according to the resource url.
#         
#         Args:
#             url: The url of the resource to get.
#             format: Desired response format (json or xml).
#             **kwargs: Optional parameters to pass in to the provider.
#         
#         Returns:
#             OEmbedResource object.
#             
#         If no object returned, raises OEmbedException
#         """
#         raise NotImplementedError
# 
# Path: oembed/resources.py
# class OEmbedResource(object):
#     """
#     OEmbed resource, as well as a factory for creating resource instances
#     from response json
#     """
#     _data = {}
#     content_object = None
#     
#     def __getattr__(self, name):
#         return self._data.get(name)
#     
#     def get_data(self):
#         return self._data
#         
#     def load_data(self, data):
#         self._data = data
#     
#     @property
#     def json(self):
#         return simplejson.dumps(self._data)
#     
#     @classmethod
#     def create(cls, data):
#         if not 'type' in data or not 'version' in data:
#             raise OEmbedException('Missing required fields on OEmbed response.')
# 
#         data['width'] = data.get('width') and int(data['width']) or None
#         data['height'] = data.get('height') and int(data['height']) or None
#         
#         filtered_data = dict([(k, v) for k, v in data.items() if v])
#         
#         resource = cls()
#         resource.load_data(filtered_data)
#         
#         return resource
# 
#     @classmethod
#     def create_json(cls, raw):
#         data = simplejson.loads(raw)
#         return cls.create(data)


import os
import re
    import Image
import oembed
    from cStringIO import StringIO
    from StringIO import StringIO
    from PIL import Image
from django.conf import settings
from django.contrib.sites.models import Site
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage
from oembed.providers import BaseProvider
from oembed.resources import OEmbedResource
from oembed.utils import size_to_nearest, scale
            # open the original to calculate its width and height
            source_file = default_storage.open(image_path)
            img = Image.open(source_file)

            # retrieve image format and dimensions
            format = img.format
            img_width, img_height = img.size

            # do the math-y parts
            new_width, new_height = scale(img_width, img_height, w, h)
            
            img = img.resize((new_width, new_height), Image.ANTIALIAS)

            img_buffer = StringIO()
            img.MAXBLOCK = 1024*1024
            img.save(img_buffer, format=format)

            source_file.close()
            default_storage.save(new_path, ContentFile(img_buffer.getvalue()))
        
        new_url = '%s%s' % (base_url, append)
        
        # get just the filename, i.e. test.jpg - used for generated the title
        # of the returned oembed resource
        image_filename = image_path.rsplit('/', 1)[-1]
        
        data = {'type': 'photo', 'provider_name': '', 'version': '1.0',
                'width': w, 'height': h, 'title': image_filename,
                'url': new_url, 'author_name': '', 'author_url': ''}
        

# Path: oembed/providers.py
# class BaseProvider(object):
#     """
#     Base class for OEmbed resources.
#     """
#     regex = None # regex this provider will match
#     provides = True  # allow this provider to be accessed by third parties
#     
#     def request_resource(self, url, **kwargs):
#         """
#         Get an OEmbedResource from one of the providers configured in this 
#         provider according to the resource url.
#         
#         Args:
#             url: The url of the resource to get.
#             format: Desired response format (json or xml).
#             **kwargs: Optional parameters to pass in to the provider.
#         
#         Returns:
#             OEmbedResource object.
#             
#         If no object returned, raises OEmbedException
#         """
#         raise NotImplementedError
# 
# Path: oembed/resources.py
# class OEmbedResource(object):
#     """
#     OEmbed resource, as well as a factory for creating resource instances
#     from response json
#     """
#     _data = {}
#     content_object = None
#     
#     def __getattr__(self, name):
#         return self._data.get(name)
#     
#     def get_data(self):
#         return self._data
#         
#     def load_data(self, data):
#         self._data = data
#     
#     @property
#     def json(self):
#         return simplejson.dumps(self._data)
#     
#     @classmethod
#     def create(cls, data):
#         if not 'type' in data or not 'version' in data:
#             raise OEmbedException('Missing required fields on OEmbed response.')
# 
#         data['width'] = data.get('width') and int(data['width']) or None
#         data['height'] = data.get('height') and int(data['height']) or None
#         
#         filtered_data = dict([(k, v) for k, v in data.items() if v])
#         
#         resource = cls()
#         resource.load_data(filtered_data)
#         
#         return resource
# 
#     @classmethod
#     def create_json(cls, raw):
#         data = simplejson.loads(raw)
#         return cls.create(data)


# Path: oembed/contrib/oembed_providers.py
import os
import re
    import Image
import oembed
    from cStringIO import StringIO
    from StringIO import StringIO
    from PIL import Image
from django.conf import settings
from django.contrib.sites.models import Site
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage
from oembed.providers import BaseProvider
from oembed.resources import OEmbedResource
from oembed.utils import size_to_nearest, scale

            # open the original to calculate its width and height
            source_file = default_storage.open(image_path)
            img = Image.open(source_file)

            # retrieve image format and dimensions
            format = img.format
            img_width, img_height = img.size

            # do the math-y parts
            new_width, new_height = scale(img_width, img_height, w, h)
            
            img = img.resize((new_width, new_height), Image.ANTIALIAS)

            img_buffer = StringIO()
            img.MAXBLOCK = 1024*1024
            img.save(img_buffer, format=format)

            source_file.close()
            default_storage.save(new_path, ContentFile(img_buffer.getvalue()))
        
        new_url = '%s%s' % (base_url, append)
        
        # get just the filename, i.e. test.jpg - used for generated the title
        # of the returned oembed resource
        image_filename = image_path.rsplit('/', 1)[-1]
        
        data = {'type': 'photo', 'provider_name': '', 'version': '1.0',
                'width': w, 'height': h, 'title': image_filename,
                'url': new_url, 'author_name': '', 'author_url': ''}
        

        return OEmbedResource.create(data)