-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
52 lines (43 loc) · 1.62 KB
/
models.py
File metadata and controls
52 lines (43 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from typing import Optional, Dict, Any, List
from pydantic import BaseModel, Field, validator
from urllib.parse import urljoin
class ResponseStatus(BaseModel):
status: str
message: Optional[str] = Field(default=None, exclude_unset=True)
class MetadataModel(BaseModel):
opengraph: Optional[Dict[str, str]] = None
json_ld: Optional[Dict[str, Any]] = None
oembed: Optional[Dict[str, Any]] = None
spotify: Optional[Dict[str, Any]] = None
@validator('opengraph', 'json_ld', 'oembed', 'spotify', pre=True)
def validate_non_empty_dict(cls, v):
if not v or not isinstance(v, dict) or not any(v.values()):
return None
return v
class LinkPreview(ResponseStatus):
url: str
title: Optional[str] = None
description: Optional[str] = None
image: Optional[str] = None
favicon: Optional[str] = None
author: Optional[str] = None
keywords: Optional[List[str]] = Field(default_factory=list)
language: Optional[str] = None
metadata: Optional[MetadataModel] = None
@validator('keywords', pre=True)
def split_keywords(cls, v):
if isinstance(v, str):
return [k.strip() for k in v.split(',') if k.strip()]
elif isinstance(v, list):
return [k for k in v if k and isinstance(k, str)]
return []
@validator('image', 'favicon')
def validate_urls(cls, v, values):
if v and values.get('url'):
try:
return urljoin(values['url'], v)
except Exception:
return None
return v
class SummarizedLinkPreview(LinkPreview):
summary: Optional[str] = None