package fsexplore import ( "encoding/json" "fmt" "github.com/pocketbase/dbx" "github.com/pocketbase/pocketbase" "github.com/pocketbase/pocketbase/models" "jubilee-server/fsq" "jubilee-server/structs" "log" "strconv" ) type ( ReducedDetail struct { Name string `json:"name"` Description string `json:"description"` Category string `json:"category"` Icon string `json:"icon"` Id string `json:"id"` Provider string `json:"provider"` Address string `json:"address"` City string `json:"city"` State string `json:"state"` Postcode string `json:"postcode"` Twitter string `json:"twitter"` Facebook string `json:"facebook"` Url string `json:"url"` Latitude float64 `json:"latitude"` Longitude float64 `json:"longitude"` Images []string `json:"images"` } DbRecord struct { Fsqid string `json:"fsqid" db:"fsqid"` Data string `json:"data" db:"data"` } ) func FsRequest(latLong structs.LatLong, soqval string, soqtype string, limit string) string { log.Println("$$:fsexplore:FsRequest") log.Printf("latLong: %s", latLong) log.Printf("soqval: %s", soqval) log.Printf("limit: %s", limit) log.Printf("soqtype: %s", soqtype) data := GetRemoteFSRequest(latLong, soqval, soqtype, limit) return data } func FsRequestDetails(app *pocketbase.PocketBase, fsqid string) string { log.Println("$$:fsexplore:FsRequestDetails") log.Printf("fsqid: %s", fsqid) formattedDetails := &DbRecord{} dberr := app.Dao().DB(). Select("fsqid", "data").From("fsdetails"). Where(dbx.NewExp("fsqid = {:fsqid}", dbx.Params{"fsqid": fsqid})).One(&formattedDetails) if dberr == nil { log.Printf("-- Cache hit fsDetails %+v\n", formattedDetails.Fsqid) return formattedDetails.Data } data := GetRemoteDetailRequest(fsqid) formattedDetails.Fsqid = fsqid formattedDetails.Data = data go func() { err := SaveReducedDetails(app, *formattedDetails) if err != nil { log.Println(err) } }() return data } func GetRemoteFSRequest(ll structs.LatLong, soqval string, soqtype string, limit string) string { lat := strconv.FormatFloat(ll.Lat, 'f', -1, 64) long := strconv.FormatFloat(ll.Long, 'f', -1, 64) var modifiertype string if soqtype == "q" { modifiertype = "chain" } else { modifiertype = "category" } modifier := soqval client := fsq.NewQueryForLocation("fsq3ksL8PkGBLhDJ6Bydixoa9Lah6djwvm5a5LIWrtmFexc=", lat, long, "", 1000, modifiertype, modifier) log.Printf("client: %s", client) s, err := client.SearchRaw() if err != nil { log.Println("Error:", err) } return s } func GetRemoteDetailRequest(fsqid string) string { client := fsq.NewQueryPlaceDetails("fsq3ksL8PkGBLhDJ6Bydixoa9Lah6djwvm5a5LIWrtmFexc=", fsqid) details, de := client.GetDetailsRaw() if de != nil { log.Println("Error:", de) } photos, pe := client.GetPhotosRaw() if pe != nil { log.Println("Error:", pe) } mergedData, _ := mergeDetails(details, photos) return mergedData } func mergeDetails(details string, photos string) (jstr string, err error) { rd := reduceDetails(details) images := reducePhotos(photos) rd.Images = images log.Printf("RD: %+v\n", rd) j, err := json.Marshal(rd) return string(j), err } func reduceDetails(details string) *ReducedDetail { dataPtr := &fsq.Results4sqPlaces{} reducedDetails := ReducedDetail{} err := json.Unmarshal([]byte(details), dataPtr) log.Println("Error", err) reducedDetails.Name = dataPtr.Name reducedDetails.Description = dataPtr.Description reducedDetails.Category = dataPtr.Categories[0].ShortName reducedDetails.Icon = dataPtr.Categories[0].Icon.Prefix + "64" + dataPtr.Categories[0].Icon.Suffix reducedDetails.Id = dataPtr.FsqId reducedDetails.Provider = "foursquare" reducedDetails.Address = dataPtr.Location.FormattedAddress reducedDetails.City = dataPtr.Location.PostTown reducedDetails.State = dataPtr.Location.Region reducedDetails.Postcode = dataPtr.Location.Postcode reducedDetails.Twitter = dataPtr.SocialMedia.Twitter reducedDetails.Facebook = dataPtr.SocialMedia.FacebookId reducedDetails.Longitude = dataPtr.Geocodes.Main.Longitude reducedDetails.Latitude = dataPtr.Geocodes.Main.Latitude return &reducedDetails } func reducePhotos(photos string) []string { dataPtr := &[]fsq.Photos4sqPlaces{} var images []string err := json.Unmarshal([]byte(photos), dataPtr) log.Println("Error", err) for _, photo := range *dataPtr { prefix := photo.Prefix suffix := photo.Suffix width := photo.Width height := photo.Height ratioHeight := 480 log.Printf("%+v,%+v => 640, %+v \n", width, height, ratioHeight) output := fmt.Sprintf("%s640%s", prefix, suffix) log.Println(output) images = append(images, output) } return images } func SaveReducedDetails(app *pocketbase.PocketBase, newDetails DbRecord) error { log.Println("$$:SaveReducedDetails", newDetails.Fsqid) collection, err := app.Dao().FindCollectionByNameOrId("fsdetails") if err != nil { log.Println(err) return err } record, recErr := app.Dao().FindRecordsByExpr("fsdetails", dbx.NewExp("fsqid = {:fsqid}", dbx.Params{"fsqid": newDetails.Fsqid})) if recErr != nil { log.Println(recErr) return recErr } if len(record) == 0 { log.Println("-- Insert new fsdetails record") record := models.NewRecord(collection) record.Set("fsqid", newDetails.Fsqid) record.Set("data", newDetails.Data) if err := app.Dao().SaveRecord(record); err != nil { log.Println("ERROR!! SaveReducedDetails NewRecord") log.Println(err) return err } } else { log.Println("-- Update weather record") rec := record[0] rec.Set("data", newDetails.Data) if err := app.Dao().SaveRecord(rec); err != nil { log.Println("ERROR!! SaveReducedDetails SaveRecord") log.Println(err) return err } } return nil }