# Path: oembed/constants.py
# RESOURCE_CHOICES = (
#     (RESOURCE_PHOTO, 'Photo'),
#     (RESOURCE_VIDEO, 'Video'),
#     (RESOURCE_RICH, 'Rich'),
#     (RESOURCE_LINK, 'Link'),
# )
# 
# Path: oembed/providers.py
# class HTTPProvider(BaseProvider):
#     """
#     Provider class for HTTP providers, like YouTube - basically
#     a proxy.
#     """
#     endpoint_url = ''
#     provides = False # since this is generally a proxy to a pre-existing oembed
#                      # provider, do not re-provide it
#     
#     # NOTE: these attributes are only required if provides = True
#     # see http://oembed.com/ section 2.1 for how to define
#     # but the gist is, this is a list of urls with wildcards
#     # indicating what will match, instead of regex bits, i.e.
#     # http://flickr.com/photos/*
#     url_scheme = None
#     resource_type = None # one of 'photo', 'video', 'rich' or 'link'
#     
#     def __init__(self):
#         self._validate()
#     
#     def _validate(self):
#         if self.provides and (not self.url_scheme or not self.resource_type):
#             raise AttributeError(
#                 'If you want to provide %s up-stream, a url_scheme and ' \
#                 'resource_type must be specified' % self.__name__)
#         if self.provides and self.resource_type not in ('photo', 'video', 'rich', 'link'):
#             raise ValueError('resource_type must be one of "photo", "video", "rich" or "link"')
#     
#     def _fetch(self, url):
#         """
#         Fetches from a URL, respecting GZip encoding, etc.
#         
#         Returns an OEmbedResource instance
#         """
#         return fetch_url(url)
#     
#     def convert_to_resource(self, headers, raw_response, params):
#         if 'content-type' not in headers:
#             raise OEmbedException('Missing mime-type in response')
#         
#         content_type = headers['content-type'].split(';')[0]
#         
#         if content_type in ('text/javascript', 'application/json'):
#             try:
#                 json_response = simplejson.loads(raw_response)
#                 resource = OEmbedResource.create(json_response)
#             except ValueError:
#                 raise OEmbedException('Unable to parse response json')
#         else:
#             raise OEmbedException('Invalid mime-type - %s' % headers['content-type'])
#         return resource
#     
#     def request_resource(self, url, **kwargs):
#         """
#         Request an OEmbedResource for a given url.  Some valid keyword args:
#         - format
#         - maxwidth
#         - maxheight
#         """
#         params = kwargs
#         
#         params['url'] = url
#         params['format'] = 'json'
#         
#         if '?' in self.endpoint_url:
#             url_with_qs = '%s&%s' % (self.endpoint_url.rstrip('&'), urlencode(params))
#         else:
#             url_with_qs = "%s?%s" % (self.endpoint_url, urlencode(params))
#         
#         headers, raw_response = self._fetch(url_with_qs)
#         resource = self.convert_to_resource(headers, raw_response, params)
#         
#         return resource
# 
# Path: oembed/listeners.py
# def start_listening():
#     post_save.connect(provider_site_invalidate, sender=StoredProvider)


from django import VERSION
from django.conf import settings
from django.contrib.contenttypes.generic import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.utils import simplejson
from oembed.constants import RESOURCE_CHOICES
from oembed.providers import HTTPProvider
from oembed.listeners import start_listening
            import oembed
    content_object = GenericForeignKey()

    class Meta:
        ordering = ('-date_added',)
        verbose_name = 'stored OEmbed'
        verbose_name_plural = 'stored OEmbeds'
        if 'mysql' not in db_engine:
            unique_together = ('match', 'maxwidth', 'maxheight')

    def __unicode__(self):
        return self.match
    
    @property
    def response(self):
        return simplejson.loads(self.response_json)


class StoredProviderManager(models.Manager):
    def active(self):
        return self.filter(active=True)

class StoredProvider(models.Model, HTTPProvider):
    """
    Essentially, a stored proxy provider that mimics the interface of a python
    HTTPProvider - used for autodiscovery
    """
    endpoint_url = models.CharField(max_length=255)
    regex = models.CharField(max_length=255)
    wildcard_regex = models.CharField(max_length=255, blank=True,
        help_text='Wild-card version of regex')

