Skip to content

metadata

Caller-supplied document metadata for GKG records.

RecordMetadata dataclass

RecordMetadata(identifier: str, batch_time: Timestamp, sequence: int, published_at: Timestamp | None = None, source_collection: int = 1, source_name: str | None = None, sharing_image: str | None = None, related_images: Iterable[str] | None = None, social_image_embeds: Iterable[str] | None = None, social_video_embeds: Iterable[str] | None = None, extras: Mapping[str, XMLValue] | None = None, source_language: str | None = None, translation_engine: str | None = None)

Document identity, timestamps, media, and translation provenance.

None media values mark unavailable data. Empty values mark known absence.

Parameters:

Name Type Description Default
identifier str

Document URL, DOI, or citation.

required
batch_time Timestamp

Record batch timestamp.

required
sequence int

Caller-managed positive sequence number within the batch.

required
published_at Timestamp | None

Optional precise publication timestamp for extras.

None
source_collection int

GKG source collection code, from 1 through 6.

1
source_name str | None

Explicit source label; defaults to a web URL's hostname.

None
sharing_image str | None

Selected sharing-image URL.

None
related_images Iterable[str] | None

Selected article image URLs.

None
social_image_embeds Iterable[str] | None

Social-post URLs containing images.

None
social_video_embeds Iterable[str] | None

Selected video URLs.

None
extras Mapping[str, XMLValue] | None

Additional XML extension elements.

None
source_language str | None

Original language code for translated bodies.

None
translation_engine str | None

Engine citation, supplied with source_language.

None

from_translation classmethod

from_translation(result: TranslationResult, metadata: RecordMetadata) -> RecordMetadata

Copy translation provenance into a new metadata instance.

Parameters:

Name Type Description Default
result TranslationResult

Translation result whose English text will be analyzed.

required
metadata RecordMetadata

Existing document metadata.

required

Returns:

Type Description
RecordMetadata

Updated metadata with source language and engine for translated input.

Raises:

Type Description
TypeError

If argument types are invalid.

ValueError

If translation provenance conflicts with existing metadata.

Source code in src/gdelt_regkg/fields/metadata.py
@classmethod
def from_translation(
    cls, result: TranslationResult, metadata: RecordMetadata
) -> RecordMetadata:
    """Copy translation provenance into a new metadata instance.

    Args:
        result: Translation result whose English text will be analyzed.
        metadata: Existing document metadata.

    Returns:
        Updated metadata with source language and engine for translated input.

    Raises:
        TypeError: If argument types are invalid.
        ValueError: If translation provenance conflicts with existing metadata.
    """
    if not isinstance(result, TranslationResult) or not isinstance(metadata, cls):
        raise TypeError("Expected TranslationResult and RecordMetadata")
    language = normalize_language(result.source_language)
    translation_info = (language, result.engine) if language != "eng" else (None, None)
    if (metadata.source_language, metadata.translation_engine) not in (
        (None, None),
        translation_info,
    ):
        raise ValueError(
            "Existing metadata conflicts with the source language or translation engine"
        )
    format_translation_info(*translation_info)
    return replace(
        metadata, source_language=translation_info[0], translation_engine=translation_info[1]
    )

to_mapping

to_mapping() -> dict[str, str | None]

Format the record identity, source, batch date, and translation cells.

Source code in src/gdelt_regkg/fields/metadata.py
def to_mapping(self) -> dict[str, str | None]:
    """Format the record identity, source, batch date, and translation cells."""
    stamp = format_date(self.batch_time)
    translation = format_translation_info(self.source_language, self.translation_engine)
    return {
        "GKGRECORDID": format_record_id(stamp, self.sequence, translated=bool(translation)),
        "V2.1DATE": stamp,
        "V2SOURCECOLLECTIONIDENTIFIER": format_source_collection(self.source_collection),
        "V2SOURCECOMMONNAME": source_common_name(self.identifier, name=self.source_name),
        "V2DOCUMENTIDENTIFIER": document_identifier(self.identifier),
        "V2.1TRANSLATIONINFO": translation,
    }

format_date

format_date(value: datetime | str | int | None) -> str

Format a UTC timestamp as YYYYMMDDHHMMSS; None/0 means unknown.

Aware datetimes are converted to UTC; naive datetimes are treated as UTC. Strings must already contain exactly fourteen ASCII digits (or '0').

Source code in src/gdelt_regkg/fields/metadata.py
def format_date(value: datetime | str | int | None) -> str:
    """Format a UTC timestamp as YYYYMMDDHHMMSS; None/0 means unknown.

    Aware datetimes are converted to UTC; naive datetimes are treated as UTC.
    Strings must already contain exactly fourteen ASCII digits (or '0').
    """
    if value is None:
        return "0"
    if isinstance(value, datetime):
        if value.utcoffset() is not None:
            value = value.astimezone(timezone.utc)
        return f"{value.year:04d}{value.month:02d}{value.day:02d}{value.hour:02d}{value.minute:02d}{value.second:02d}"
    if isinstance(value, bool) or not isinstance(value, (str, Integral)):
        raise TypeError("Expected datetime, compact timestamp, integer, or None")
    result = str(value)
    if result == "0":
        return result
    if not re.fullmatch(r"[0-9]{14}", result):
        raise ValueError("Date must be YYYYMMDDHHMMSS or 0")
    # Parsing rejects impossible calendar dates before formatting the result.
    return format_date(datetime.strptime(result, "%Y%m%d%H%M%S"))

