Skip to content

Commit

Permalink
Merge pull request #58 from mnhkahn/feature_pv
Browse files Browse the repository at this point in the history
feat(search): add sort support, sort by pv & pub date.
  • Loading branch information
mnhkahn authored Sep 2, 2018
2 parents ccc6481 + 5885529 commit 8aac340
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 6 deletions.
87 changes: 87 additions & 0 deletions analytics/google_analytics.go
Original file line number Diff line number Diff line change
@@ -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 [email protected].
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"`
}
2 changes: 1 addition & 1 deletion dao/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 4 additions & 1 deletion dao/peanut_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
}
Expand All @@ -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 {
Expand Down
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand All @@ -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=
Expand All @@ -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=
Expand Down
1 change: 1 addition & 0 deletions models/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit 8aac340

Please sign in to comment.