| 1 | """Google: the server-rendered results page of Google's careers site. |
| 2 | |
| 3 | ``/about/careers/applications/jobs/results/?sort_by=date&page=N`` embeds its |
| 4 | twenty roles as the ``ds:1`` ``AF_initDataCallback`` data: each role's id, |
| 5 | title, company, locations and text, and the board's total. The order is by |
| 6 | the day a role was last changed, and no field is shown to be the day it was |
| 7 | first posted, so no row claims a posted date and the board prints no count |
| 8 | of roles new in the window. |
| 9 | |
| 10 | Google's robots.txt disallows this path. The owner decided the lane reads it |
| 11 | anyway (see SECURITY.md); ``LAST30DAYS_JOBS_RESPECT_ROBOTS=1`` restores the |
| 12 | skip. The read is kept small and slow: ``PAGE_LIMIT`` pages, two at a time. |
| 13 | """ |
| 14 | |
| 15 | from __future__ import annotations |
| 16 | |
| 17 | import json |
| 18 | import re |
| 19 | from typing import Any, Callable |
| 20 | |
| 21 | from .. import http, jobs |
| 22 | |
| 23 | PROVIDER_GOOGLE = "google" |
| 24 | PAGE_SIZE = 20 |
| 25 | # Twenty-five pages of twenty: 500 of the newest-changed roles, enough for the |
| 26 | # digest's forty, in about five seconds. |
| 27 | PAGE_LIMIT = 25 |
| 28 | WORKERS = 2 |
| 29 | RESULTS_URL = "https://www.google.com/about/careers/applications/jobs/results/" |
| 30 | _DATA = re.compile(r"AF_initDataCallback\(\{key:\s*'ds:1'.*?data:", re.S) |
| 31 | |
| 32 | |
| 33 | def listing_url(page: int = 1) -> str: |
| 34 | return f"{RESULTS_URL}?sort_by=date&page={page}" |
| 35 | |
| 36 | |
| 37 | def page_data(html: str) -> Any: |
| 38 | """The ``ds:1`` data of one results page, or None.""" |
| 39 | match = _DATA.search(html or "") |
| 40 | if not match: |
| 41 | return None |
| 42 | try: |
| 43 | found, _end = json.JSONDecoder().raw_decode(html[match.end():].lstrip()) |
| 44 | except ValueError: |
| 45 | return None |
| 46 | return found |
| 47 | |
| 48 | |
| 49 | def _text(field: Any) -> str: |
| 50 | """A ``[null, "<html>"]`` text field's HTML, or ''.""" |
| 51 | if isinstance(field, list) and len(field) > 1 and isinstance(field[1], str): |
| 52 | return field[1] |
| 53 | return "" |
| 54 | |
| 55 | |
| 56 | def _slug(title: str) -> str: |
| 57 | return re.sub(r"[^a-z0-9]+", "-", title.lower()).strip("-") |
| 58 | |
| 59 | |
| 60 | def parse_listing(payload: Any) -> tuple[list[dict[str, Any]], int]: |
| 61 | data = page_data(payload) if isinstance(payload, str) else None |
| 62 | rows = data[0] if isinstance(data, list) and data and isinstance(data[0], list) else None |
| 63 | if rows is None: |
| 64 | raise jobs.BoardShapeError("no ds:1 job list") |
| 65 | total = data[2] if len(data) > 2 and isinstance(data[2], int) else 0 |
| 66 | items: list[dict[str, Any]] = [] |
| 67 | for row in rows: |
| 68 | if not isinstance(row, list) or len(row) < 11 or not row[0] or not row[1]: |
| 69 | continue |
| 70 | ident, title = str(row[0]), str(row[1]).strip() |
| 71 | places = [ |
| 72 | str(place[0]).strip() for place in row[9] or [] |
| 73 | if isinstance(place, list) and place and place[0] |
| 74 | ] if isinstance(row[9], list) else [] |
| 75 | html = "<br/>".join(part for part in ( |
| 76 | _text(row[10]), |
| 77 | "<h3>Responsibilities</h3>" + _text(row[3]) if _text(row[3]) else "", |
| 78 | _text(row[4]), |
| 79 | ) if part) |
| 80 | item = jobs._ats_item( |
| 81 | provider=PROVIDER_GOOGLE, |
| 82 | slug="www.google.com", |
| 83 | ident=ident, |
| 84 | title=title, |
| 85 | url=f"{RESULTS_URL}{ident}-{_slug(title)}", |
| 86 | description=jobs._clean_html(html), |
| 87 | description_blocks=jobs._block_text(html), |
| 88 | date=None, |
| 89 | department="", |
| 90 | location=places[0] if places else "", |
| 91 | company_name=str(row[7] or "Google").strip(), |
| 92 | whole_posting=True, |
| 93 | ) |
| 94 | item["locations"] = places |
| 95 | items.append(item) |
| 96 | return items, total |
| 97 | |
| 98 | |
| 99 | def read_board( |
| 100 | entry: dict[str, Any], *, deadline: float, date_range: tuple[str, str], |
| 101 | ) -> tuple[list[dict[str, Any]], dict[str, Any]]: |
| 102 | """The newest-changed roles, ``PAGE_LIMIT`` pages at most, with the total.""" |
| 103 | |
| 104 | def page(offset: int) -> Callable[[float], Any]: |
| 105 | url = listing_url(offset // PAGE_SIZE + 1) |
| 106 | return lambda http_deadline: _page_text(url, http_deadline) |
| 107 | |
| 108 | items, info = jobs._read_newest_first( |
| 109 | page, parse_listing, page_size=PAGE_SIZE, row_ceiling=PAGE_SIZE * PAGE_LIMIT, |
| 110 | window_start=None, deadline=deadline, workers=WORKERS, |
| 111 | ) |
| 112 | # Ordered by the day a role last changed: the window is never counted. |
| 113 | info["board_window_covered"] = False |
| 114 | return items, info |
| 115 | |
| 116 | |
| 117 | def _page_text(url: str, http_deadline: float) -> str: |
| 118 | """One results page; a page that does not answer is a failed call.""" |
| 119 | text = http.get_text( |
| 120 | url, accept="text/html", timeout=jobs.API_TIMEOUT_SECONDS, retries=1, |
| 121 | deadline_monotonic=http_deadline, |
| 122 | ) |
| 123 | if text is None: |
| 124 | raise http.HTTPError(f"Google results page did not answer: {url}") |
| 125 | return text |
| 126 |