package netflix import ( "context" "crypto/sha256" "encoding/json" "fmt" "io" "net/http" "net/url" "regexp" "strings" "time" "github.com/hireme/hireme/internal/crawler" "github.com/hireme/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, `
", "\n") s = strings.ReplaceAll(s, "
", "\n") reTags := regexp.MustCompile(`<[^>]*>`) s = reTags.ReplaceAllString(s, "") return strings.TrimSpace(s) }