-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedin.go
66 lines (58 loc) · 1.39 KB
/
linkedin.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
package main
import (
"context"
"database/sql"
_ "embed"
"encoding/json"
"github.com/newhook/whoishiring/linkedin"
"github.com/newhook/whoishiring/queries"
"github.com/pkg/errors"
"strings"
"text/template"
"time"
)
//go:embed linkedin_resume.tmpl
var linkedInResumeContent string
var (
linkedInResumeTemplate = template.Must(template.New("linkedin_resume").Parse(linkedInResumeContent))
)
func scrapeLinkedIn(ctx context.Context, q *queries.Queries, link string) (string, error) {
var jsonBody []byte
saved, err := q.GetLinkedInScrape(ctx, link)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
return "", errors.WithStack(err)
}
profile, err := linkedin.Person(ctx, link)
if err != nil {
return "", err
}
jsonBody, err = json.Marshal(profile)
if err != nil {
return "", errors.WithStack(err)
}
now := int(time.Now().Unix())
err = q.InsertLinkedInScrape(ctx, queries.InsertLinkedInScrapeParams{
Url: link,
Json: string(jsonBody),
CreatedAt: now,
UpdatedAt: now,
})
if err != nil {
return "", errors.WithStack(err)
}
} else {
jsonBody = []byte(saved.Json)
}
var raw map[string]any
err = json.Unmarshal(jsonBody, &raw)
if err != nil {
return "", errors.WithStack(err)
}
sb := &strings.Builder{}
err = linkedInResumeTemplate.Execute(sb, raw)
if err != nil {
return "", errors.WithStack(err)
}
return sb.String(), nil
}