2014-01-15 04:45:23 +00:00
|
|
|
/*
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
Copyright 2014 The Perkeep Authors
|
2014-01-15 04:45:23 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Package foursquare implements an importer for foursquare.com accounts.
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
package foursquare // import "perkeep.org/pkg/importer/foursquare"
|
2014-01-15 04:45:23 +00:00
|
|
|
|
|
|
|
import (
|
2017-11-26 09:05:38 +00:00
|
|
|
"context"
|
2014-01-15 04:45:23 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2014-03-09 21:18:27 +00:00
|
|
|
"net/url"
|
2014-04-12 20:42:51 +00:00
|
|
|
"path"
|
2014-04-06 07:56:32 +00:00
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
2014-03-09 21:18:27 +00:00
|
|
|
"strconv"
|
2014-05-05 18:51:17 +00:00
|
|
|
"strings"
|
2014-01-15 04:45:23 +00:00
|
|
|
"sync"
|
2014-03-09 21:18:27 +00:00
|
|
|
"time"
|
2014-01-15 04:45:23 +00:00
|
|
|
|
2018-01-03 05:03:30 +00:00
|
|
|
"perkeep.org/internal/httputil"
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
"perkeep.org/pkg/blob"
|
|
|
|
"perkeep.org/pkg/importer"
|
|
|
|
"perkeep.org/pkg/schema"
|
|
|
|
"perkeep.org/pkg/schema/nodeattr"
|
2015-12-16 00:22:48 +00:00
|
|
|
|
|
|
|
"go4.org/ctxutil"
|
2016-03-03 15:53:12 +00:00
|
|
|
"golang.org/x/oauth2"
|
2014-01-15 04:45:23 +00:00
|
|
|
)
|
|
|
|
|
2014-03-09 21:18:27 +00:00
|
|
|
const (
|
2014-03-30 22:44:26 +00:00
|
|
|
apiURL = "https://api.foursquare.com/v2/"
|
|
|
|
authURL = "https://foursquare.com/oauth2/authenticate"
|
|
|
|
tokenURL = "https://foursquare.com/oauth2/access_token"
|
|
|
|
|
2014-07-21 21:56:03 +00:00
|
|
|
apiVersion = "20140225"
|
|
|
|
checkinsAPIPath = "users/self/checkins"
|
|
|
|
|
2014-05-06 00:18:39 +00:00
|
|
|
// runCompleteVersion is a cache-busting version number of the
|
|
|
|
// importer code. It should be incremented whenever the
|
|
|
|
// behavior of this importer is updated enough to warrant a
|
|
|
|
// complete run. Otherwise, if the importer runs to
|
|
|
|
// completion, this version number is recorded on the account
|
|
|
|
// permanode and subsequent importers can stop early.
|
2016-04-30 04:21:10 +00:00
|
|
|
runCompleteVersion = "2"
|
2014-05-06 00:18:39 +00:00
|
|
|
|
2014-03-30 22:44:26 +00:00
|
|
|
// Permanode attributes on account node:
|
2014-06-13 22:51:30 +00:00
|
|
|
acctAttrUserId = "foursquareUserId"
|
|
|
|
acctAttrUserFirst = "foursquareFirstName"
|
|
|
|
acctAttrUserLast = "foursquareLastName"
|
|
|
|
acctAttrAccessToken = "oauthAccessToken"
|
2014-07-21 21:56:03 +00:00
|
|
|
|
2014-07-21 22:12:51 +00:00
|
|
|
checkinsRequestLimit = 100 // max number of checkins we will ask for in a checkins list request
|
2014-07-21 21:56:03 +00:00
|
|
|
photosRequestLimit = 5
|
2014-07-30 23:47:34 +00:00
|
|
|
|
|
|
|
attrFoursquareId = "foursquareId"
|
|
|
|
attrFoursquareVenuePermanode = "foursquareVenuePermanode"
|
|
|
|
attrFoursquareCategoryName = "foursquareCategoryName"
|
2014-03-09 21:18:27 +00:00
|
|
|
)
|
|
|
|
|
2014-01-15 04:45:23 +00:00
|
|
|
func init() {
|
2014-03-30 22:44:26 +00:00
|
|
|
importer.Register("foursquare", &imp{
|
|
|
|
imageFileRef: make(map[string]blob.Ref),
|
|
|
|
})
|
2014-01-15 04:45:23 +00:00
|
|
|
}
|
|
|
|
|
2014-03-30 22:44:26 +00:00
|
|
|
var _ importer.ImporterSetupHTMLer = (*imp)(nil)
|
2014-01-15 04:45:23 +00:00
|
|
|
|
2014-03-30 22:44:26 +00:00
|
|
|
type imp struct {
|
|
|
|
mu sync.Mutex // guards following
|
2014-03-12 01:13:37 +00:00
|
|
|
imageFileRef map[string]blob.Ref // url to file schema blob
|
2014-05-14 23:09:20 +00:00
|
|
|
|
|
|
|
importer.OAuth2 // for CallbackRequestAccount and CallbackURLParameters
|
2014-03-30 22:44:26 +00:00
|
|
|
}
|
|
|
|
|
2016-04-30 02:35:59 +00:00
|
|
|
func (im *imp) NeedsAPIKey() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
func (im *imp) SupportsIncremental() bool {
|
|
|
|
return true
|
|
|
|
}
|
2014-03-12 01:13:37 +00:00
|
|
|
|
2014-03-30 22:44:26 +00:00
|
|
|
func (im *imp) IsAccountReady(acctNode *importer.Object) (ok bool, err error) {
|
|
|
|
if acctNode.Attr(acctAttrUserId) != "" && acctNode.Attr(acctAttrAccessToken) != "" {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
2014-01-15 04:45:23 +00:00
|
|
|
}
|
|
|
|
|
2014-03-30 22:44:26 +00:00
|
|
|
func (im *imp) SummarizeAccount(acct *importer.Object) string {
|
|
|
|
ok, err := im.IsAccountReady(acct)
|
|
|
|
if err != nil {
|
|
|
|
return "Not configured; error = " + err.Error()
|
2014-01-15 04:45:23 +00:00
|
|
|
}
|
2014-03-30 22:44:26 +00:00
|
|
|
if !ok {
|
|
|
|
return "Not configured"
|
2014-01-15 04:45:23 +00:00
|
|
|
}
|
2014-03-30 22:44:26 +00:00
|
|
|
if acct.Attr(acctAttrUserFirst) == "" && acct.Attr(acctAttrUserLast) == "" {
|
|
|
|
return fmt.Sprintf("userid %s", acct.Attr(acctAttrUserId))
|
2014-01-15 04:45:23 +00:00
|
|
|
}
|
2014-03-30 22:44:26 +00:00
|
|
|
return fmt.Sprintf("userid %s (%s %s)", acct.Attr(acctAttrUserId),
|
|
|
|
acct.Attr(acctAttrUserFirst), acct.Attr(acctAttrUserLast))
|
2014-01-15 04:45:23 +00:00
|
|
|
}
|
|
|
|
|
2014-03-30 22:44:26 +00:00
|
|
|
func (im *imp) AccountSetupHTML(host *importer.Host) string {
|
|
|
|
base := host.ImporterBaseURL() + "foursquare"
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
<h1>Configuring Foursquare</h1>
|
|
|
|
<p>Visit <a href='https://foursquare.com/developers/apps'>https://foursquare.com/developers/apps</a> and click "Create a new app".</p>
|
|
|
|
<p>Use the following settings:</p>
|
|
|
|
<ul>
|
|
|
|
<li>Download / welcome page url: <b>%s</b></li>
|
|
|
|
<li>Your privacy policy url: <b>%s</b></li>
|
|
|
|
<li>Redirect URI(s): <b>%s</b></li>
|
|
|
|
</ul>
|
|
|
|
<p>Click "SAVE CHANGES". Copy the "Client ID" and "Client Secret" into the boxes above.</p>
|
|
|
|
`, base, base+"/privacy", base+"/callback")
|
2014-01-15 04:45:23 +00:00
|
|
|
}
|
|
|
|
|
2014-03-30 22:44:26 +00:00
|
|
|
// A run is our state for a given run of the importer.
|
|
|
|
type run struct {
|
|
|
|
*importer.RunContext
|
2014-05-06 00:18:39 +00:00
|
|
|
im *imp
|
|
|
|
incremental bool // whether we've completed a run in the past
|
|
|
|
|
|
|
|
mu sync.Mutex // guards anyErr
|
|
|
|
anyErr bool
|
2014-01-15 04:45:23 +00:00
|
|
|
}
|
|
|
|
|
2014-03-30 22:44:26 +00:00
|
|
|
func (r *run) token() string {
|
|
|
|
return r.RunContext.AccountNode().Attr(acctAttrAccessToken)
|
2014-01-15 04:45:23 +00:00
|
|
|
}
|
|
|
|
|
2014-03-30 22:44:26 +00:00
|
|
|
func (im *imp) Run(ctx *importer.RunContext) error {
|
|
|
|
r := &run{
|
2014-05-06 00:18:39 +00:00
|
|
|
RunContext: ctx,
|
|
|
|
im: im,
|
2014-06-13 22:51:30 +00:00
|
|
|
incremental: ctx.AccountNode().Attr(importer.AcctAttrCompletedVersion) == runCompleteVersion,
|
2014-03-09 21:18:27 +00:00
|
|
|
}
|
|
|
|
|
2014-03-30 22:44:26 +00:00
|
|
|
if err := r.importCheckins(); err != nil {
|
|
|
|
return err
|
2014-03-12 01:13:37 +00:00
|
|
|
}
|
2014-05-06 00:18:39 +00:00
|
|
|
|
|
|
|
r.mu.Lock()
|
|
|
|
anyErr := r.anyErr
|
|
|
|
r.mu.Unlock()
|
|
|
|
|
|
|
|
if !anyErr {
|
2014-06-13 22:51:30 +00:00
|
|
|
if err := r.AccountNode().SetAttrs(importer.AcctAttrCompletedVersion, runCompleteVersion); err != nil {
|
2014-05-06 00:18:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-12 01:13:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-06 00:18:39 +00:00
|
|
|
func (r *run) errorf(format string, args ...interface{}) {
|
|
|
|
log.Printf(format, args...)
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
r.anyErr = true
|
|
|
|
}
|
|
|
|
|
2014-03-12 01:13:37 +00:00
|
|
|
// urlFileRef slurps urlstr from the net, writes to a file and returns its
|
2016-05-24 15:59:50 +00:00
|
|
|
// fileref or "" on error or if urlstr was empty.
|
2014-04-12 20:42:51 +00:00
|
|
|
func (r *run) urlFileRef(urlstr, filename string) string {
|
2014-03-30 22:44:26 +00:00
|
|
|
im := r.im
|
|
|
|
im.mu.Lock()
|
2014-03-12 01:13:37 +00:00
|
|
|
if br, ok := im.imageFileRef[urlstr]; ok {
|
2014-03-30 22:44:26 +00:00
|
|
|
im.mu.Unlock()
|
2014-03-12 01:13:37 +00:00
|
|
|
return br.String()
|
|
|
|
}
|
2014-03-30 22:44:26 +00:00
|
|
|
im.mu.Unlock()
|
|
|
|
|
2016-05-24 15:59:50 +00:00
|
|
|
if urlstr == "" {
|
|
|
|
return ""
|
|
|
|
}
|
2016-04-25 20:48:38 +00:00
|
|
|
res, err := ctxutil.Client(r.Context()).Get(urlstr)
|
2014-03-12 01:13:37 +00:00
|
|
|
if err != nil {
|
2016-05-24 15:59:50 +00:00
|
|
|
log.Printf("foursquare: couldn't fetch image %q: %v", urlstr, err)
|
2014-03-12 01:13:37 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
2014-04-12 20:42:51 +00:00
|
|
|
fileRef, err := schema.WriteFileFromReader(r.Host.Target(), filename, res.Body)
|
2014-03-12 01:13:37 +00:00
|
|
|
if err != nil {
|
2014-05-06 00:18:39 +00:00
|
|
|
r.errorf("couldn't write file: %v", err)
|
2014-03-12 01:13:37 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2014-03-30 22:44:26 +00:00
|
|
|
im.mu.Lock()
|
|
|
|
defer im.mu.Unlock()
|
2014-03-12 01:13:37 +00:00
|
|
|
im.imageFileRef[urlstr] = fileRef
|
|
|
|
return fileRef.String()
|
|
|
|
}
|
|
|
|
|
2014-04-06 07:56:32 +00:00
|
|
|
type byCreatedAt []*checkinItem
|
|
|
|
|
2016-04-30 02:35:59 +00:00
|
|
|
func (s byCreatedAt) Less(i, j int) bool {
|
|
|
|
return s[i].CreatedAt < s[j].CreatedAt
|
|
|
|
}
|
|
|
|
func (s byCreatedAt) Len() int {
|
|
|
|
return len(s)
|
|
|
|
}
|
|
|
|
func (s byCreatedAt) Swap(i, j int) {
|
|
|
|
s[i], s[j] = s[j], s[i]
|
|
|
|
}
|
2014-04-06 07:56:32 +00:00
|
|
|
|
2014-03-30 22:44:26 +00:00
|
|
|
func (r *run) importCheckins() error {
|
2014-07-21 21:56:03 +00:00
|
|
|
limit := checkinsRequestLimit
|
2014-03-09 21:18:27 +00:00
|
|
|
offset := 0
|
|
|
|
continueRequests := true
|
|
|
|
|
|
|
|
for continueRequests {
|
|
|
|
resp := checkinsList{}
|
2016-05-11 17:13:10 +00:00
|
|
|
if err := r.im.doUserAPI(r.Context(), r.token(), &resp, checkinsAPIPath, "limit", strconv.Itoa(limit), "offset", strconv.Itoa(offset)); err != nil {
|
2014-03-09 21:18:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
itemcount := len(resp.Response.Checkins.Items)
|
2014-04-23 05:37:42 +00:00
|
|
|
log.Printf("foursquare: importing %d checkins (offset %d)", itemcount, offset)
|
|
|
|
if itemcount < limit {
|
2014-03-09 21:18:27 +00:00
|
|
|
continueRequests = false
|
|
|
|
} else {
|
|
|
|
offset += itemcount
|
|
|
|
}
|
|
|
|
|
2014-03-30 22:44:26 +00:00
|
|
|
checkinsNode, err := r.getTopLevelNode("checkins", "Checkins")
|
2014-03-09 21:18:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-03-30 22:44:26 +00:00
|
|
|
placesNode, err := r.getTopLevelNode("places", "Places")
|
2014-03-09 21:18:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-30 02:35:59 +00:00
|
|
|
pplNode, err := r.getTopLevelNode("people", "People")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-04-06 07:56:32 +00:00
|
|
|
sort.Sort(byCreatedAt(resp.Response.Checkins.Items))
|
2014-05-06 00:18:39 +00:00
|
|
|
sawOldItem := false
|
2014-03-09 21:18:27 +00:00
|
|
|
for _, checkin := range resp.Response.Checkins.Items {
|
2014-04-06 07:56:32 +00:00
|
|
|
placeNode, err := r.importPlace(placesNode, &checkin.Venue)
|
2014-03-09 21:18:27 +00:00
|
|
|
if err != nil {
|
2016-04-30 02:35:59 +00:00
|
|
|
r.errorf("Foursquare importer: error importing place %s: %v", checkin.Venue.Id, err)
|
2014-03-09 21:18:27 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-04-30 02:35:59 +00:00
|
|
|
companionRefs, err := r.importCompanions(pplNode, checkin.With)
|
2014-03-09 21:18:27 +00:00
|
|
|
if err != nil {
|
2016-04-30 02:35:59 +00:00
|
|
|
r.errorf("Foursquare importer: error importing companions for checkin %s: %v", checkin.Id, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, dup, err := r.importCheckin(checkinsNode, checkin, placeNode.PermanodeRef(), companionRefs)
|
|
|
|
if err != nil {
|
|
|
|
r.errorf("Foursquare importer: error importing checkin %s: %v", checkin.Id, err)
|
2014-03-09 21:18:27 +00:00
|
|
|
continue
|
|
|
|
}
|
2014-04-06 07:56:32 +00:00
|
|
|
|
2014-05-06 00:18:39 +00:00
|
|
|
if dup {
|
|
|
|
sawOldItem = true
|
|
|
|
}
|
|
|
|
|
2014-05-05 18:51:17 +00:00
|
|
|
err = r.importPhotos(placeNode, dup)
|
2014-04-06 07:56:32 +00:00
|
|
|
if err != nil {
|
2016-04-30 02:35:59 +00:00
|
|
|
r.errorf("Foursquare importer: error importing photos for checkin %s: %v", checkin.Id, err)
|
2014-04-06 07:56:32 +00:00
|
|
|
continue
|
|
|
|
}
|
2014-03-09 21:18:27 +00:00
|
|
|
}
|
2014-05-06 00:18:39 +00:00
|
|
|
if sawOldItem && r.incremental {
|
|
|
|
break
|
|
|
|
}
|
2014-03-09 21:18:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-05 18:51:17 +00:00
|
|
|
func (r *run) importPhotos(placeNode *importer.Object, checkinWasDup bool) error {
|
2014-04-06 07:56:32 +00:00
|
|
|
photosNode, err := placeNode.ChildPathObject("photos")
|
2014-01-15 04:45:23 +00:00
|
|
|
if err != nil {
|
2014-03-09 21:18:27 +00:00
|
|
|
return err
|
2014-01-15 04:45:23 +00:00
|
|
|
}
|
|
|
|
|
2014-04-23 05:37:42 +00:00
|
|
|
if err := photosNode.SetAttrs(
|
2014-07-30 23:47:34 +00:00
|
|
|
nodeattr.Title, "Photos of "+placeNode.Attr("title"),
|
|
|
|
nodeattr.DefaultVisibility, "hide"); err != nil {
|
2014-04-23 05:37:42 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-04-06 07:56:32 +00:00
|
|
|
|
2014-05-05 18:51:17 +00:00
|
|
|
nHave := 0
|
|
|
|
photosNode.ForeachAttr(func(key, value string) {
|
|
|
|
if strings.HasPrefix(key, "camliPath:") {
|
|
|
|
nHave++
|
|
|
|
}
|
|
|
|
})
|
2014-07-21 21:56:03 +00:00
|
|
|
nWant := photosRequestLimit
|
2014-05-05 18:51:17 +00:00
|
|
|
if checkinWasDup {
|
|
|
|
nWant = 1
|
|
|
|
}
|
|
|
|
if nHave >= nWant {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-11 17:13:10 +00:00
|
|
|
clientID, clientSecret, err := r.Credentials()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-04-06 07:56:32 +00:00
|
|
|
resp := photosList{}
|
2016-05-11 17:13:10 +00:00
|
|
|
if err = r.im.doCredAPI(r.Context(), clientID, clientSecret, &resp,
|
2014-07-30 23:47:34 +00:00
|
|
|
"venues/"+placeNode.Attr(attrFoursquareId)+"/photos",
|
2014-05-05 18:51:17 +00:00
|
|
|
"limit", strconv.Itoa(nWant)); err != nil {
|
2014-04-06 07:56:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-04-23 05:37:42 +00:00
|
|
|
var need []*photoItem
|
2014-04-06 07:56:32 +00:00
|
|
|
for _, photo := range resp.Response.Photos.Items {
|
|
|
|
attr := "camliPath:" + photo.Id + filepath.Ext(photo.Suffix)
|
2014-04-23 05:37:42 +00:00
|
|
|
if photosNode.Attr(attr) == "" {
|
|
|
|
need = append(need, photo)
|
2014-04-06 07:56:32 +00:00
|
|
|
}
|
2014-04-23 05:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(need) > 0 {
|
2014-07-30 23:47:34 +00:00
|
|
|
venueTitle := placeNode.Attr(nodeattr.Title)
|
|
|
|
log.Printf("foursquare: importing %d photos for venue %s", len(need), venueTitle)
|
2014-04-23 05:37:42 +00:00
|
|
|
for _, photo := range need {
|
|
|
|
attr := "camliPath:" + photo.Id + filepath.Ext(photo.Suffix)
|
2014-05-05 18:51:17 +00:00
|
|
|
if photosNode.Attr(attr) != "" {
|
|
|
|
continue
|
|
|
|
}
|
2014-04-23 05:37:42 +00:00
|
|
|
url := photo.Prefix + "original" + photo.Suffix
|
2014-07-30 23:47:34 +00:00
|
|
|
log.Printf("foursquare: importing photo for venue %s: %s", venueTitle, url)
|
2014-04-23 05:37:42 +00:00
|
|
|
ref := r.urlFileRef(url, "")
|
|
|
|
if ref == "" {
|
2014-05-06 00:18:39 +00:00
|
|
|
r.errorf("Error slurping photo: %s", url)
|
2014-04-23 05:37:42 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := photosNode.SetAttr(attr, ref); err != nil {
|
2014-05-06 00:18:39 +00:00
|
|
|
r.errorf("Error adding venue photo: %#v", err)
|
2014-04-23 05:37:42 +00:00
|
|
|
}
|
2014-04-06 07:56:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-30 02:35:59 +00:00
|
|
|
func (r *run) importCheckin(parent *importer.Object, checkin *checkinItem, placeRef blob.Ref, companionRefs []string) (checkinNode *importer.Object, dup bool, err error) {
|
2014-05-05 18:51:17 +00:00
|
|
|
checkinNode, err = parent.ChildPathObject(checkin.Id)
|
2014-04-06 07:56:32 +00:00
|
|
|
if err != nil {
|
2014-05-05 18:51:17 +00:00
|
|
|
return
|
2014-04-06 07:56:32 +00:00
|
|
|
}
|
|
|
|
|
2014-03-09 21:18:27 +00:00
|
|
|
title := fmt.Sprintf("Checkin at %s", checkin.Venue.Name)
|
2014-07-30 23:47:34 +00:00
|
|
|
dup = checkinNode.Attr(nodeattr.StartDate) != ""
|
2014-03-09 21:18:27 +00:00
|
|
|
if err := checkinNode.SetAttrs(
|
2014-07-30 23:47:34 +00:00
|
|
|
attrFoursquareId, checkin.Id,
|
|
|
|
attrFoursquareVenuePermanode, placeRef.String(),
|
|
|
|
nodeattr.Type, "foursquare.com:checkin",
|
|
|
|
nodeattr.StartDate, schema.RFC3339FromTime(time.Unix(checkin.CreatedAt, 0)),
|
|
|
|
nodeattr.Title, title); err != nil {
|
2014-05-05 18:51:17 +00:00
|
|
|
return nil, false, err
|
2014-03-09 21:18:27 +00:00
|
|
|
}
|
2016-04-30 02:35:59 +00:00
|
|
|
|
|
|
|
if err := checkinNode.SetAttrValues("with", companionRefs); err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
2014-05-05 18:51:17 +00:00
|
|
|
return checkinNode, dup, nil
|
2014-03-09 21:18:27 +00:00
|
|
|
}
|
|
|
|
|
2016-04-30 02:35:59 +00:00
|
|
|
func (r *run) importCompanions(parent *importer.Object, companions []*user) (companionRefs []string, err error) {
|
|
|
|
for _, user := range companions {
|
|
|
|
personNode, err := parent.ChildPathObject(user.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-05-24 15:59:50 +00:00
|
|
|
attrs := []string{
|
2016-04-30 02:35:59 +00:00
|
|
|
attrFoursquareId, user.Id,
|
|
|
|
nodeattr.Type, "foursquare.com:person",
|
2016-05-24 15:59:50 +00:00
|
|
|
nodeattr.Title, user.FirstName + " " + user.LastName,
|
2016-04-30 02:35:59 +00:00
|
|
|
nodeattr.GivenName, user.FirstName,
|
2016-05-24 15:59:50 +00:00
|
|
|
nodeattr.FamilyName, user.LastName,
|
|
|
|
}
|
|
|
|
if icon := user.icon(); icon != "" {
|
|
|
|
attrs = append(attrs, nodeattr.CamliContentImage, r.urlFileRef(icon, path.Base(icon)))
|
|
|
|
}
|
|
|
|
if err := personNode.SetAttrs(attrs...); err != nil {
|
2016-04-30 02:35:59 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
companionRefs = append(companionRefs, personNode.PermanodeRef().String())
|
|
|
|
}
|
|
|
|
return companionRefs, nil
|
|
|
|
}
|
|
|
|
|
2014-04-06 07:56:32 +00:00
|
|
|
func (r *run) importPlace(parent *importer.Object, place *venueItem) (*importer.Object, error) {
|
2014-03-09 21:18:27 +00:00
|
|
|
placeNode, err := parent.ChildPathObject(place.Id)
|
2014-01-15 04:45:23 +00:00
|
|
|
if err != nil {
|
2014-04-06 07:56:32 +00:00
|
|
|
return nil, err
|
2014-03-12 01:13:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
catName := ""
|
|
|
|
if cat := place.primaryCategory(); cat != nil {
|
|
|
|
catName = cat.Name
|
2014-01-15 04:45:23 +00:00
|
|
|
}
|
2014-03-09 21:18:27 +00:00
|
|
|
|
2016-05-05 17:07:22 +00:00
|
|
|
attrs := []string{
|
2014-07-30 23:47:34 +00:00
|
|
|
attrFoursquareId, place.Id,
|
|
|
|
nodeattr.Type, "foursquare.com:venue",
|
|
|
|
attrFoursquareCategoryName, catName,
|
|
|
|
nodeattr.Title, place.Name,
|
2016-05-05 17:07:22 +00:00
|
|
|
}
|
2016-05-24 15:59:50 +00:00
|
|
|
if icon := place.icon(); icon != "" {
|
|
|
|
attrs = append(attrs,
|
|
|
|
nodeattr.CamliContentImage, r.urlFileRef(icon, path.Base(icon)))
|
|
|
|
}
|
2016-05-05 17:07:22 +00:00
|
|
|
if place.Location != nil {
|
|
|
|
attrs = append(attrs,
|
|
|
|
nodeattr.StreetAddress, place.Location.Address,
|
|
|
|
nodeattr.AddressLocality, place.Location.City,
|
|
|
|
nodeattr.PostalCode, place.Location.PostalCode,
|
|
|
|
nodeattr.AddressRegion, place.Location.State,
|
|
|
|
nodeattr.AddressCountry, place.Location.Country,
|
|
|
|
nodeattr.Latitude, fmt.Sprint(place.Location.Lat),
|
|
|
|
nodeattr.Longitude, fmt.Sprint(place.Location.Lng))
|
|
|
|
}
|
|
|
|
if err := placeNode.SetAttrs(attrs...); err != nil {
|
2014-04-06 07:56:32 +00:00
|
|
|
return nil, err
|
2014-03-09 21:18:27 +00:00
|
|
|
}
|
|
|
|
|
2014-04-06 07:56:32 +00:00
|
|
|
return placeNode, nil
|
2014-01-15 04:45:23 +00:00
|
|
|
}
|
|
|
|
|
2014-03-30 22:44:26 +00:00
|
|
|
func (r *run) getTopLevelNode(path string, title string) (*importer.Object, error) {
|
|
|
|
childObject, err := r.RootNode().ChildPathObject(path)
|
2014-03-09 21:18:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-07-30 23:47:34 +00:00
|
|
|
if err := childObject.SetAttr(nodeattr.Title, title); err != nil {
|
2014-03-09 21:18:27 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return childObject, nil
|
|
|
|
}
|
|
|
|
|
2015-12-12 21:47:31 +00:00
|
|
|
func (im *imp) getUserInfo(ctx context.Context, accessToken string) (user, error) {
|
2014-03-30 22:44:26 +00:00
|
|
|
var ui userInfo
|
2016-05-11 17:13:10 +00:00
|
|
|
if err := im.doUserAPI(ctx, accessToken, &ui, "users/self"); err != nil {
|
2014-03-30 22:44:26 +00:00
|
|
|
return user{}, err
|
2014-01-15 04:45:23 +00:00
|
|
|
}
|
2014-03-30 22:44:26 +00:00
|
|
|
if ui.Response.User.Id == "" {
|
|
|
|
return user{}, fmt.Errorf("No userid returned")
|
2014-03-09 21:18:27 +00:00
|
|
|
}
|
2014-03-30 22:44:26 +00:00
|
|
|
return ui.Response.User, nil
|
2014-03-09 21:18:27 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 17:13:10 +00:00
|
|
|
// doUserAPI makes requests to the Foursquare API with a user token.
|
|
|
|
// https://developer.foursquare.com/overview/auth#requests
|
|
|
|
func (im *imp) doUserAPI(ctx context.Context, accessToken string, result interface{}, apiPath string, keyval ...string) error {
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("oauth_token", accessToken)
|
|
|
|
return im.doAPI(ctx, form, result, apiPath, keyval...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// doCredAPI makes userless requests to the Foursquare API, which have a larger
|
|
|
|
// quota than user requests for some endpoints.
|
|
|
|
// https://developer.foursquare.com/overview/auth#userless
|
|
|
|
// https://developer.foursquare.com/overview/ratelimits
|
|
|
|
func (im *imp) doCredAPI(ctx context.Context, clientID, clientSecret string, result interface{}, apiPath string, keyval ...string) error {
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("client_id", clientID)
|
|
|
|
form.Set("client_secret", clientSecret)
|
|
|
|
return im.doAPI(ctx, form, result, apiPath, keyval...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (im *imp) doAPI(ctx context.Context, form url.Values, result interface{}, apiPath string, keyval ...string) error {
|
2014-03-09 21:18:27 +00:00
|
|
|
if len(keyval)%2 == 1 {
|
|
|
|
panic("Incorrect number of keyval arguments")
|
|
|
|
}
|
|
|
|
|
2014-07-21 21:56:03 +00:00
|
|
|
form.Set("v", apiVersion) // 4sq requires this to version their API
|
2014-03-09 21:18:27 +00:00
|
|
|
for i := 0; i < len(keyval); i += 2 {
|
|
|
|
form.Set(keyval[i], keyval[i+1])
|
|
|
|
}
|
|
|
|
|
|
|
|
fullURL := apiURL + apiPath
|
2014-03-30 22:44:26 +00:00
|
|
|
res, err := doGet(ctx, fullURL, form)
|
2014-03-09 21:18:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = httputil.DecodeJSON(res, result)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error parsing response for %s: %v", fullURL, err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-12 21:47:31 +00:00
|
|
|
func doGet(ctx context.Context, url string, form url.Values) (*http.Response, error) {
|
2014-03-09 21:18:27 +00:00
|
|
|
requestURL := url + "?" + form.Encode()
|
|
|
|
req, err := http.NewRequest("GET", requestURL, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-12-16 00:22:48 +00:00
|
|
|
res, err := ctxutil.Client(ctx).Do(req)
|
2014-03-09 21:18:27 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error fetching %s: %v", url, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("Get request on %s failed with: %s", requestURL, res.Status)
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2016-03-03 15:53:12 +00:00
|
|
|
// auth returns a new oauth2 Config
|
|
|
|
func auth(ctx *importer.SetupContext) (*oauth2.Config, error) {
|
|
|
|
clientID, secret, err := ctx.Credentials()
|
2014-03-30 22:44:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-03 15:53:12 +00:00
|
|
|
return &oauth2.Config{
|
|
|
|
ClientID: clientID,
|
2014-03-30 22:44:26 +00:00
|
|
|
ClientSecret: secret,
|
2016-03-03 15:53:12 +00:00
|
|
|
Endpoint: oauth2.Endpoint{
|
|
|
|
AuthURL: authURL,
|
|
|
|
TokenURL: tokenURL,
|
|
|
|
},
|
|
|
|
RedirectURL: ctx.CallbackURL(),
|
|
|
|
// No scope needed for foursquare as far as I can tell
|
2014-03-30 22:44:26 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (im *imp) ServeSetup(w http.ResponseWriter, r *http.Request, ctx *importer.SetupContext) error {
|
|
|
|
oauthConfig, err := auth(ctx)
|
2014-05-14 23:09:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-03-30 22:44:26 +00:00
|
|
|
}
|
2014-05-14 23:09:20 +00:00
|
|
|
oauthConfig.RedirectURL = im.RedirectURL(im, ctx)
|
|
|
|
state, err := im.RedirectState(im, ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, oauthConfig.AuthCodeURL(state), http.StatusFound)
|
|
|
|
return nil
|
2014-01-15 04:45:23 +00:00
|
|
|
}
|
|
|
|
|
2014-03-30 22:44:26 +00:00
|
|
|
func (im *imp) ServeCallback(w http.ResponseWriter, r *http.Request, ctx *importer.SetupContext) {
|
|
|
|
oauthConfig, err := auth(ctx)
|
|
|
|
if err != nil {
|
|
|
|
httputil.ServeError(w, r, fmt.Errorf("Error getting oauth config: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-01-15 04:45:23 +00:00
|
|
|
if r.Method != "GET" {
|
|
|
|
http.Error(w, "Expected a GET", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
code := r.FormValue("code")
|
|
|
|
if code == "" {
|
|
|
|
http.Error(w, "Expected a code", 400)
|
|
|
|
return
|
|
|
|
}
|
2016-03-03 15:53:12 +00:00
|
|
|
token, err := oauthConfig.Exchange(ctx, code)
|
2014-01-15 04:45:23 +00:00
|
|
|
log.Printf("Token = %#v, error %v", token, err)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Token Exchange error: %v", err)
|
|
|
|
http.Error(w, "token exchange error", 500)
|
|
|
|
return
|
|
|
|
}
|
2014-03-09 21:18:27 +00:00
|
|
|
|
2014-03-30 22:44:26 +00:00
|
|
|
u, err := im.getUserInfo(ctx.Context, token.AccessToken)
|
2014-03-09 21:18:27 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Couldn't get username: %v", err)
|
|
|
|
http.Error(w, "can't get username", 500)
|
|
|
|
return
|
|
|
|
}
|
2014-03-30 22:44:26 +00:00
|
|
|
if err := ctx.AccountNode.SetAttrs(
|
|
|
|
acctAttrUserId, u.Id,
|
|
|
|
acctAttrUserFirst, u.FirstName,
|
|
|
|
acctAttrUserLast, u.LastName,
|
|
|
|
acctAttrAccessToken, token.AccessToken,
|
|
|
|
); err != nil {
|
|
|
|
httputil.ServeError(w, r, fmt.Errorf("Error setting attribute: %v", err))
|
|
|
|
return
|
2014-01-15 04:45:23 +00:00
|
|
|
}
|
2014-03-30 22:44:26 +00:00
|
|
|
http.Redirect(w, r, ctx.AccountURL(), http.StatusFound)
|
|
|
|
|
2014-01-15 04:45:23 +00:00
|
|
|
}
|