<commit_before>Search = require '../../collections/search'
GoogleSearchResults = require './collections/google_search_results.coffee'
removeDiacritics = require('diacritics').remove

@index = (req, res) ->
  return res.redirect("/") unless req.query.q

  term = removeDiacritics req.query.q
  data = { q: term }
  res.locals.sd.term = term
  page = Number(req.query.page)
  if page && page > 1
    res.locals.sd.page = page
    data.start = (page - 1) * 10

  results = new GoogleSearchResults()
  results.fetch
    dataType: 'jsonp'
    data: data
    success: (results, response) ->
      totalPages = Math.floor(response.queries?.nextPage?[0].totalResults / 10)
      # Google docs say they only supports 99 pages
      # They seem to support a few more but it is unreliable
      totalPages = 99 if totalPages > 99

      models = results.moveMatchResultsToTop term
      res.locals.sd.RESULTS = results.toJSON()
      res.render 'template',
        term: term
        referrer: req.query.referrer<commit_msg>cache requests to google site search api<commit_after>Search = require '../../collections/search'
GoogleSearchResults = require './collections/google_search_results.coffee'
removeDiacritics = require('diacritics').remove

@index = (req, res) ->
  return res.redirect("/") unless req.query.q

  term = removeDiacritics req.query.q
  data = { q: term }
  res.locals.sd.term = term
  page = Number(req.query.page)
  if page && page > 1
    res.locals.sd.page = page
    data.start = (page - 1) * 10

  results = new GoogleSearchResults()
  results.fetch
    dataType: 'jsonp'
    data: data
    cache: true
    cacheTime: 86400 # 1 day
    success: (results, response) ->
      totalPages = Math.floor(response.queries?.nextPage?[0].totalResults / 10)
      # Google docs say they only supports 99 pages
      # They seem to support a few more but it is unreliable
      totalPages = 99 if totalPages > 99

      models = results.moveMatchResultsToTop term
      res.locals.sd.RESULTS = results.toJSON()
      res.render 'template',
        term: term
        referrer: req.query.referrer
{'fraction_non_alphanumeric': 0.11296472831267874, 'fraction_numerical': 0.015252621544327931, 'mean_word_length': 2.9904942965779466, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2233560', 'n_tokens_mistral': 681, 'n_tokens_neox': 642, 'n_words': 179}