diff --git a/analytics/google_analytics.go b/analytics/google_analytics.go new file mode 100644 index 0000000..8eee7e8 --- /dev/null +++ b/analytics/google_analytics.go @@ -0,0 +1,87 @@ +// Package analytics +// https://stackoverflow.com/questions/12837748/analytics-google-api-error-403-user-does-not-have-any-google-analytics-account +package analytics + +import ( + "encoding/json" + "io/ioutil" + "strconv" + + "github.com/mnhkahn/gogogo/logger" + + "golang.org/x/oauth2" + "golang.org/x/oauth2/jwt" +) + +func Analytics(link string, conf *jwt.Config) (map[string]int, error) { + // Initiate an http.Client, the following GET request will be + // authorized and authenticated on the behalf of user@example.com. + client := conf.Client(oauth2.NoContext) + resp, err := client.Get(link) + if err != nil { + return nil, err + } + + anaByts, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + res := new(analyticsResult) + err = json.Unmarshal(anaByts, res) + if err != nil { + return nil, err + } + + analytics := make(map[string]int) + + for _, ana := range res.Rows { + if len(ana) != 2 { + continue + } + path := ana[0] + pv, err := strconv.Atoi(ana[1]) + if err != nil { + logger.Warn(err) + continue + } + analytics[path] = pv + } + + return analytics, nil +} + +type analyticsResult struct { + ColumnHeaders []struct { + ColumnType string `json:"columnType"` + DataType string `json:"dataType"` + Name string `json:"name"` + } `json:"columnHeaders"` + ContainsSampledData bool `json:"containsSampledData"` + ID string `json:"id"` + ItemsPerPage int64 `json:"itemsPerPage"` + Kind string `json:"kind"` + ProfileInfo struct { + AccountID string `json:"accountId"` + InternalWebPropertyID string `json:"internalWebPropertyId"` + ProfileID string `json:"profileId"` + ProfileName string `json:"profileName"` + TableID string `json:"tableId"` + WebPropertyID string `json:"webPropertyId"` + } `json:"profileInfo"` + Query struct { + Dimensions string `json:"dimensions"` + End_date string `json:"end-date"` + Ids string `json:"ids"` + Max_results int64 `json:"max-results"` + Metrics []string `json:"metrics"` + Start_date string `json:"start-date"` + Start_index int64 `json:"start-index"` + } `json:"query"` + Rows [][]string `json:"rows"` + SelfLink string `json:"selfLink"` + TotalResults int64 `json:"totalResults"` + TotalsForAllResults struct { + Ga_pageviews string `json:"ga:pageviews"` + } `json:"totalsForAllResults"` +} diff --git a/dao/dao.go b/dao/dao.go index 99990c4..36150c6 100644 --- a/dao/dao.go +++ b/dao/dao.go @@ -11,7 +11,7 @@ type DaoContainer interface { DelResult(id interface{}) GetResults() ([]*Result, error) GetResultById(id uint64) (*Result, error) - Search(q string, limit, start int) (int, float64, []Result) + Search(q string, limit, start int, sort string, asc bool) (int, float64, []Result) Debug(is_debug bool) Close() error } diff --git a/dao/peanut_dao.go b/dao/peanut_dao.go index 3cc2de8..f7a39f7 100644 --- a/dao/peanut_dao.go +++ b/dao/peanut_dao.go @@ -50,6 +50,7 @@ func (this *PeanutContainer) toDucument(p *Result) *index.Document { document.Tags = strings.Split(p.Tags, " ") document.Link = p.Link document.Figure = p.Figure + document.PV = p.PV return document } @@ -66,6 +67,7 @@ func (this *PeanutContainer) toResults(ps []*index.Document) []Result { Tags: strings.Join(p.Tags, " "), Link: p.Link, Figure: p.Figure, + PV: p.PV, } res = append(res, rr) } @@ -84,11 +86,12 @@ func (this *PeanutContainer) GetResultById(id uint64) (*Result, error) { return nil, nil } -func (this *PeanutContainer) Search(q string, limit, start int) (int, float64, []Result) { +func (this *PeanutContainer) Search(q string, limit, start int, sort string, asc bool) (int, float64, []Result) { cnt, res, err := this.ind.Search(&index.Param{ Query: q, Offset: start, Size: limit, + Sort: index.Sorter{sort, asc}, }) if err != nil { diff --git a/go.mod b/go.mod index a6fe28f..8942ba1 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,7 @@ module github.com/mnhkahn/maodou require ( github.com/PuerkitoBio/goquery v1.4.1 github.com/andybalholm/cascadia v1.0.0 // indirect - github.com/kr/pty v1.1.2 // indirect - github.com/mnhkahn/gogogo v1.0.1 - github.com/mnhkahn/peanut v1.0.2 + github.com/mnhkahn/gogogo v1.0.2 + github.com/mnhkahn/peanut v1.0.3 golang.org/x/net v0.0.0-20180826012351-8a410e7b638d - golang.org/x/sys v0.0.0-20180830151530-49385e6e1522 // indirect ) diff --git a/go.sum b/go.sum index 9064f2b..b13a6be 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,7 @@ github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9Pq github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4= github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/huichen/sego v0.0.0-20180617034105-3f3c8a8cfacc h1:3LXYtoxQGFSjIL5ZJAn4PceSpwRohuTKYL1W4kJ7G8g= github.com/huichen/sego v0.0.0-20180617034105-3f3c8a8cfacc/go.mod h1:+/Bm7uk1bnJJMi9l6P88FgHeGtscOQiYbxW1j+BmgBY= github.com/issue9/assert v0.0.0-20180725152606-9e19636c7256/go.mod h1:KLwR3U/5rbCxqwAnV3aCr+dz07aoIyIfk2lefIVr2BA= @@ -21,6 +22,8 @@ github.com/mnhkahn/gods v1.0.1 h1:Hiz/zIpgPfOCzt0rCamnZ+pwAhrYCZGDsuQnT0tzArk= github.com/mnhkahn/gods v1.0.1/go.mod h1:tPMQcwh/o/HfDrKK4MgvJzjJ4AP5jInErWwAlo4gvnA= github.com/mnhkahn/gogogo v1.0.1 h1:nQqxYUvAf1uxTbrExD7+3KuBMpFa4nTaDk2h9GB0Wp8= github.com/mnhkahn/gogogo v1.0.1/go.mod h1:2H7pELixbPqzXoX5mOT8co8aTkhZpl5gVb4CcZDJO54= +github.com/mnhkahn/gogogo v1.0.2 h1:uFhGfOz5y4hsRztE7HA3yQW58lQ6ATDl53ezC5NHq1M= +github.com/mnhkahn/gogogo v1.0.2/go.mod h1:KM3JDQ9Xx9atIfBnKhEErETA/J1uMsFFK1ld4nwuBig= github.com/mnhkahn/peanut v0.0.0-20180828055434-ec108b6c00a8 h1:c+s8E7JbEkhN/mTkSS7cGqAr0TiJXUpEWz5M1r0OmtY= github.com/mnhkahn/peanut v0.0.0-20180830083834-4b0ce468b4f3 h1:MsxWo97ApFYCFczGfthp/9vGTKxOW+OUYrzeuti8Ieg= github.com/mnhkahn/peanut v0.0.0-20180830083834-4b0ce468b4f3/go.mod h1:z/P9SGuhOq6nOGkrGXYZR7UDUPHUSpAFTSmfA+tVKoA= @@ -30,6 +33,8 @@ github.com/mnhkahn/peanut v1.0.1 h1:qRNT4lRsWPqxi26in00YYE27r3f9cwSDDW/JK+F/JxY= github.com/mnhkahn/peanut v1.0.1/go.mod h1:z/P9SGuhOq6nOGkrGXYZR7UDUPHUSpAFTSmfA+tVKoA= github.com/mnhkahn/peanut v1.0.2 h1:FUXEpG+8zze69y+Tp1w41XM84nZXUfrMM1vrmwAhcsE= github.com/mnhkahn/peanut v1.0.2/go.mod h1:z/P9SGuhOq6nOGkrGXYZR7UDUPHUSpAFTSmfA+tVKoA= +github.com/mnhkahn/peanut v1.0.3 h1:n/77qc19c2JT2ZSO7vNZowfs9nq6oEE4HVaF9/o2ty0= +github.com/mnhkahn/peanut v1.0.3/go.mod h1:gvR73OkzzLVYD3Cu3CPBIocG16RJOAHz+PGhsl+kebM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sasbury/mini v0.0.0-20161224193750-64bd399395db/go.mod h1:yOzd9gzygiw8QvoQeezToFEojl93FdFfDD2d64ZV6u8= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= @@ -40,8 +45,11 @@ github.com/willf/bitset v1.1.9/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPyS golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d h1:g9qWBGx4puODJTMVyoPrpoxPFgVGd+z1DZwjfRu4d0I= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180828065106-d99a578cf41b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180831094639-fa5fdf94c789/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/natefinch/lumberjack.v2 v2.0.0-20170531160350-a96e63847dc3 h1:AFxeG48hTWHhDTQDk/m2gorfVHUEa9vo3tp3D7TzwjI= diff --git a/models/result.go b/models/result.go index 7392e53..62f3b63 100644 --- a/models/result.go +++ b/models/result.go @@ -15,4 +15,5 @@ type Result struct { Link string `orm:"size(100)" json:"link"` Source string `orm:"size(45)" json:"source"` ParseDate time.Time `orm:"auto_now_add;type(datetime)" json:"parse_date"` + PV int `json:"pv"` }