 express = require 'express'
 NodeCache = require 'node-cache'
 fs = require 'fs'
+_ = require 'underscore'
 
 app = express()
 cache = new NodeCache()

                urls.push detail_url
            f urls

get_tierschutzUrls = (url) ->
    new Promise (f, r) ->
        request url, (err, response, body) ->
            if err
                console.error err
                r err
                return
            urls = []
            $ = cheerio.load body
            $('td', 'table', '#center').each ->
                elem = $(this)
                if elem.attr 'colspan'
                    return
                else
                    detail_url = tierschutz_base_url + elem.find('a').attr('href')
                    urls.push detail_url
                    return
            f urls

get_data = ->
    new Promise (f, r) ->
        values = cache.get('tiere')
        if not values
            console.log "no cache"
            get_tierfreundeUrls tierfreunde_url
                .then (urls) ->
                    p = []
                    for url in urls
                        p.push get_tierfreunde url
                    Promise.all p
                .then (values) ->
                    cache.set('tiere', values, 60*60*24)
                    f values
                .catch (err) ->

            fs.writeFileSync filename, JSON.stringify postedPets
            f notPostedPet

###
get_data().then (values) ->
    console.log values.length
get_notPostedPet().then (pet) ->
    console.log pet
###

# Return all pets
app.get '/', (req, res) ->
    get_data()
