-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprofiles.py
93 lines (65 loc) · 2.31 KB
/
profiles.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
from typing_extensions import Literal
from pydantic import BaseModel
PROFILE_PATH = os.path.normcase("./profiles/profiles/")
class ScrapingTargets(BaseModel):
container_list: str
link_containers: str
links: str
class ElementSelector(BaseModel):
element: str
content_field: str
class ArticleMeta(BaseModel):
author: str | ElementSelector
publish_date: str | ElementSelector
title: str | ElementSelector
description: str | ElementSelector
image_url: str | ElementSelector
class ArticleContent(BaseModel):
container: str
remove: list[str]
class ProfileSource(BaseModel):
name: str
profile_name: str
address: str
image_url: str
retrieval_method: Literal["scraping", "dynamic", "rss"]
news_paths: list[str]
scraping_targets: ScrapingTargets
class ProfileScraping(BaseModel):
js_injections: list[str] = []
meta: ArticleMeta
content: ArticleContent
class Profile(BaseModel):
source: ProfileSource
scraping: ProfileScraping
def list_profiles(
complete_file_name: bool = False, include_disabled: bool = False
) -> list[str]:
def is_profile(name: str) -> bool:
if name.endswith(".profile"):
return True
if include_disabled and name.endswith(".disabled"):
return True
return False
def strip_extension(name: str) -> str:
return name.removesuffix(".profile").removesuffix(".disabled")
if complete_file_name:
return [x for x in os.listdir(PROFILE_PATH) if is_profile(x)]
else:
return [strip_extension(x) for x in os.listdir(PROFILE_PATH) if is_profile(x)]
def get_profile(specific_profile: str) -> Profile:
if not specific_profile.endswith(".profile") and not specific_profile.endswith(
".disabled"
):
specific_profile += ".profile"
with open(os.path.join(PROFILE_PATH, specific_profile)) as f:
return Profile.model_validate_json(f.read())
def get_profiles(include_disabled: bool = False) -> list[Profile]:
profiles: list[Profile] = []
for profile_name in list_profiles(
complete_file_name=True, include_disabled=include_disabled
):
with open(os.path.join(PROFILE_PATH, profile_name)) as f:
profiles.append(Profile.model_validate_json(f.read().strip()))
return profiles