2019-12-31 07:16:11 +01:00
|
|
|
package places
|
|
|
|
|
|
|
|
import (
|
2020-09-06 14:48:09 +02:00
|
|
|
"crypto/sha1"
|
2019-12-31 07:16:11 +01:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2020-02-04 05:18:22 +01:00
|
|
|
"strings"
|
2020-04-24 14:11:17 +02:00
|
|
|
"time"
|
2019-12-31 07:16:11 +01:00
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/s2"
|
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-12-31 07:16:11 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Location
|
|
|
|
type Location struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
LocLat float64 `json:"lat"`
|
|
|
|
LocLng float64 `json:"lng"`
|
|
|
|
LocName string `json:"name"`
|
|
|
|
LocCategory string `json:"category"`
|
|
|
|
Place Place `json:"place"`
|
|
|
|
Cached bool `json:"-"`
|
|
|
|
}
|
|
|
|
|
2020-12-04 23:16:22 +01:00
|
|
|
const ApiName = "places"
|
2020-05-26 09:02:19 +02:00
|
|
|
|
2020-09-06 14:48:09 +02:00
|
|
|
var Key = "f60f5b25d59c397989e3cd374f81cdd7710a4fca"
|
|
|
|
var Secret = "photoprism"
|
2020-11-16 19:39:40 +01:00
|
|
|
var UserAgent = "PhotoPrism/0.0.0"
|
2020-12-02 00:18:56 +01:00
|
|
|
var ReverseLookupURL = "https://places.photoprism.app/v1/location/%s"
|
2020-09-06 14:18:40 +02:00
|
|
|
var client = &http.Client{Timeout: 60 * time.Second}
|
2019-12-31 07:16:11 +01:00
|
|
|
|
2020-04-28 19:41:06 +02:00
|
|
|
func NewLocation(id string, lat, lng float64, name, category string, place Place, cached bool) *Location {
|
2020-01-19 13:27:15 +01:00
|
|
|
result := &Location{
|
|
|
|
ID: id,
|
|
|
|
LocLat: lat,
|
|
|
|
LocLng: lng,
|
|
|
|
LocName: name,
|
|
|
|
LocCategory: category,
|
|
|
|
Place: place,
|
|
|
|
Cached: cached,
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2020-12-04 23:16:22 +01:00
|
|
|
|
2019-12-31 07:16:11 +01:00
|
|
|
func FindLocation(id string) (result Location, err error) {
|
|
|
|
if len(id) > 16 || len(id) == 0 {
|
2020-12-04 23:16:22 +01:00
|
|
|
return result, fmt.Errorf("invalid cell %s (%s)", id, ApiName)
|
2019-12-31 07:16:11 +01:00
|
|
|
}
|
|
|
|
|
2020-05-30 01:41:47 +02:00
|
|
|
start := time.Now()
|
2019-12-31 07:16:11 +01:00
|
|
|
lat, lng := s2.LatLng(id)
|
|
|
|
|
|
|
|
if lat == 0.0 || lng == 0.0 {
|
2020-12-04 23:16:22 +01:00
|
|
|
return result, fmt.Errorf("skipping lat %f, lng %f (%s)", lat, lng, ApiName)
|
2019-12-31 07:16:11 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 13:29:01 +01:00
|
|
|
if hit, ok := cache.Get(id); ok {
|
2020-12-04 23:16:22 +01:00
|
|
|
log.Debugf("places: cache hit for lat %f, lng %f", lat, lng)
|
2021-01-08 13:29:01 +01:00
|
|
|
cached := hit.(Location)
|
|
|
|
cached.Cached = true
|
|
|
|
return cached, nil
|
2019-12-31 07:16:11 +01:00
|
|
|
}
|
|
|
|
|
2020-09-06 14:48:09 +02:00
|
|
|
url := fmt.Sprintf(ReverseLookupURL, id)
|
2019-12-31 07:16:11 +01:00
|
|
|
|
2020-12-04 23:16:22 +01:00
|
|
|
log.Debugf("places: sending request to %s", url)
|
2019-12-31 07:16:11 +01:00
|
|
|
|
2020-04-24 14:11:17 +02:00
|
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
2019-12-31 07:16:11 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-12-04 23:16:22 +01:00
|
|
|
log.Errorf("places: %s", err.Error())
|
2019-12-31 07:16:11 +01:00
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2020-09-06 14:18:40 +02:00
|
|
|
req.Header.Set("User-Agent", UserAgent)
|
|
|
|
|
2020-09-06 14:48:09 +02:00
|
|
|
if Key != "" {
|
|
|
|
req.Header.Set("X-Key", Key)
|
|
|
|
req.Header.Set("X-Signature", fmt.Sprintf("%x", sha1.Sum([]byte(Key+url+Secret))))
|
|
|
|
}
|
|
|
|
|
2020-04-28 19:41:06 +02:00
|
|
|
var r *http.Response
|
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
r, err = client.Do(req)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-04-24 14:11:17 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-12-04 23:16:22 +01:00
|
|
|
log.Errorf("places: %s (http request)", err.Error())
|
2020-04-24 14:11:17 +02:00
|
|
|
return result, err
|
|
|
|
} else if r.StatusCode >= 400 {
|
2020-12-04 23:16:22 +01:00
|
|
|
err = fmt.Errorf("request failed with code %d (%s)", r.StatusCode, ApiName)
|
2020-04-24 14:11:17 +02:00
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2019-12-31 07:16:11 +01:00
|
|
|
err = json.NewDecoder(r.Body).Decode(&result)
|
|
|
|
|
|
|
|
if err != nil {
|
2020-12-04 23:16:22 +01:00
|
|
|
log.Errorf("places: %s (decode json)", err.Error())
|
2019-12-31 07:16:11 +01:00
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.ID == "" {
|
2020-12-04 23:16:22 +01:00
|
|
|
return result, fmt.Errorf("no result for %s (%s)", id, ApiName)
|
2019-12-31 07:16:11 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 13:29:01 +01:00
|
|
|
cache.SetDefault(id, result)
|
|
|
|
log.Debugf("places: cached cell %s [%s]", id, time.Since(start))
|
2019-12-31 07:16:11 +01:00
|
|
|
|
|
|
|
result.Cached = false
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Location) CellID() (result string) {
|
|
|
|
return l.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Location) Name() (result string) {
|
2020-02-04 05:18:22 +01:00
|
|
|
return strings.SplitN(l.LocName, "/", 2)[0]
|
2019-12-31 07:16:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l Location) Category() (result string) {
|
|
|
|
return l.LocCategory
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Location) Label() (result string) {
|
|
|
|
return l.Place.LocLabel
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Location) State() (result string) {
|
|
|
|
return l.Place.LocState
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Location) City() (result string) {
|
|
|
|
return l.Place.LocCity
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Location) CountryCode() (result string) {
|
|
|
|
return l.Place.LocCountry
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Location) Latitude() (result float64) {
|
|
|
|
return l.LocLat
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Location) Longitude() (result float64) {
|
|
|
|
return l.LocLng
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Location) Keywords() (result []string) {
|
2021-05-01 11:06:44 +02:00
|
|
|
return txt.UniqueWords(txt.Words(l.Place.LocKeywords))
|
2019-12-31 07:16:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l Location) Source() string {
|
|
|
|
return "places"
|
|
|
|
}
|