# Path: blango/spider.py
# class Spider(BaseSpider):
#     def __init__(self, uri, data = ''):
#         super(Spider, self).__init__(uri)
#         self.data = data
# 
#     def pingback(self, source):
#         if not self._require_fetch():
#             return False
#         pburi = self.stream.headers.get('X-Pingback')
#         if not pburi:
#             r = re.compile('<link.*?rel="pingback".*?href="([^"]+)" ?/?>', re.I)
#             m = r.search(self.data)
#             if not m:
#                 return False
#             pburi = m.group(1)
#         server = ServerProxy(pburi)
#         try:
#             server.pingback.ping(source, self.uri)
#             return True
#         except Error, v:
#             return False
# 
#     def trackback(self, url, page_name='', title='', excerpt=''):
#         if not self._require_fetch():
#             print self.code
#             return False
# 
#         r = re.compile('trackback:ping="([^"]+)"')
#         m = r.search(self.data)
#         if not m:
#             return False
#         tburi = m.group(1)
#         t = Spider(tburi)
#         t.request.add_header('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8')
#         values = { 'title': title,
#             'excerpt': excerpt,
#             'url': url,
#             'blog_name': page_name }
#         data = urllib.urlencode(values)
#         t.request = urllib2.Request(tburi, data)
#         t.fetch()
#         return t.data.find('<error>0</error>') != -1
# 
#     def backlinks(self, back):
#         if not self._require_fetch():
#             return False
# 
#         r = re.compile('<a.*?href="%s".*>' % back)
#         return r.search(self.data) != None
# 
# Path: stem.py
# class Stemmer(object):
#     def __init__(self, lang, encoding=None):
#         self.stemmer = libstemmer.sb_stemmer_new(lang, encoding)
#         if not self.stemmer:
#             raise RuntimeError('Lang "%s" is not supported by this stemmer' % lang)
# 
#         self.mutex = Lock()
# 
#     def __del__(self):
#         libstemmer.sb_stemmer_delete(self.stemmer)
#         # No need to call the parent class, since object.__del__ does not exist
# 
#     def stem(self, word):
#         if isinstance(word, unicode):
#             w = word.encode('utf8')
#         else:
#             w = word
# 
#         # sb_stemmer_stem stemmer is not thread safe
#         self.mutex.acquire()
#         stem = libstemmer.sb_stemmer_stem(self.stemmer, w.lower(), len(w))
#         self.mutex.release()
#         return stem
# 
# HAS_LIBSTEMMER = True


import re
import os
import urlparse
from datetime import datetime, timedelta
from cStringIO import StringIO
from django.db import models
from django.contrib.auth.models import User
from django.template.defaultfilters import slugify, capfirst, force_escape
from django.utils.translation import ugettext, ugettext_lazy as _
from django.utils.safestring import mark_safe
from django.core.files.base import ContentFile
from django.conf import settings
from PIL import Image
from blango.spider import Spider
from stem import Stemmer, HAS_LIBSTEMMER
from markdown import markdown
    def save(self, *args, **kwargs):
        spider = Spider(self.link)
        if 'html' in spider.oembed:
            self.pre_html = '<div class="entry-embed">%s</div>' % \
                spider.oembed['html']
        elif spider.oembed.get('type') == 'photo':
            self.pre_html = '<div class="entry-embed"><img alt="%(title)s" ' \
                    'src="%(url)s" width="%(width)s" height="%(height)s" />' \
                    '</div>' % spider.oembed
        elif not self.thumbnail:
            thumbnail = None
            thumbnail_size = (0, 0)
            required_attrs = ('src', 'alt', 'width', 'height')
            for img in spider.soup.xpath('//img'):
                if all (k in img.attrib for k in required_attrs):
                    width, height = int(img.get('width')), int(img.get('height'))
                    if width * height > reduce(lambda x, y: x * y, thumbnail_size):
                        thumbnail = img
                        thumbnail_size = (width, height)
            # Try to fetch a thumbnail
            if thumbnail is not None:
                THUMBNAIL_SIZE = 150
                img_url = thumbnail.get('src')
                img_data = spider.fetch(img_url)
                im = Image.open(StringIO(img_data))
                im.thumbnail((THUMBNAIL_SIZE, THUMBNAIL_SIZE))
                buf = StringIO()
                im.save(buf, format=im.format)
                self.thumbnail.save(os.path.basename(img_url),
                                    ContentFile(buf.getvalue()))

