/
project.go
537 lines (446 loc) · 13.5 KB
/
project.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// Copyright 2016 The StudyGolang Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// http://studygolang.com
// Author:polaris polaris@studygolang.com
package logic
import (
"errors"
"math/rand"
"net/url"
"strconv"
"strings"
"time"
. "github.com/studygolang/studygolang/db"
"github.com/studygolang/studygolang/internal/model"
"github.com/PuerkitoBio/goquery"
"github.com/lunny/html2md"
"github.com/polaris1119/config"
"github.com/polaris1119/logger"
"github.com/polaris1119/set"
"golang.org/x/net/context"
)
type ProjectLogic struct{}
var DefaultProject = ProjectLogic{}
func (self ProjectLogic) Publish(ctx context.Context, user *model.Me, form url.Values) (err error) {
objLog := GetLogger(ctx)
id := form.Get("id")
isModify := id != ""
project := &model.OpenProject{}
if isModify {
_, err = MasterDB.ID(id).Get(project)
if err != nil {
objLog.Errorln("Publish Project find error:", err)
return
}
if !CanEdit(user, project) {
err = NotModifyAuthorityErr
return
}
err = schemaDecoder.Decode(project, form)
if err != nil {
objLog.Errorln("Publish Project schema decode error:", err)
return
}
} else {
err = schemaDecoder.Decode(project, form)
if err != nil {
objLog.Errorln("Publish Project schema decode error:", err)
return
}
project.Username = user.Username
}
if project.Uri == "" {
project.Uri = strings.Replace(project.Name, " ", "-", -1)
}
project.Uri = strings.ToLower(project.Uri)
if !isModify && self.UriExists(ctx, form.Get("uri")) {
err = errors.New("项目已存在")
return
}
github := "github.com"
pos := strings.Index(project.Src, github)
if pos != -1 {
project.Repo = project.Src[pos+len(github)+1:]
}
var affected int64
if !isModify {
affected, err = MasterDB.Insert(project)
} else {
affected, err = MasterDB.ID(id).Update(project)
}
if err != nil {
objLog.Errorln("Publish Project error:", err)
return
}
if affected == 0 {
return
}
if isModify {
go modifyObservable.NotifyObservers(user.Uid, model.TypeProject, project.Id)
} else {
go publishObservable.NotifyObservers(user.Uid, model.TypeProject, project.Id)
}
return
}
// UriExists 通过 uri 是否存在 project
func (ProjectLogic) UriExists(ctx context.Context, uri string) bool {
total, err := MasterDB.Where("uri=?", uri).Count(new(model.OpenProject))
if err != nil || total == 0 {
return false
}
return true
}
// Total 开源项目总数
func (ProjectLogic) Total() int64 {
total, err := MasterDB.Count(new(model.OpenProject))
if err != nil {
logger.Errorln("ProjectLogic Total error:", err)
}
return total
}
// FindBy 获取开源项目列表(分页)
func (ProjectLogic) FindBy(ctx context.Context, limit int, lastIds ...int) []*model.OpenProject {
objLog := GetLogger(ctx)
dbSession := MasterDB.Where("status IN(?,?)", model.ProjectStatusNew, model.ProjectStatusOnline)
if len(lastIds) > 0 && lastIds[0] > 0 {
dbSession.And("id<?", lastIds[0])
}
projectList := make([]*model.OpenProject, 0)
err := dbSession.OrderBy("id DESC").Limit(limit).Find(&projectList)
if err != nil {
objLog.Errorln("ProjectLogic FindBy Error:", err)
return nil
}
return projectList
}
// FindByIds 获取多个项目详细信息
func (ProjectLogic) FindByIds(ids []int) []*model.OpenProject {
if len(ids) == 0 {
return nil
}
projects := make([]*model.OpenProject, 0)
err := MasterDB.In("id", ids).Find(&projects)
if err != nil {
logger.Errorln("ProjectLogic FindByIds error:", err)
return nil
}
return projects
}
// findByIds 获取多个项目详细信息 包内使用
func (ProjectLogic) findByIds(ids []int) map[int]*model.OpenProject {
if len(ids) == 0 {
return nil
}
projects := make(map[int]*model.OpenProject)
err := MasterDB.In("id", ids).Find(&projects)
if err != nil {
logger.Errorln("ProjectLogic FindByIds error:", err)
return nil
}
return projects
}
// FindOne 获取单个项目
func (ProjectLogic) FindOne(ctx context.Context, val interface{}) *model.OpenProject {
objLog := GetLogger(ctx)
field := "id"
_, ok := val.(int)
if !ok {
val := val.(string)
if _, err := strconv.Atoi(val); err != nil {
field = "uri"
}
}
project := &model.OpenProject{}
_, err := MasterDB.Where(field+"=? AND status IN(?,?)", val, model.ProjectStatusNew, model.ProjectStatusOnline).Get(project)
if err != nil {
objLog.Errorln("project service FindProject error:", err)
return nil
}
project.User = DefaultUser.FindOne(ctx, "username", project.Username)
return project
}
// FindRecent 获得某个用户最近发布的开源项目
func (ProjectLogic) FindRecent(ctx context.Context, username string) []*model.OpenProject {
projectList := make([]*model.OpenProject, 0)
err := MasterDB.Where("username=?", username).Limit(5).OrderBy("id DESC").Find(&projectList)
if err != nil {
logger.Errorln("project logic FindRecent error:", err)
return nil
}
return projectList
}
// FindAll 支持多页翻看
func (self ProjectLogic) FindAll(ctx context.Context, paginator *Paginator, orderBy string, querystring string, args ...interface{}) []*model.OpenProject {
objLog := GetLogger(ctx)
projects := make([]*model.OpenProject, 0)
session := MasterDB.OrderBy(orderBy)
if querystring != "" {
session.Where(querystring, args...)
}
err := session.Limit(paginator.PerPage(), paginator.Offset()).Find(&projects)
if err != nil {
objLog.Errorln("ProjectLogic FindAll error:", err)
return nil
}
self.fillUser(projects)
return projects
}
func (ProjectLogic) Count(ctx context.Context, querystring string, args ...interface{}) int64 {
objLog := GetLogger(ctx)
var (
total int64
err error
)
if querystring == "" {
total, err = MasterDB.Count(new(model.OpenProject))
} else {
total, err = MasterDB.Where(querystring, args...).Count(new(model.OpenProject))
}
if err != nil {
objLog.Errorln("ProjectLogic Count error:", err)
}
return total
}
func (ProjectLogic) fillUser(projects []*model.OpenProject) {
usernameSet := set.New(set.NonThreadSafe)
uidSet := set.New(set.NonThreadSafe)
for _, project := range projects {
usernameSet.Add(project.Username)
if project.Lastreplyuid != 0 {
uidSet.Add(project.Lastreplyuid)
}
}
if !usernameSet.IsEmpty() {
userMap := DefaultUser.FindUserInfos(nil, set.StringSlice(usernameSet))
for _, project := range projects {
for _, user := range userMap {
if project.Username == user.Username {
project.User = user
break
}
}
}
}
if !uidSet.IsEmpty() {
replyUserMap := DefaultUser.FindUserInfos(nil, set.IntSlice(uidSet))
for _, project := range projects {
if project.Lastreplyuid == 0 {
continue
}
project.LastReplyUser = replyUserMap[project.Lastreplyuid]
}
}
}
// getOwner 通过objid获得 project 的所有者
func (ProjectLogic) getOwner(ctx context.Context, id int) int {
project := &model.OpenProject{}
_, err := MasterDB.ID(id).Get(project)
if err != nil {
logger.Errorln("project logic getOwner Error:", err)
return 0
}
user := DefaultUser.FindOne(ctx, "username", project.Username)
return user.Uid
}
// ParseProjectList 解析其他网站的开源项目
func (self ProjectLogic) ParseProjectList(pUrl string) error {
pUrl = strings.TrimSpace(pUrl)
if !strings.HasPrefix(pUrl, "http") {
pUrl = "http://" + pUrl
}
var (
doc *goquery.Document
err error
)
if doc, err = goquery.NewDocument(pUrl); err != nil {
logger.Errorln("goquery opensource project newdocument error:", err)
return err
}
// 最后面的先入库处理
projectsSelection := doc.Find("#projectList .list-container").Children()
for i := projectsSelection.Length() - 1; i >= 0; i-- {
contentSelection := goquery.NewDocumentFromNode(projectsSelection.Get(i)).Selection
projectUrl, ok := contentSelection.Find(".content .header a").First().Attr("href")
if !ok || projectUrl == "" {
logger.Errorln("project url is empty")
continue
}
go func(projectUrl string) {
err := self.ParseOneProject(projectUrl)
if err != nil {
logger.Errorln(err)
}
}(projectUrl)
}
return err
}
const OsChinaDomain = "https://www.oschina.net"
// ProjectLogoPrefix 开源项目 logo 前缀
const ProjectLogoPrefix = "plogo"
var PresetUsernames = config.ConfigFile.MustValueArray("crawl", "preset_users", ",")
// ParseOneProject 处理单个 project
func (ProjectLogic) ParseOneProject(projectUrl string) error {
if !strings.HasPrefix(projectUrl, "http") {
projectUrl = OsChinaDomain + projectUrl
}
var (
doc *goquery.Document
err error
)
// 加上 ?fromerr=xfwefs,否则页面有 js 重定向
if doc, err = goquery.NewDocument(projectUrl + "?fromerr=xfwefs"); err != nil {
return errors.New("goquery fetch " + projectUrl + " error:" + err.Error())
}
// 标题
category := strings.TrimSpace(doc.Find(".detail-header h1 .project-title").Text())
name := strings.TrimSpace(doc.Find(".detail-header h1 .project-name").Text())
if category == "" && name == "" {
return errors.New("projectUrl:" + projectUrl + " category and name are empty")
}
tmpIndex := strings.LastIndex(category, name)
if tmpIndex != -1 {
category = category[:tmpIndex]
}
// uri
uri := projectUrl[strings.LastIndex(projectUrl, "/")+1:]
project := &model.OpenProject{}
_, err = MasterDB.Where("uri=?", uri).Get(project)
// 已经存在
if project.Id != 0 {
logger.Infoln("url", projectUrl, "has exists!")
return nil
}
logoSelection := doc.Find(".detail-header .logo-wrap img")
if logoSelection.AttrOr("alt", "") != "" {
project.Logo = logoSelection.AttrOr("src", "")
if !strings.HasPrefix(project.Logo, "http") {
// project.Logo = OsChinaDomain + project.Logo
// 默认图
project.Logo = ""
} else {
project.Logo, err = DefaultUploader.TransferUrl(nil, project.Logo, ProjectLogoPrefix)
if err != nil {
logger.Errorln("project logo upload error:", err)
}
}
}
// 获取项目相关链接
doc.Find(".related-links a").Each(func(i int, aSelection *goquery.Selection) {
uri := aSelection.AttrOr("href", "")
switch aSelection.Text() {
case "软件首页":
project.Home = uri
case "软件文档":
project.Doc = uri
case "官方下载":
project.Download = uri
}
})
doc.Find(".info-list .box .info-item").Each(func(i int, liSelection *goquery.Selection) {
aSelection := liSelection.Find("span")
txt := strings.TrimSpace(aSelection.Text())
if i == 0 {
project.Licence = txt
if txt == "未知" {
project.Licence = "其他"
}
} else if i == 1 {
txt = liSelection.Find("span a:first-child").Text()
project.Lang = txt
} else if i == 2 {
project.Os = txt
}
})
project.Name = name
project.Category = strings.TrimSpace(category)
project.Uri = uri
project.Src = project.Download
if strings.HasPrefix(project.Src, "https://github.com/") {
project.Repo = project.Src[len("https://github.com/"):]
} else if strings.HasPrefix(project.Src, "https://gitee.com/") {
project.Repo = project.Src[len("https://gitee.com/"):]
} else {
return nil
}
pos := strings.Index(project.Repo, "/")
if pos > -1 {
project.Author = project.Repo[:pos]
} else {
project.Author = "网友"
}
if project.Doc == "" {
// TODO:暂时认为一定是 Go 语言
project.Doc = "https://godoc.org/" + project.Src[8:]
}
desc := ""
doc.Find(".project-body").Children().Each(func(i int, domSelection *goquery.Selection) {
if domSelection.HasClass("ad-wrap") {
return
}
doc.FindSelection(domSelection).WrapHtml(`<div id="tmp` + strconv.Itoa(i) + `"></div>`)
domHtml, _ := doc.Find("#tmp" + strconv.Itoa(i)).Html()
if domSelection.Is("pre") {
desc += domHtml + "\n\n"
} else {
desc += html2md.Convert(domHtml) + "\n\n"
}
})
project.Desc = strings.TrimSpace(desc)
project.Username = PresetUsernames[rand.Intn(len(PresetUsernames))]
project.Status = model.ProjectStatusOnline
project.Ctime = model.OftenTime(time.Now())
_, err = MasterDB.Insert(project)
if err != nil {
return errors.New("insert into open project error:" + err.Error())
}
return nil
}
// 项目评论
type ProjectComment struct{}
// 更新该项目的评论信息
// cid:评论id;objid:被评论对象id;uid:评论者;cmttime:评论时间
func (self ProjectComment) UpdateComment(cid, objid, uid int, cmttime time.Time) {
// 更新评论数(TODO:暂时每次都更新表)
_, err := MasterDB.Table(new(model.OpenProject)).ID(objid).Incr("cmtnum", 1).Update(map[string]interface{}{
"lastreplyuid": uid,
"lastreplytime": cmttime,
})
if err != nil {
logger.Errorln("更新项目评论数失败:", err)
return
}
}
func (self ProjectComment) String() string {
return "project"
}
// 实现 CommentObjecter 接口
func (self ProjectComment) SetObjinfo(ids []int, commentMap map[int][]*model.Comment) {
projects := DefaultProject.FindByIds(ids)
if len(projects) == 0 {
return
}
for _, project := range projects {
objinfo := make(map[string]interface{})
objinfo["title"] = project.Category + project.Name
objinfo["uri"] = model.PathUrlMap[model.TypeProject]
objinfo["type_name"] = model.TypeNameMap[model.TypeProject]
for _, comment := range commentMap[project.Id] {
comment.Objinfo = objinfo
}
}
}
// 项目喜欢
type ProjectLike struct{}
// 更新该项目的喜欢数
// objid:被喜欢对象id;num: 喜欢数(负数表示取消喜欢)
func (self ProjectLike) UpdateLike(objid, num int) {
// 更新喜欢数(TODO:暂时每次都更新表)
_, err := MasterDB.ID(objid).Incr("likenum", num).Update(new(model.OpenProject))
if err != nil {
logger.Errorln("更新项目喜欢数失败:", err)
}
}
func (self ProjectLike) String() string {
return "project"
}