Files
hireme/pkg/crawler/netflix/netflix.go
T

221 lines
5.8 KiB
Go

package netflix
import (
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"strings"
"time"
"git.pengzhan.dev/haopengzhan/hireme/internal/crawler"
"git.pengzhan.dev/haopengzhan/hireme/internal/logger"
)
type NetflixCrawler struct {
client *http.Client
}
func NewNetflixCrawler() *NetflixCrawler {
return NewNetflixCrawlerWithClient(&http.Client{
Timeout: 30 * time.Second,
})
}
func NewNetflixCrawlerWithClient(client *http.Client) *NetflixCrawler {
return &NetflixCrawler{
client: client,
}
}
func (c *NetflixCrawler) CanHandle(platform string) bool {
p := strings.ToLower(platform)
return p == "netflix" || strings.Contains(p, "jobs.netflix.com")
}
type netflixSearchResponse struct {
Records struct {
Total int `json:"total"`
Postings []netflixJobItem `json:"postings"`
} `json:"records"`
}
type netflixJobItem struct {
ID int64 `json:"id"`
ExternalID string `json:"external_id"`
Title string `json:"text"`
Description string `json:"description"`
Location string `json:"location"`
Locations []string `json:"locations"`
Team string `json:"team"`
Department string `json:"department"`
}
func (c *NetflixCrawler) DiscoverJobs(ctx context.Context, targetURL string) ([]crawler.DiscoveredJob, error) {
seen := make(map[string]bool)
var discovered []crawler.DiscoveredJob
searchAPI := targetURL
if !strings.Contains(searchAPI, "api/search") {
u, err := url.Parse(targetURL)
if err == nil && u.Host != "" {
q := u.Query()
searchAPI = fmt.Sprintf("https://jobs.netflix.com/api/search?%s", q.Encode())
} else {
searchAPI = fmt.Sprintf("https://jobs.netflix.com/api/search?q=%s", url.QueryEscape(targetURL))
}
}
body, err := c.fetchURL(ctx, searchAPI)
if err != nil {
return nil, err
}
var resp netflixSearchResponse
if err := json.Unmarshal([]byte(body), &resp); err != nil {
return nil, fmt.Errorf("failed to parse Netflix search response: %w", err)
}
for _, job := range resp.Records.Postings {
jobID := job.ExternalID
if jobID == "" && job.ID != 0 {
jobID = fmt.Sprintf("%d", job.ID)
}
if jobID == "" {
continue
}
applyURL := fmt.Sprintf("https://jobs.netflix.com/jobs/%s", jobID)
if !seen[applyURL] {
seen[applyURL] = true
discovered = append(discovered, crawler.DiscoveredJob{
CanonicalID: fmt.Sprintf("netflix:%s", jobID),
ApplyURL: applyURL,
})
}
}
logger.Log.Infow("Netflix discovery finished", "found_count", len(discovered))
return discovered, nil
}
func (c *NetflixCrawler) FetchJobDetail(ctx context.Context, applyURL string) (*crawler.ExtractedJob, error) {
jobID := parseNetflixJobID(applyURL)
if jobID == "" {
jobID = fmt.Sprintf("%x", sha256.Sum256([]byte(applyURL)))[:16]
}
// Try API endpoint: https://jobs.netflix.com/api/search?q={id} or direct detail
detailAPI := fmt.Sprintf("https://jobs.netflix.com/api/postings/%s", jobID)
body, err := c.fetchURL(ctx, detailAPI)
if err == nil && json.Valid([]byte(body)) {
var job netflixJobItem
if err := json.Unmarshal([]byte(body), &job); err == nil && job.Title != "" {
loc := job.Location
if loc == "" && len(job.Locations) > 0 {
loc = strings.Join(job.Locations, "; ")
}
if loc == "" {
loc = "Seattle / Remote, WA"
}
return &crawler.ExtractedJob{
CanonicalID: fmt.Sprintf("netflix:%s", jobID),
Title: job.Title,
Company: "Netflix",
Location: loc,
ApplyURL: applyURL,
RawContent: cleanHTML(job.Description),
}, nil
}
}
// Fallback to HTML
htmlBody, err := c.fetchURL(ctx, applyURL)
if err != nil {
return nil, err
}
title := extractRegex(htmlBody, `<h1[^>]*>(.*?)</h1>`)
if title == "" {
title = "Software Engineer - Netflix"
}
return &crawler.ExtractedJob{
CanonicalID: fmt.Sprintf("netflix:%s", jobID),
Title: cleanHTML(title),
Company: "Netflix",
Location: "Seattle / Remote, WA",
ApplyURL: applyURL,
RawContent: cleanHTML(htmlBody),
}, nil
}
func (c *NetflixCrawler) 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 parseNetflixJobID(u string) string {
re := regexp.MustCompile(`/jobs/([0-9A-Za-z\-]+)`)
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)
}