1
restaurant-search/build/webcache.go

89 lines
2.3 KiB
Go
Raw Normal View History

2015-08-13 07:29:39 +00:00
/*
* Copyright (c) 2015 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
2015-08-15 08:37:34 +00:00
package main
2015-08-13 07:29:39 +00:00
import (
"bytes"
"crypto/md5"
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
2015-08-14 08:27:09 +00:00
"time"
2015-08-13 07:29:39 +00:00
"github.com/PuerkitoBio/goquery"
)
2015-08-15 08:37:34 +00:00
type webCache struct {
2015-08-14 07:40:37 +00:00
directory string
2015-08-14 08:27:09 +00:00
ticker *time.Ticker
2015-08-13 07:29:39 +00:00
}
2015-08-15 08:37:34 +00:00
func newWebCache(directory string) (*webCache, error) {
2015-08-14 07:40:37 +00:00
if err := os.MkdirAll(directory, 0755); err != nil {
2015-08-13 07:29:39 +00:00
return nil, err
}
2015-08-15 08:37:34 +00:00
cache := &webCache{
2015-08-14 08:27:09 +00:00
directory: directory,
ticker: time.NewTicker(time.Millisecond * 100),
}
return cache, nil
2015-08-13 07:29:39 +00:00
}
2015-08-15 08:37:34 +00:00
func (c *webCache) urlToLocal(url string) string {
2015-08-13 07:29:39 +00:00
hash := md5.New()
hash.Write([]byte(url))
2015-08-14 07:40:37 +00:00
return path.Join(c.directory, fmt.Sprintf("%x.html", hash.Sum(nil)))
2015-08-13 07:29:39 +00:00
}
2015-08-15 08:37:34 +00:00
func (c *webCache) load(url string) (*goquery.Document, error) {
2015-08-13 07:29:39 +00:00
localPath := c.urlToLocal(url)
if file, err := os.Open(localPath); err == nil {
defer file.Close()
return goquery.NewDocumentFromReader(file)
2015-08-14 07:40:37 +00:00
}
2015-08-13 07:29:39 +00:00
2015-08-14 08:27:09 +00:00
<-c.ticker.C
2015-08-14 07:40:37 +00:00
res, err := http.Get(url)
if err != nil {
return nil, err
}
defer res.Body.Close()
2015-08-13 07:29:39 +00:00
2015-08-14 07:40:37 +00:00
var buff bytes.Buffer
if _, err := buff.ReadFrom(res.Body); err != nil {
return nil, err
}
2015-08-13 07:29:39 +00:00
2015-08-14 07:40:37 +00:00
if err := ioutil.WriteFile(localPath, buff.Bytes(), 0644); err != nil {
return nil, err
2015-08-13 07:29:39 +00:00
}
2015-08-14 07:40:37 +00:00
return goquery.NewDocumentFromReader(&buff)
2015-08-13 07:29:39 +00:00
}