# Path: oembed/constants.py
# RESOURCE_CHOICES = (
#     (RESOURCE_PHOTO, 'Photo'),
#     (RESOURCE_VIDEO, 'Video'),
#     (RESOURCE_RICH, 'Rich'),
#     (RESOURCE_LINK, 'Link'),
# )
# 
# Path: oembed/providers.py
# class HTTPProvider(BaseProvider):
#     """
#     Provider class for HTTP providers, like YouTube - basically
#     a proxy.
#     """
#     endpoint_url = ''
#     provides = False # since this is generally a proxy to a pre-existing oembed
#                      # provider, do not re-provide it
#     
#     # NOTE: these attributes are only required if provides = True
#     # see http://oembed.com/ section 2.1 for how to define
#     # but the gist is, this is a list of urls with wildcards
#     # indicating what will match, instead of regex bits, i.e.
#     # http://flickr.com/photos/*
#     url_scheme = None
#     resource_type = None # one of 'photo', 'video', 'rich' or 'link'
#     
#     def __init__(self):
#         self._validate()
#     
#     def _validate(self):
#         if self.provides and (not self.url_scheme or not self.resource_type):
#             raise AttributeError(
#                 'If you want to provide %s up-stream, a url_scheme and ' \
#                 'resource_type must be specified' % self.__name__)
#         if self.provides and self.resource_type not in ('photo', 'video', 'rich', 'link'):
#             raise ValueError('resource_type must be one of "photo", "video", "rich" or "link"')
#     
#     def _fetch(self, url):
#         """
#         Fetches from a URL, respecting GZip encoding, etc.
#         
#         Returns an OEmbedResource instance
#         """
#         return fetch_url(url)
#     
#     def convert_to_resource(self, headers, raw_response, params):
#         if 'content-type' not in headers:
#             raise OEmbedException('Missing mime-type in response')
#         
#         content_type = headers['content-type'].split(';')[0]
#         
#         if content_type in ('text/javascript', 'application/json'):
#             try:
#                 json_response = simplejson.loads(raw_response)
#                 resource = OEmbedResource.create(json_response)
#             except ValueError:
#                 raise OEmbedException('Unable to parse response json')
#         else:
#             raise OEmbedException('Invalid mime-type - %s' % headers['content-type'])
#         return resource
#     
#     def request_resource(self, url, **kwargs):
#         """
#         Request an OEmbedResource for a given url.  Some valid keyword args:
#         - format
#         - maxwidth
#         - maxheight
#         """
#         params = kwargs
#         
#         params['url'] = url
#         params['format'] = 'json'
#         
#         if '?' in self.endpoint_url:
#             url_with_qs = '%s&%s' % (self.endpoint_url.rstrip('&'), urlencode(params))
#         else:
#             url_with_qs = "%s?%s" % (self.endpoint_url, urlencode(params))
#         
#         headers, raw_response = self._fetch(url_with_qs)
#         resource = self.convert_to_resource(headers, raw_response, params)
#         
#         return resource
# 
# Path: oembed/listeners.py
# def start_listening():
#     post_save.connect(provider_site_invalidate, sender=StoredProvider)


# Path: oembed/models.py
from django import VERSION
from django.conf import settings
from django.contrib.contenttypes.generic import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.utils import simplejson
from oembed.constants import RESOURCE_CHOICES
from oembed.providers import HTTPProvider
from oembed.listeners import start_listening
            import oembed

    content_object = GenericForeignKey()

    class Meta:
        ordering = ('-date_added',)
        verbose_name = 'stored OEmbed'
        verbose_name_plural = 'stored OEmbeds'
        if 'mysql' not in db_engine:
            unique_together = ('match', 'maxwidth', 'maxheight')

    def __unicode__(self):
        return self.match
    
    @property
    def response(self):
        return simplejson.loads(self.response_json)


class StoredProviderManager(models.Manager):
    def active(self):
        return self.filter(active=True)

class StoredProvider(models.Model, HTTPProvider):
    """
    Essentially, a stored proxy provider that mimics the interface of a python
    HTTPProvider - used for autodiscovery
    """
    endpoint_url = models.CharField(max_length=255)
    regex = models.CharField(max_length=255)
    wildcard_regex = models.CharField(max_length=255, blank=True,
        help_text='Wild-card version of regex')

    resource_type = models.CharField(choices=RESOURCE_CHOICES, max_length=8)