Example #1
0
def noun(request, noun, debug = False):
  noun = noun.lstrip('+').rstrip('+')
  track_page_view(request)
  noun_query = NounExternal.objects.filter(noun = noun, status__lte = 30)
  if noun_query.exists():
    if noun_query.count() < 100:
      random.choice([SearchBing, SearchGoogle]).do_next_search(noun)
    this_image = NounExternal.get_random_noun(noun, 30)
    if not this_image.image:
      this_image.populate()
    if this_image.image:
      if debug:
        return detail(request, this_image)
      return this_image.http_image

  random.choice([SearchBing, SearchGoogle]).do_next_search(noun)

  while True:
    noun_query = NounExternal.objects.filter(noun = noun, status__lte = 30)
    if noun_query.exists():
      this_image = NounExternal.get_random_noun(noun, 30)
      if not this_image.image:
        this_image.populate()
      if not this_image.image:
        continue
      if debug:
        return detail(request, this_image)
      return this_image.http_image
    else:
      random.choice([SearchBing, SearchGoogle]).do_next_search(noun)
Example #2
0
def index(request):
  template = 'index.html'
  this_image = NounExternal.get_random()
  data = {'noun': this_image}

  context = RequestContext(request)
  return render_to_response(template, data, context)
Example #3
0
def random_noun(request, width = None, height = None, debug = False):
  track_page_view(request)

  if width and height:
    nouns = NounExternal.objects.only('noun').distinct().values_list('noun', flat = True)
    noun = random.choice(nouns)
    return noun_static(request, noun, width, height, debug)
  else:
    this_image = NounExternal.get_random()
    if debug:
      return detail(request, this_image)
    return this_image.http_image
Example #4
0
def noun_static(request, noun, width, height, debug = False):
  noun = noun.lstrip('+').rstrip('+')
  width = max(min(MAX_IMAGE_WIDTH, int(width)), 1)
  height = max(min(MAX_IMAGE_HEIGHT, int(height)), 1)
  track_page_view(request)

  noun_query = NounStatic.objects.filter(noun = noun, width = width, height = height)[:1]
  if noun_query:
    this_image = noun_query.get()
    if debug:
      return detail(request, this_image)
    return this_image.http_image

  noun_query = NounExternal.objects.filter(noun = noun, width = width, height = height, status__lt = 20)
  if noun_query.exists():
    this_image = noun_query[0]
    if not this_image.image:
      this_image.populate()
    if this_image.status == 10:
      this_image = this_image.to_static()
      if debug:
        return detail(request, this_image)
      return this_image.http_image

  aspect = float(width)/height
  noun_query = NounExternal.objects.filter(noun= noun, width__gte = width, height__gte = height, aspect = aspect, status__lt = 20)
  if noun_query.exists():
    this_image = noun_query[0]
    if not this_image.image:
      this_image.populate()
    if this_image.status == 10:
      this_image = this_image.to_static(size=(width, height))
      if debug:
        return detail(request, this_image)
      return this_image.http_image


  # At this point we couldn't find a suitable match, so... we'll serve
  # up a best fit result but it won't be perminant

  random.choice([SearchBing, SearchGoogle]).do_next_search(noun)

  radius = 1
  search_max = 32
  slope = float(height)/width
  while True:
    noun_query = NounExternal.get_knn_window(noun, width, height, radius)
    if not noun_query.exists():
      radius = radius*2
      if radius > search_max:
        search_max = search_max*2
        if search_max > 2048:
          return Http404
        radius = 1
        random.choice([SearchBing, SearchGoogle]).do_next_search(noun)
      continue

    noun_query = sorted(noun_query, key = lambda noun_obj: noun_obj.compare(width, height) )
    noun_query.reverse()
    while noun_query:
      this_image = noun_query.pop()
      if not this_image.image:
        this_image.populate()
      if not this_image.image:
        continue
      ret = this_image.http_image_resized(size=(width, height))
      if debug:
        return detail(request, this_image, (width, height))
      return this_image.http_image_resized(size=(width, height))