format_record_id

format_record_id(batch_time: datetime | str | int, sequence: int, *, translated: bool = False) -> str

Build a local record ID from batch time and caller-managed sequence.

The caller owns uniqueness; these IDs are not assigned by GDELT. Batch time is separate from the document's publication time.

Source code in src/gdelt_regkg/fields/metadata.py
def format_record_id(
    batch_time: datetime | str | int, sequence: int, *, translated: bool = False
) -> str:
    """Build a local record ID from batch time and caller-managed sequence.

    The caller owns uniqueness; these IDs are not assigned by GDELT.
    Batch time is separate from the document's publication time.
    """
    stamp = format_date(batch_time)
    if stamp == "0":
        raise ValueError("Record IDs require a known batch timestamp")
    if isinstance(sequence, bool) or not isinstance(sequence, Integral) or sequence < 1:
        raise ValueError("Sequence must be a positive integer")
    if not isinstance(translated, bool):
        raise TypeError("translated must be bool")
    return f"{stamp}-{'T' if translated else ''}{sequence}"

format_source_collection

format_source_collection(value: int = 1) -> str

1 web, 2 citation, 3 CORE, 4 DTIC, 5 JSTOR, 6 nontextual source.

Source code in src/gdelt_regkg/fields/metadata.py
def format_source_collection(value: int = 1) -> str:
    """1 web, 2 citation, 3 CORE, 4 DTIC, 5 JSTOR, 6 nontextual source."""
    if isinstance(value, bool) or not isinstance(value, Integral) or value not in range(1, 7):
        raise ValueError("Source collection must be an integer from 1 through 6")
    return str(value)

source_common_name

source_common_name(identifier: str, *, name: str | None = None) -> str

Use an explicit source label, otherwise extract a web URL's hostname.

Keeps subdomains; registrable-domain normalization is not implemented. Offline sources should always supply name explicitly.

Source code in src/gdelt_regkg/fields/metadata.py
def source_common_name(identifier: str, *, name: str | None = None) -> str:
    """Use an explicit source label, otherwise extract a web URL's hostname.

    Keeps subdomains; registrable-domain normalization is not implemented.
    Offline sources should always supply name explicitly.
    """
    if name is not None:
        return _cell(name)
    parsed = urlsplit(_cell(identifier))
    if parsed.scheme.lower() not in {"http", "https"} or not parsed.hostname:
        raise ValueError("Supply an HTTP(S) URL or an explicit source name")
    return parsed.hostname.lower()

document_identifier

document_identifier(value: str) -> str

Preserve a supplied URL, DOI, or citation without fetching it.

Source code in src/gdelt_regkg/fields/metadata.py
def document_identifier(value: str) -> str:
    """Preserve a supplied URL, DOI, or citation without fetching it."""
    value = _cell(value)
    if not value.strip():
        raise ValueError("Document identifier must not be empty")
    return value

format_translation_info

format_translation_info(source_language: str | None = None, engine: str | None = None) -> str

Format the source language and translation engine; does not translate.

Supply both an ISO 639-2 code and an engine citation, or neither. Only the code's shape is checked, not membership in ISO 639-2.

Source code in src/gdelt_regkg/fields/metadata.py
def format_translation_info(source_language: str | None = None, engine: str | None = None) -> str:
    """Format the source language and translation engine; does not translate.

    Supply both an ISO 639-2 code and an engine citation, or neither.
    Only the code's shape is checked, not membership in ISO 639-2.
    """
    if source_language is None and engine is None:
        return ""
    if not isinstance(source_language, str) or not re.fullmatch(r"[a-z]{3}", source_language):
        raise ValueError("Source language must be a lowercase three-letter ISO 639-2 code")
    if not isinstance(engine, str):
        raise ValueError("Provide a nonempty engine citation")
    engine = text_component(engine, separators=";")
    if not engine:
        raise ValueError("Provide a nonempty engine citation")
    return f"srclc:{source_language};eng:{engine}"

format_sharing_image

format_sharing_image(url: str | None) -> str | None

Format a supplied image URL; None is unavailable and '' means no image.

Source code in src/gdelt_regkg/fields/metadata.py
def format_sharing_image(url: str | None) -> str | None:
    """Format a supplied image URL; None is unavailable and '' means no image."""
    return url if url is None or url == "" else _media_url(url)
format_related_images(urls: Iterable[str] | None) -> str | None

Format caller-selected article image URLs, preserving order and duplicates.

Source code in src/gdelt_regkg/fields/metadata.py
def format_related_images(urls: Iterable[str] | None) -> str | None:
    """Format caller-selected article image URLs, preserving order and duplicates."""
    return _media_urls(urls)

format_social_image_embeds

format_social_image_embeds(urls: Iterable[str] | None) -> str | None

Format supplied social-post URLs; the caller determines image content.

Source code in src/gdelt_regkg/fields/metadata.py
def format_social_image_embeds(urls: Iterable[str] | None) -> str | None:
    """Format supplied social-post URLs; the caller determines image content."""
    return _media_urls(urls)

format_social_video_embeds

format_social_video_embeds(urls: Iterable[str] | None) -> str | None

Format supplied video URLs; the caller identifies and normalizes embeds.

Source code in src/gdelt_regkg/fields/metadata.py
def format_social_video_embeds(urls: Iterable[str] | None) -> str | None:
    """Format supplied video URLs; the caller identifies and normalizes embeds."""
    return _media_urls(urls)