from collections import defaultdict
import re

import requests

from regparser import content
from regparser.layer.layer import Layer
import settings


class Graphics(Layer):
    gid = re.compile(ur'!\[([\w\s]*)\]\(([a-zA-Z0-9.\-]+?)\)')

    def check_for_thumb(self, url):
        thumb_url = re.sub(r'(.(png|gif|jpg))$', '.thumb' + '\\1', url)
        response = requests.head(thumb_url)

        if response.status_code == requests.codes.not_implemented:
            response = requests.get(thumb_url)

        if response.status_code == requests.codes.ok:
            return thumb_url

    def process(self, node):
        """If this node has a marker for an image in it, note where to get
        that image."""
        matches_by_text = defaultdict(list)
        for match in Graphics.gid.finditer(node.text):
            matches_by_text[match.group(0)].append(match)

        layer_el = []
        for text in matches_by_text:
            match = matches_by_text[text][0]
            url = content.ImageOverrides().get(
                match.group(2), settings.DEFAULT_IMAGE_URL % match.group(2))
            layer_el_vals = {
                'text': match.group(0),
                'url': url,
                'alt': match.group(1),
                'locations': list(range(len(matches_by_text[text])))
            }
            thumb_url = self.check_for_thumb(url)

            if thumb_url:
                layer_el_vals['thumb_url'] = thumb_url
            layer_el.append(layer_el_vals)

        if layer_el:
            return layer_el

@@ -38,16 +38,31 @@
 mport re
+%0Aimport logging
 %0A%0Aimport
@@ -355,16 +355,34 @@
 ', url)%0A
+%0A        try:%0A    
         
@@ -408,32 +408,128 @@
 .head(thumb_url)
+%0A        except:%0A            logging.warning(%22Error fetching %25s%22 %25 thumb_url)%0A            return
 %0A%0A        if res

Handle Connection errors, read errors, etc, more gracefully.