# Path: blango/spider.py
# class Spider(BaseSpider):
#     def __init__(self, uri, data = ''):
#         super(Spider, self).__init__(uri)
#         self.data = data
# 
#     def pingback(self, source):
#         if not self._require_fetch():
#             return False
#         pburi = self.stream.headers.get('X-Pingback')
#         if not pburi:
#             r = re.compile('<link.*?rel="pingback".*?href="([^"]+)" ?/?>', re.I)
#             m = r.search(self.data)
#             if not m:
#                 return False
#             pburi = m.group(1)
#         server = ServerProxy(pburi)
#         try:
#             server.pingback.ping(source, self.uri)
#             return True
#         except Error, v:
#             return False
# 
#     def trackback(self, url, page_name='', title='', excerpt=''):
#         if not self._require_fetch():
#             print self.code
#             return False
# 
#         r = re.compile('trackback:ping="([^"]+)"')
#         m = r.search(self.data)
#         if not m:
#             return False
#         tburi = m.group(1)
#         t = Spider(tburi)
#         t.request.add_header('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8')
#         values = { 'title': title,
#             'excerpt': excerpt,
#             'url': url,
#             'blog_name': page_name }
#         data = urllib.urlencode(values)
#         t.request = urllib2.Request(tburi, data)
#         t.fetch()
#         return t.data.find('<error>0</error>') != -1
# 
#     def backlinks(self, back):
#         if not self._require_fetch():
#             return False
# 
#         r = re.compile('<a.*?href="%s".*>' % back)
#         return r.search(self.data) != None
# 
# Path: stem.py
# class Stemmer(object):
#     def __init__(self, lang, encoding=None):
#         self.stemmer = libstemmer.sb_stemmer_new(lang, encoding)
#         if not self.stemmer:
#             raise RuntimeError('Lang "%s" is not supported by this stemmer' % lang)
# 
#         self.mutex = Lock()
# 
#     def __del__(self):
#         libstemmer.sb_stemmer_delete(self.stemmer)
#         # No need to call the parent class, since object.__del__ does not exist
# 
#     def stem(self, word):
#         if isinstance(word, unicode):
#             w = word.encode('utf8')
#         else:
#             w = word
# 
#         # sb_stemmer_stem stemmer is not thread safe
#         self.mutex.acquire()
#         stem = libstemmer.sb_stemmer_stem(self.stemmer, w.lower(), len(w))
#         self.mutex.release()
#         return stem
# 
# HAS_LIBSTEMMER = True


# Path: blango/models.py
import re
import os
import urlparse
from datetime import datetime, timedelta
from cStringIO import StringIO
from django.db import models
from django.contrib.auth.models import User
from django.template.defaultfilters import slugify, capfirst, force_escape
from django.utils.translation import ugettext, ugettext_lazy as _
from django.utils.safestring import mark_safe
from django.core.files.base import ContentFile
from django.conf import settings
from PIL import Image
from blango.spider import Spider
from stem import Stemmer, HAS_LIBSTEMMER
from markdown import markdown

    def save(self, *args, **kwargs):
        spider = Spider(self.link)
        if 'html' in spider.oembed:
            self.pre_html = '<div class="entry-embed">%s</div>' % \
                spider.oembed['html']
        elif spider.oembed.get('type') == 'photo':
            self.pre_html = '<div class="entry-embed"><img alt="%(title)s" ' \
                    'src="%(url)s" width="%(width)s" height="%(height)s" />' \
                    '</div>' % spider.oembed
        elif not self.thumbnail:
            thumbnail = None
            thumbnail_size = (0, 0)
            required_attrs = ('src', 'alt', 'width', 'height')
            for img in spider.soup.xpath('//img'):
                if all (k in img.attrib for k in required_attrs):
                    width, height = int(img.get('width')), int(img.get('height'))
                    if width * height > reduce(lambda x, y: x * y, thumbnail_size):
                        thumbnail = img
                        thumbnail_size = (width, height)
            # Try to fetch a thumbnail
            if thumbnail is not None:
                THUMBNAIL_SIZE = 150
                img_url = thumbnail.get('src')
                img_data = spider.fetch(img_url)
                im = Image.open(StringIO(img_data))
                im.thumbnail((THUMBNAIL_SIZE, THUMBNAIL_SIZE))
                buf = StringIO()
                im.save(buf, format=im.format)
                self.thumbnail.save(os.path.basename(img_url),
                                    ContentFile(buf.getvalue()))

                buf.close()