Skip to content

Commit d51c8e6

Browse files
authored
Merge pull request #302 from compscidr/jason/go-scholar-update
WiP: go scholar library to automatically pull in google scholar entries
2 parents 00132e1 + 9cdfbe7 commit d51c8e6

File tree

9 files changed

+170
-311
lines changed

9 files changed

+170
-311
lines changed

.github/workflows/push.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ jobs:
1212
runs-on: ubuntu-latest
1313
steps:
1414

15-
- name: Set up Go 1.20.0
15+
- name: Set up Go 1.22.2
1616
uses: actions/setup-go@v5
1717
with:
18-
go-version: 1.20.0
18+
go-version: 1.22.2
1919
id: go
2020

2121
- name: Check out code into the Go module directory

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
articles.json
2+
profiles.json
3+
14
# Binaries for programs and plugins
25
*.exe
36
*.exe~

admin/admin_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package admin_test
33
import (
44
"bytes"
55
"encoding/json"
6+
scholar "github.com/compscidr/scholar"
67
"goblog/admin"
78
"goblog/auth"
89
"goblog/blog"
@@ -44,7 +45,8 @@ func TestCreatePost(t *testing.T) {
4445
db.AutoMigrate(&auth.BlogUser{})
4546
db.AutoMigrate(&blog.Post{})
4647
a := &Auth{}
47-
b := blog.New(db, a, "test")
48+
sch := scholar.New("profiles.json", "articles.json")
49+
b := blog.New(db, a, "test", sch)
4850
ad := admin.New(db, a, b, "test")
4951

5052
router := gin.Default()

blog/blog.go

Lines changed: 79 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package blog
22

33
import (
44
"errors"
5+
scholar "github.com/compscidr/scholar"
56
"goblog/auth"
67
"log"
78
"net/http"
@@ -25,11 +26,12 @@ type Blog struct {
2526
db *gorm.DB
2627
auth auth.IAuth
2728
Version string
29+
scholar *scholar.Scholar
2830
}
2931

3032
// New constructs an Admin API
31-
func New(db *gorm.DB, auth auth.IAuth, version string) Blog {
32-
api := Blog{db, auth, version}
33+
func New(db *gorm.DB, auth auth.IAuth, version string, scholar *scholar.Scholar) Blog {
34+
api := Blog{db, auth, version, scholar}
3335
return api
3436
}
3537

@@ -172,20 +174,20 @@ func (b Blog) NoRoute(c *gin.Context) {
172174
if err == nil && post != nil {
173175
if b.auth.IsAdmin(c) {
174176
c.HTML(http.StatusOK, "post-admin.html", gin.H{
175-
"logged_in": b.auth.IsLoggedIn(c),
176-
"is_admin": b.auth.IsAdmin(c),
177-
"post": post,
178-
"version": b.Version,
179-
"recent": b.GetLatest(),
177+
"logged_in": b.auth.IsLoggedIn(c),
178+
"is_admin": b.auth.IsAdmin(c),
179+
"post": post,
180+
"version": b.Version,
181+
"recent": b.GetLatest(),
180182
"admin_page": false,
181183
})
182184
} else {
183185
c.HTML(http.StatusOK, "post.html", gin.H{
184-
"logged_in": b.auth.IsLoggedIn(c),
185-
"is_admin": b.auth.IsAdmin(c),
186-
"post": post,
187-
"version": b.Version,
188-
"recent": b.GetLatest(),
186+
"logged_in": b.auth.IsLoggedIn(c),
187+
"is_admin": b.auth.IsAdmin(c),
188+
"post": post,
189+
"version": b.Version,
190+
"recent": b.GetLatest(),
189191
"admin_page": false,
190192
})
191193
}
@@ -205,7 +207,7 @@ func (b Blog) NoRoute(c *gin.Context) {
205207
"description": "The page at '" + c.Request.URL.String() + "' was not found",
206208
"version": b.Version,
207209
"recent": b.GetLatest(),
208-
"admin_page": false,
210+
"admin_page": false,
209211
})
210212
}
211213

@@ -214,24 +216,24 @@ func (b Blog) NoRoute(c *gin.Context) {
214216
// need to modify this function
215217
func (b Blog) Home(c *gin.Context) {
216218
c.HTML(http.StatusOK, "home.html", gin.H{
217-
"logged_in": b.auth.IsLoggedIn(c),
218-
"is_admin": b.auth.IsAdmin(c),
219-
"version": b.Version,
220-
"title": "Software Engineer",
221-
"recent": b.GetLatest(),
219+
"logged_in": b.auth.IsLoggedIn(c),
220+
"is_admin": b.auth.IsAdmin(c),
221+
"version": b.Version,
222+
"title": "Software Engineer",
223+
"recent": b.GetLatest(),
222224
"admin_page": false,
223225
})
224226
}
225227

226228
// Posts is the index page for blog posts
227229
func (b Blog) Posts(c *gin.Context) {
228230
c.HTML(http.StatusOK, "posts.html", gin.H{
229-
"logged_in": b.auth.IsLoggedIn(c),
230-
"is_admin": b.auth.IsAdmin(c),
231-
"posts": b.GetPosts(false),
232-
"version": b.Version,
233-
"title": "Posts",
234-
"recent": b.GetLatest(),
231+
"logged_in": b.auth.IsLoggedIn(c),
232+
"is_admin": b.auth.IsAdmin(c),
233+
"posts": b.GetPosts(false),
234+
"version": b.Version,
235+
"title": "Posts",
236+
"recent": b.GetLatest(),
235237
"admin_page": false,
236238
})
237239
}
@@ -246,15 +248,15 @@ func (b Blog) Post(c *gin.Context) {
246248
"version": b.Version,
247249
"title": "Post Not Found",
248250
"recent": b.GetLatest(),
249-
"admin_page": false,
251+
"admin_page": false,
250252
})
251253
} else {
252254
c.HTML(http.StatusOK, "post.html", gin.H{
253-
"logged_in": b.auth.IsLoggedIn(c),
254-
"is_admin": b.auth.IsAdmin(c),
255-
"post": post,
256-
"version": b.Version,
257-
"recent": b.GetLatest(),
255+
"logged_in": b.auth.IsLoggedIn(c),
256+
"is_admin": b.auth.IsAdmin(c),
257+
"post": post,
258+
"version": b.Version,
259+
"recent": b.GetLatest(),
258260
"admin_page": false,
259261
})
260262
//if b.auth.IsAdmin(c) {
@@ -286,17 +288,17 @@ func (b Blog) Tag(c *gin.Context) {
286288
"version": b.Version,
287289
"title": "Tag '" + tag + "' Not Found",
288290
"recent": b.GetLatest(),
289-
"admin_page": false,
291+
"admin_page": false,
290292
})
291293
} else {
292294
c.HTML(http.StatusOK, "tag.html", gin.H{
293-
"logged_in": b.auth.IsLoggedIn(c),
294-
"is_admin": b.auth.IsAdmin(c),
295-
"posts": posts,
296-
"tag": tag,
297-
"version": b.Version,
298-
"title": "Posts with Tag '" + tag + "'",
299-
"recent": b.GetLatest(),
295+
"logged_in": b.auth.IsLoggedIn(c),
296+
"is_admin": b.auth.IsAdmin(c),
297+
"posts": posts,
298+
"tag": tag,
299+
"version": b.Version,
300+
"title": "Posts with Tag '" + tag + "'",
301+
"recent": b.GetLatest(),
300302
"admin_page": false,
301303
})
302304
}
@@ -305,58 +307,61 @@ func (b Blog) Tag(c *gin.Context) {
305307
// Tags is the index page for all Tags
306308
func (b Blog) Tags(c *gin.Context) {
307309
c.HTML(http.StatusOK, "tags.html", gin.H{
308-
"version": b.Version,
309-
"title": "Tags",
310-
"tags": b.getTags(),
311-
"recent": b.GetLatest(),
310+
"version": b.Version,
311+
"title": "Tags",
312+
"tags": b.getTags(),
313+
"recent": b.GetLatest(),
312314
"admin_page": false,
313315
})
314316
}
315317

316318
// Speaking is the index page for presentations
317319
func (b Blog) Speaking(c *gin.Context) {
318320
c.HTML(http.StatusOK, "presentations.html", gin.H{
319-
"logged_in": b.auth.IsLoggedIn(c),
320-
"is_admin": b.auth.IsAdmin(c),
321-
"version": b.Version,
322-
"title": "Presentations and Speaking",
323-
"recent": b.GetLatest(),
321+
"logged_in": b.auth.IsLoggedIn(c),
322+
"is_admin": b.auth.IsAdmin(c),
323+
"version": b.Version,
324+
"title": "Presentations and Speaking",
325+
"recent": b.GetLatest(),
324326
"admin_page": false,
325327
})
326328
}
327329

328330
// Speaking is the index page for research publications
329331
func (b Blog) Research(c *gin.Context) {
332+
articles := b.scholar.QueryProfileWithMemoryCache("SbUmSEAAAAAJ", 50)
333+
b.scholar.SaveCache("profiles.json", "articles.json")
330334
c.HTML(http.StatusOK, "research.html", gin.H{
331-
"logged_in": b.auth.IsLoggedIn(c),
332-
"is_admin": b.auth.IsAdmin(c),
333-
"version": b.Version,
334-
"title": "Research Publications",
335-
"recent": b.GetLatest(),
335+
"logged_in": b.auth.IsLoggedIn(c),
336+
"is_admin": b.auth.IsAdmin(c),
337+
"version": b.Version,
338+
"title": "Research Publications",
339+
"recent": b.GetLatest(),
340+
"articles": articles,
336341
"admin_page": false,
337342
})
338343
}
339344

340345
// Projects is the index page for projects / code
341346
func (b Blog) Projects(c *gin.Context) {
342347
c.HTML(http.StatusOK, "projects.html", gin.H{
343-
"logged_in": b.auth.IsLoggedIn(c),
344-
"is_admin": b.auth.IsAdmin(c),
345-
"version": b.Version,
346-
"title": "Projects",
347-
"recent": b.GetLatest(),
348+
"logged_in": b.auth.IsLoggedIn(c),
349+
"is_admin": b.auth.IsAdmin(c),
350+
"version": b.Version,
351+
"title": "Projects",
352+
"recent": b.GetLatest(),
348353
"admin_page": false,
349354
})
350355
}
351356

352357
// About is the about page
353358
func (b Blog) About(c *gin.Context) {
354359
c.HTML(http.StatusOK, "about.html", gin.H{
355-
"logged_in": b.auth.IsLoggedIn(c),
356-
"is_admin": b.auth.IsAdmin(c),
357-
"version": b.Version,
358-
"title": "About",
359-
"recent": b.GetLatest(),
360+
"logged_in": b.auth.IsLoggedIn(c),
361+
"is_admin": b.auth.IsAdmin(c),
362+
"version": b.Version,
363+
"title": "About",
364+
"recent": b.GetLatest(),
360365
"admin_page": false,
361366
})
362367
}
@@ -371,7 +376,7 @@ func (b Blog) Archives(c *gin.Context) {
371376
"byYear": b.getArchivesByYear(),
372377
"byYearMonth": b.getArchivesByYearMonth(),
373378
"recent": b.GetLatest(),
374-
"admin_page": false,
379+
"admin_page": false,
375380
})
376381
}
377382

@@ -413,11 +418,11 @@ func (b Blog) Login(c *gin.Context) {
413418
if err != nil {
414419
//todo: handle better - perhaps return error to browser
415420
c.HTML(http.StatusInternalServerError, "Error loading .env file: "+err.Error(), gin.H{
416-
"logged_in": b.auth.IsLoggedIn(c),
417-
"is_admin": b.auth.IsAdmin(c),
418-
"version": b.Version,
419-
"title": "Login Configuration Error",
420-
"recent": b.GetLatest(),
421+
"logged_in": b.auth.IsLoggedIn(c),
422+
"is_admin": b.auth.IsAdmin(c),
423+
"version": b.Version,
424+
"title": "Login Configuration Error",
425+
"recent": b.GetLatest(),
421426
"admin_page": false,
422427
})
423428
return
@@ -426,12 +431,12 @@ func (b Blog) Login(c *gin.Context) {
426431

427432
clientID := os.Getenv("client_id")
428433
c.HTML(http.StatusOK, "login.html", gin.H{
429-
"logged_in": b.auth.IsLoggedIn(c),
430-
"is_admin": b.auth.IsAdmin(c),
431-
"client_id": clientID,
432-
"version": b.Version,
433-
"title": "Login",
434-
"recent": b.GetLatest(),
434+
"logged_in": b.auth.IsLoggedIn(c),
435+
"is_admin": b.auth.IsAdmin(c),
436+
"client_id": clientID,
437+
"version": b.Version,
438+
"title": "Login",
439+
"recent": b.GetLatest(),
435440
"admin_page": false,
436441
})
437442
}

blog/blog_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package blog_test
33
import (
44
"bytes"
55
"encoding/json"
6+
scholar "github.com/compscidr/scholar"
67
"goblog/admin"
78
"goblog/auth"
89
"goblog/blog"
@@ -42,7 +43,8 @@ func TestBlogWorkflow(t *testing.T) {
4243
db.AutoMigrate(&blog.Post{})
4344
db.AutoMigrate(&blog.Tag{})
4445
a := &Auth{}
45-
b := blog.New(db, a, "test")
46+
sch := scholar.New("profiles.json", "articles.json")
47+
b := blog.New(db, a, "test", sch)
4648
admin := admin.New(db, a, b, "test")
4749

4850
router := gin.Default()

go.mod

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
module goblog
22

3-
go 1.19
3+
go 1.23
4+
5+
toolchain go1.23.2
46

57
require (
8+
github.com/compscidr/scholar v1.0.5
69
github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6
710
github.com/gin-contrib/sessions v1.0.1
811
github.com/gin-contrib/static v1.1.2
@@ -16,6 +19,8 @@ require (
1619
)
1720

1821
require (
22+
github.com/PuerkitoBio/goquery v1.10.0 // indirect
23+
github.com/andybalholm/cascadia v1.3.2 // indirect
1924
github.com/beevik/etree v1.1.0 // indirect
2025
github.com/bytedance/sonic v1.11.6 // indirect
2126
github.com/bytedance/sonic/loader v0.1.1 // indirect
@@ -42,19 +47,20 @@ require (
4247
github.com/kr/text v0.2.0 // indirect
4348
github.com/leodido/go-urn v1.4.0 // indirect
4449
github.com/mattn/go-isatty v0.0.20 // indirect
45-
github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
50+
github.com/mattn/go-sqlite3 v1.14.24 // indirect
4651
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4752
github.com/modern-go/reflect2 v1.0.2 // indirect
53+
github.com/orcaman/concurrent-map/v2 v2.0.1 // indirect
4854
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
4955
github.com/pmezard/go-difflib v1.0.0 // indirect
5056
github.com/stretchr/objx v0.5.2 // indirect
5157
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
5258
github.com/ugorji/go/codec v1.2.12 // indirect
5359
golang.org/x/arch v0.8.0 // indirect
54-
golang.org/x/crypto v0.23.0 // indirect
55-
golang.org/x/net v0.25.0 // indirect
56-
golang.org/x/sys v0.20.0 // indirect
57-
golang.org/x/text v0.15.0 // indirect
60+
golang.org/x/crypto v0.27.0 // indirect
61+
golang.org/x/net v0.29.0 // indirect
62+
golang.org/x/sys v0.25.0 // indirect
63+
golang.org/x/text v0.18.0 // indirect
5864
google.golang.org/protobuf v1.34.1 // indirect
5965
gopkg.in/yaml.v3 v3.0.1 // indirect
6066
)

0 commit comments

Comments
 (0)