-
Notifications
You must be signed in to change notification settings - Fork 0
/
recommend.go
101 lines (85 loc) · 2.26 KB
/
recommend.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"math/rand"
"strconv"
"strings"
"github.com/Terryhung/infohub_rest/mongo_lib"
"github.com/Terryhung/infohub_rest/news"
"github.com/Terryhung/infohub_rest/redis_lib"
"github.com/Terryhung/infohub_rest/utils"
"github.com/go-redis/redis"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type InfohubUser struct {
Gaid string `json:"gaid" bson:"gaid"`
Cand []int `json:"candidates" bson:"candidates"`
Method string `json:"method" bson:"method"`
Top []int `json:"top" bson:"top"`
}
type Setting struct {
DB string
C string
Method string
}
func Recommendar(gaid string, lang string, cty string, session *mgo.Session, r_client *redis.Client, r_status bool, s Setting) (bool, []news.News) {
db := s.DB
col := s.C
user := InfohubUser{}
news_results := []news.News{}
status, _ := mongo_lib.FindOne(db, col, session, bson.M{"gaid": gaid}, &user)
if !status {
// Check general user
g_gaids := []string{"general-user", cty}
g_gaid := strings.Join(g_gaids, "-")
// Try
status, _ = mongo_lib.FindOne(db, col, session, bson.M{"gaid": g_gaid}, &user)
if !status {
return false, news_results
}
}
cands := user.Cand
// Candidate
if len(user.Cand) > 10 {
cands = user.Cand[:10]
}
if len(cands) == 0 {
cands = user.Top
}
// Query News
news_db := "analysis"
news_col := "news_meta_baas"
for _, c := range cands {
cond := bson.M{"hier_category": c, "language": lang, "category": bson.M{"$ne": "girls"}}
var c_news []news.News
// Concate Key for cache
keys := []string{lang, strconv.Itoa(c)}
key := strings.Join(keys, "-")
need_cache := false
// Check Cache
if r_status {
// Check data in redis
redis_lib.CheckExists(r_client, key, &c_news)
// No Data: check data in Mongo
if len(c_news) == 0 {
mongo_lib.Find(news_db, news_col, session, cond, &c_news)
need_cache = true
}
} else {
mongo_lib.Find(news_db, news_col, session, cond, &c_news)
}
// Random Pick
if len(c_news) > 0 {
random_index := rand.Intn(len(c_news))
n := c_news[random_index]
n.By = s.Method
n.Id = utils.SpecialID(n.Link)
news_results = append(news_results, n)
}
// Need Cache?
if need_cache && r_status {
redis_lib.SetValue(r_client, key, c_news, 3600)
}
}
return status, news_results
}