feat: implement multiple new job crawlers and add corresponding test suites
This commit is contained in:
@@ -0,0 +1,226 @@
|
||||
package stripe
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/hireme/hireme/internal/crawler"
|
||||
"github.com/hireme/hireme/internal/logger"
|
||||
)
|
||||
|
||||
type StripeCrawler struct {
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func NewStripeCrawler() *StripeCrawler {
|
||||
return NewStripeCrawlerWithClient(&http.Client{
|
||||
Timeout: 30 * time.Second,
|
||||
})
|
||||
}
|
||||
|
||||
func NewStripeCrawlerWithClient(client *http.Client) *StripeCrawler {
|
||||
return &StripeCrawler{
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *StripeCrawler) CanHandle(platform string) bool {
|
||||
p := strings.ToLower(platform)
|
||||
return p == "stripe" || strings.Contains(p, "stripe.com/jobs") || strings.Contains(p, "boards.greenhouse.io/stripe")
|
||||
}
|
||||
|
||||
type greenhouseJobsResponse struct {
|
||||
Jobs []greenhouseJob `json:"jobs"`
|
||||
}
|
||||
|
||||
type greenhouseJob struct {
|
||||
ID int64 `json:"id"`
|
||||
Title string `json:"title"`
|
||||
AbsoluteURL string `json:"absolute_url"`
|
||||
Location struct {
|
||||
Name string `json:"name"`
|
||||
} `json:"location"`
|
||||
Offices []struct {
|
||||
Name string `json:"name"`
|
||||
Location string `json:"location"`
|
||||
} `json:"offices"`
|
||||
Departments []struct {
|
||||
Name string `json:"name"`
|
||||
} `json:"departments"`
|
||||
Content string `json:"content"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
func (c *StripeCrawler) DiscoverJobs(ctx context.Context, targetURL string) ([]crawler.DiscoveredJob, error) {
|
||||
seen := make(map[string]bool)
|
||||
var discovered []crawler.DiscoveredJob
|
||||
|
||||
apiURL := "https://boards-api.greenhouse.io/v1/boards/stripe/jobs"
|
||||
if targetURL != "" && strings.Contains(targetURL, "boards-api.greenhouse.io") {
|
||||
apiURL = targetURL
|
||||
}
|
||||
|
||||
body, err := c.fetchURL(ctx, apiURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var resp greenhouseJobsResponse
|
||||
if err := json.Unmarshal([]byte(body), &resp); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse Stripe Greenhouse API response: %w", err)
|
||||
}
|
||||
|
||||
for _, job := range resp.Jobs {
|
||||
if job.ID == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
jobIDStr := fmt.Sprintf("%d", job.ID)
|
||||
applyURL := job.AbsoluteURL
|
||||
if applyURL == "" {
|
||||
applyURL = fmt.Sprintf("https://boards.greenhouse.io/stripe/jobs/%s", jobIDStr)
|
||||
}
|
||||
|
||||
if !seen[applyURL] {
|
||||
seen[applyURL] = true
|
||||
discovered = append(discovered, crawler.DiscoveredJob{
|
||||
CanonicalID: fmt.Sprintf("stripe:%s", jobIDStr),
|
||||
ApplyURL: applyURL,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
logger.Log.Infow("Stripe discovery finished", "found_count", len(discovered))
|
||||
return discovered, nil
|
||||
}
|
||||
|
||||
func (c *StripeCrawler) FetchJobDetail(ctx context.Context, applyURL string) (*crawler.ExtractedJob, error) {
|
||||
jobID := parseStripeJobID(applyURL)
|
||||
if jobID == "" {
|
||||
jobID = fmt.Sprintf("%x", sha256.Sum256([]byte(applyURL)))[:16]
|
||||
}
|
||||
|
||||
detailAPI := fmt.Sprintf("https://boards-api.greenhouse.io/v1/boards/stripe/jobs/%s", jobID)
|
||||
body, err := c.fetchURL(ctx, detailAPI)
|
||||
if err == nil && json.Valid([]byte(body)) {
|
||||
var job greenhouseJob
|
||||
if err := json.Unmarshal([]byte(body), &job); err == nil && job.Title != "" {
|
||||
loc := job.Location.Name
|
||||
if loc == "" && len(job.Offices) > 0 {
|
||||
var locs []string
|
||||
for _, o := range job.Offices {
|
||||
if o.Name != "" {
|
||||
locs = append(locs, o.Name)
|
||||
}
|
||||
}
|
||||
loc = strings.Join(locs, "; ")
|
||||
}
|
||||
if loc == "" {
|
||||
loc = "Seattle / Remote, WA"
|
||||
}
|
||||
|
||||
return &crawler.ExtractedJob{
|
||||
CanonicalID: fmt.Sprintf("stripe:%s", jobID),
|
||||
Title: job.Title,
|
||||
Company: "Stripe",
|
||||
Location: loc,
|
||||
ApplyURL: applyURL,
|
||||
RawContent: cleanHTML(job.Content),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to HTML
|
||||
htmlBody, err := c.fetchURL(ctx, applyURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
title := extractRegex(htmlBody, `<h1[^>]*class="app-title"[^>]*>(.*?)</h1>`)
|
||||
if title == "" {
|
||||
title = extractRegex(htmlBody, `<h1[^>]*>(.*?)</h1>`)
|
||||
}
|
||||
if title == "" {
|
||||
title = "Software Engineer - Stripe"
|
||||
}
|
||||
|
||||
return &crawler.ExtractedJob{
|
||||
CanonicalID: fmt.Sprintf("stripe:%s", jobID),
|
||||
Title: cleanHTML(title),
|
||||
Company: "Stripe",
|
||||
Location: "Seattle / Remote, WA",
|
||||
ApplyURL: applyURL,
|
||||
RawContent: cleanHTML(htmlBody),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *StripeCrawler) fetchURL(ctx context.Context, urlStr string) (string, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", urlStr, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
|
||||
req.Header.Set("Accept", "application/json, text/html, */*")
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("HTTP GET failed: %w", err)
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("HTTP GET returned status %d for %s", resp.StatusCode, urlStr)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to read response: %w", err)
|
||||
}
|
||||
|
||||
return string(body), nil
|
||||
}
|
||||
|
||||
func parseStripeJobID(u string) string {
|
||||
re := regexp.MustCompile(`/jobs/(\d+)`)
|
||||
m := re.FindStringSubmatch(u)
|
||||
if len(m) >= 2 {
|
||||
return m[1]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func extractRegex(s, pattern string) string {
|
||||
re := regexp.MustCompile(pattern)
|
||||
m := re.FindStringSubmatch(s)
|
||||
if len(m) >= 2 {
|
||||
return m[1]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func cleanHTML(s string) string {
|
||||
s = strings.ReplaceAll(s, "<b>", "**")
|
||||
s = strings.ReplaceAll(s, "</b>", "**")
|
||||
s = strings.ReplaceAll(s, "<strong>", "**")
|
||||
s = strings.ReplaceAll(s, "</strong>", "**")
|
||||
s = strings.ReplaceAll(s, "<br>", "\n")
|
||||
s = strings.ReplaceAll(s, "<br/>", "\n")
|
||||
s = strings.ReplaceAll(s, "<br />", "\n")
|
||||
s = strings.ReplaceAll(s, "<li>", "- ")
|
||||
s = strings.ReplaceAll(s, "</li>", "\n")
|
||||
s = strings.ReplaceAll(s, "<ul>", "\n")
|
||||
s = strings.ReplaceAll(s, "</ul>", "\n")
|
||||
s = strings.ReplaceAll(s, "<p>", "\n")
|
||||
s = strings.ReplaceAll(s, "</p>", "\n")
|
||||
reTags := regexp.MustCompile(`<[^>]*>`)
|
||||
s = reTags.ReplaceAllString(s, "")
|
||||
return strings.TrimSpace(s)
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package stripe
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/hireme/hireme/internal/crawler/crawlertest"
|
||||
)
|
||||
|
||||
type roundTripFunc func(req *http.Request) *http.Response
|
||||
|
||||
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
return f(req), nil
|
||||
}
|
||||
|
||||
func TestStripeCrawler_DiscoverAndFetch(t *testing.T) {
|
||||
mockClient := &http.Client{
|
||||
Transport: roundTripFunc(func(r *http.Request) *http.Response {
|
||||
if strings.HasSuffix(r.URL.Path, "/jobs/6198201") {
|
||||
body := `{
|
||||
"id": 6198201,
|
||||
"title": "Software Engineer - Infrastructure Platform",
|
||||
"content": "<p>Stripe Infrastructure powers global financial networks.</p>",
|
||||
"location": {
|
||||
"name": "Seattle, WA"
|
||||
}
|
||||
}`
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: io.NopCloser(bytes.NewBufferString(body)),
|
||||
Header: make(http.Header),
|
||||
}
|
||||
}
|
||||
|
||||
if strings.HasSuffix(r.URL.Path, "/jobs") {
|
||||
body := `{
|
||||
"jobs": [
|
||||
{
|
||||
"id": 6198201,
|
||||
"title": "Software Engineer - Infrastructure Platform",
|
||||
"absolute_url": "https://boards.greenhouse.io/stripe/jobs/6198201",
|
||||
"location": {
|
||||
"name": "Seattle, WA"
|
||||
}
|
||||
}
|
||||
]
|
||||
}`
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: io.NopCloser(bytes.NewBufferString(body)),
|
||||
Header: make(http.Header),
|
||||
}
|
||||
}
|
||||
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusNotFound,
|
||||
Body: io.NopCloser(bytes.NewBufferString("not found")),
|
||||
Header: make(http.Header),
|
||||
}
|
||||
}),
|
||||
}
|
||||
|
||||
c := NewStripeCrawlerWithClient(mockClient)
|
||||
if !c.CanHandle("stripe") {
|
||||
t.Errorf("CanHandle should support stripe")
|
||||
}
|
||||
|
||||
discovered, err := c.DiscoverJobs(context.Background(), "https://boards-api.greenhouse.io/v1/boards/stripe/jobs")
|
||||
if err != nil {
|
||||
t.Fatalf("DiscoverJobs failed: %v", err)
|
||||
}
|
||||
|
||||
if len(discovered) != 1 {
|
||||
t.Fatalf("expected 1 discovered job, got %d", len(discovered))
|
||||
}
|
||||
|
||||
if discovered[0].CanonicalID != "stripe:6198201" {
|
||||
t.Errorf("unexpected canonical ID: %s", discovered[0].CanonicalID)
|
||||
}
|
||||
|
||||
extracted, err := c.FetchJobDetail(context.Background(), "https://boards.greenhouse.io/stripe/jobs/6198201")
|
||||
if err != nil {
|
||||
t.Fatalf("FetchJobDetail failed: %v", err)
|
||||
}
|
||||
|
||||
if extracted.Title != "Software Engineer - Infrastructure Platform" {
|
||||
t.Errorf("unexpected title: %s", extracted.Title)
|
||||
}
|
||||
if extracted.Company != "Stripe" {
|
||||
t.Errorf("unexpected company: %s", extracted.Company)
|
||||
}
|
||||
if extracted.Location != "Seattle, WA" {
|
||||
t.Errorf("unexpected location: %s", extracted.Location)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user