Skip to content

Commit

Permalink
Merge pull request #302 from compscidr/jason/go-scholar-update
Browse files Browse the repository at this point in the history
WiP: go scholar library to automatically pull in google scholar entries
  • Loading branch information
compscidr authored Oct 26, 2024
2 parents 00132e1 + 9cdfbe7 commit d51c8e6
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 311 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:
runs-on: ubuntu-latest
steps:

- name: Set up Go 1.20.0
- name: Set up Go 1.22.2
uses: actions/setup-go@v5
with:
go-version: 1.20.0
go-version: 1.22.2
id: go

- name: Check out code into the Go module directory
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
articles.json
profiles.json

# Binaries for programs and plugins
*.exe
*.exe~
Expand Down
4 changes: 3 additions & 1 deletion admin/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package admin_test
import (
"bytes"
"encoding/json"
scholar "github.com/compscidr/scholar"
"goblog/admin"
"goblog/auth"
"goblog/blog"
Expand Down Expand Up @@ -44,7 +45,8 @@ func TestCreatePost(t *testing.T) {
db.AutoMigrate(&auth.BlogUser{})
db.AutoMigrate(&blog.Post{})
a := &Auth{}
b := blog.New(db, a, "test")
sch := scholar.New("profiles.json", "articles.json")
b := blog.New(db, a, "test", sch)
ad := admin.New(db, a, b, "test")

router := gin.Default()
Expand Down
153 changes: 79 additions & 74 deletions blog/blog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package blog

import (
"errors"
scholar "github.com/compscidr/scholar"
"goblog/auth"
"log"
"net/http"
Expand All @@ -25,11 +26,12 @@ type Blog struct {
db *gorm.DB
auth auth.IAuth
Version string
scholar *scholar.Scholar
}

// New constructs an Admin API
func New(db *gorm.DB, auth auth.IAuth, version string) Blog {
api := Blog{db, auth, version}
func New(db *gorm.DB, auth auth.IAuth, version string, scholar *scholar.Scholar) Blog {
api := Blog{db, auth, version, scholar}
return api
}

Expand Down Expand Up @@ -172,20 +174,20 @@ func (b Blog) NoRoute(c *gin.Context) {
if err == nil && post != nil {
if b.auth.IsAdmin(c) {
c.HTML(http.StatusOK, "post-admin.html", gin.H{
"logged_in": b.auth.IsLoggedIn(c),
"is_admin": b.auth.IsAdmin(c),
"post": post,
"version": b.Version,
"recent": b.GetLatest(),
"logged_in": b.auth.IsLoggedIn(c),
"is_admin": b.auth.IsAdmin(c),
"post": post,
"version": b.Version,
"recent": b.GetLatest(),
"admin_page": false,
})
} else {
c.HTML(http.StatusOK, "post.html", gin.H{
"logged_in": b.auth.IsLoggedIn(c),
"is_admin": b.auth.IsAdmin(c),
"post": post,
"version": b.Version,
"recent": b.GetLatest(),
"logged_in": b.auth.IsLoggedIn(c),
"is_admin": b.auth.IsAdmin(c),
"post": post,
"version": b.Version,
"recent": b.GetLatest(),
"admin_page": false,
})
}
Expand All @@ -205,7 +207,7 @@ func (b Blog) NoRoute(c *gin.Context) {
"description": "The page at '" + c.Request.URL.String() + "' was not found",
"version": b.Version,
"recent": b.GetLatest(),
"admin_page": false,
"admin_page": false,
})
}

Expand All @@ -214,24 +216,24 @@ func (b Blog) NoRoute(c *gin.Context) {
// need to modify this function
func (b Blog) Home(c *gin.Context) {
c.HTML(http.StatusOK, "home.html", gin.H{
"logged_in": b.auth.IsLoggedIn(c),
"is_admin": b.auth.IsAdmin(c),
"version": b.Version,
"title": "Software Engineer",
"recent": b.GetLatest(),
"logged_in": b.auth.IsLoggedIn(c),
"is_admin": b.auth.IsAdmin(c),
"version": b.Version,
"title": "Software Engineer",
"recent": b.GetLatest(),
"admin_page": false,
})
}

// Posts is the index page for blog posts
func (b Blog) Posts(c *gin.Context) {
c.HTML(http.StatusOK, "posts.html", gin.H{
"logged_in": b.auth.IsLoggedIn(c),
"is_admin": b.auth.IsAdmin(c),
"posts": b.GetPosts(false),
"version": b.Version,
"title": "Posts",
"recent": b.GetLatest(),
"logged_in": b.auth.IsLoggedIn(c),
"is_admin": b.auth.IsAdmin(c),
"posts": b.GetPosts(false),
"version": b.Version,
"title": "Posts",
"recent": b.GetLatest(),
"admin_page": false,
})
}
Expand All @@ -246,15 +248,15 @@ func (b Blog) Post(c *gin.Context) {
"version": b.Version,
"title": "Post Not Found",
"recent": b.GetLatest(),
"admin_page": false,
"admin_page": false,
})
} else {
c.HTML(http.StatusOK, "post.html", gin.H{
"logged_in": b.auth.IsLoggedIn(c),
"is_admin": b.auth.IsAdmin(c),
"post": post,
"version": b.Version,
"recent": b.GetLatest(),
"logged_in": b.auth.IsLoggedIn(c),
"is_admin": b.auth.IsAdmin(c),
"post": post,
"version": b.Version,
"recent": b.GetLatest(),
"admin_page": false,
})
//if b.auth.IsAdmin(c) {
Expand Down Expand Up @@ -286,17 +288,17 @@ func (b Blog) Tag(c *gin.Context) {
"version": b.Version,
"title": "Tag '" + tag + "' Not Found",
"recent": b.GetLatest(),
"admin_page": false,
"admin_page": false,
})
} else {
c.HTML(http.StatusOK, "tag.html", gin.H{
"logged_in": b.auth.IsLoggedIn(c),
"is_admin": b.auth.IsAdmin(c),
"posts": posts,
"tag": tag,
"version": b.Version,
"title": "Posts with Tag '" + tag + "'",
"recent": b.GetLatest(),
"logged_in": b.auth.IsLoggedIn(c),
"is_admin": b.auth.IsAdmin(c),
"posts": posts,
"tag": tag,
"version": b.Version,
"title": "Posts with Tag '" + tag + "'",
"recent": b.GetLatest(),
"admin_page": false,
})
}
Expand All @@ -305,58 +307,61 @@ func (b Blog) Tag(c *gin.Context) {
// Tags is the index page for all Tags
func (b Blog) Tags(c *gin.Context) {
c.HTML(http.StatusOK, "tags.html", gin.H{
"version": b.Version,
"title": "Tags",
"tags": b.getTags(),
"recent": b.GetLatest(),
"version": b.Version,
"title": "Tags",
"tags": b.getTags(),
"recent": b.GetLatest(),
"admin_page": false,
})
}

// Speaking is the index page for presentations
func (b Blog) Speaking(c *gin.Context) {
c.HTML(http.StatusOK, "presentations.html", gin.H{
"logged_in": b.auth.IsLoggedIn(c),
"is_admin": b.auth.IsAdmin(c),
"version": b.Version,
"title": "Presentations and Speaking",
"recent": b.GetLatest(),
"logged_in": b.auth.IsLoggedIn(c),
"is_admin": b.auth.IsAdmin(c),
"version": b.Version,
"title": "Presentations and Speaking",
"recent": b.GetLatest(),
"admin_page": false,
})
}

// Speaking is the index page for research publications
func (b Blog) Research(c *gin.Context) {
articles := b.scholar.QueryProfileWithMemoryCache("SbUmSEAAAAAJ", 50)
b.scholar.SaveCache("profiles.json", "articles.json")
c.HTML(http.StatusOK, "research.html", gin.H{
"logged_in": b.auth.IsLoggedIn(c),
"is_admin": b.auth.IsAdmin(c),
"version": b.Version,
"title": "Research Publications",
"recent": b.GetLatest(),
"logged_in": b.auth.IsLoggedIn(c),
"is_admin": b.auth.IsAdmin(c),
"version": b.Version,
"title": "Research Publications",
"recent": b.GetLatest(),
"articles": articles,
"admin_page": false,
})
}

// Projects is the index page for projects / code
func (b Blog) Projects(c *gin.Context) {
c.HTML(http.StatusOK, "projects.html", gin.H{
"logged_in": b.auth.IsLoggedIn(c),
"is_admin": b.auth.IsAdmin(c),
"version": b.Version,
"title": "Projects",
"recent": b.GetLatest(),
"logged_in": b.auth.IsLoggedIn(c),
"is_admin": b.auth.IsAdmin(c),
"version": b.Version,
"title": "Projects",
"recent": b.GetLatest(),
"admin_page": false,
})
}

// About is the about page
func (b Blog) About(c *gin.Context) {
c.HTML(http.StatusOK, "about.html", gin.H{
"logged_in": b.auth.IsLoggedIn(c),
"is_admin": b.auth.IsAdmin(c),
"version": b.Version,
"title": "About",
"recent": b.GetLatest(),
"logged_in": b.auth.IsLoggedIn(c),
"is_admin": b.auth.IsAdmin(c),
"version": b.Version,
"title": "About",
"recent": b.GetLatest(),
"admin_page": false,
})
}
Expand All @@ -371,7 +376,7 @@ func (b Blog) Archives(c *gin.Context) {
"byYear": b.getArchivesByYear(),
"byYearMonth": b.getArchivesByYearMonth(),
"recent": b.GetLatest(),
"admin_page": false,
"admin_page": false,
})
}

Expand Down Expand Up @@ -413,11 +418,11 @@ func (b Blog) Login(c *gin.Context) {
if err != nil {
//todo: handle better - perhaps return error to browser
c.HTML(http.StatusInternalServerError, "Error loading .env file: "+err.Error(), gin.H{
"logged_in": b.auth.IsLoggedIn(c),
"is_admin": b.auth.IsAdmin(c),
"version": b.Version,
"title": "Login Configuration Error",
"recent": b.GetLatest(),
"logged_in": b.auth.IsLoggedIn(c),
"is_admin": b.auth.IsAdmin(c),
"version": b.Version,
"title": "Login Configuration Error",
"recent": b.GetLatest(),
"admin_page": false,
})
return
Expand All @@ -426,12 +431,12 @@ func (b Blog) Login(c *gin.Context) {

clientID := os.Getenv("client_id")
c.HTML(http.StatusOK, "login.html", gin.H{
"logged_in": b.auth.IsLoggedIn(c),
"is_admin": b.auth.IsAdmin(c),
"client_id": clientID,
"version": b.Version,
"title": "Login",
"recent": b.GetLatest(),
"logged_in": b.auth.IsLoggedIn(c),
"is_admin": b.auth.IsAdmin(c),
"client_id": clientID,
"version": b.Version,
"title": "Login",
"recent": b.GetLatest(),
"admin_page": false,
})
}
Expand Down
4 changes: 3 additions & 1 deletion blog/blog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package blog_test
import (
"bytes"
"encoding/json"
scholar "github.com/compscidr/scholar"
"goblog/admin"
"goblog/auth"
"goblog/blog"
Expand Down Expand Up @@ -42,7 +43,8 @@ func TestBlogWorkflow(t *testing.T) {
db.AutoMigrate(&blog.Post{})
db.AutoMigrate(&blog.Tag{})
a := &Auth{}
b := blog.New(db, a, "test")
sch := scholar.New("profiles.json", "articles.json")
b := blog.New(db, a, "test", sch)
admin := admin.New(db, a, b, "test")

router := gin.Default()
Expand Down
18 changes: 12 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
module goblog

go 1.19
go 1.23

toolchain go1.23.2

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

require (
github.com/PuerkitoBio/goquery v1.10.0 // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/beevik/etree v1.1.0 // indirect
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
Expand All @@ -42,19 +47,20 @@ require (
github.com/kr/text v0.2.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
github.com/mattn/go-sqlite3 v1.14.24 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/orcaman/concurrent-map/v2 v2.0.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit d51c8e6

Please sign in to comment.