socialcli 0.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of socialcli might be problematic. Click here for more details.

Files changed (224) hide show
  1. socialcli-0.3.0.dist-info/METADATA +168 -0
  2. socialcli-0.3.0.dist-info/RECORD +224 -0
  3. socialcli-0.3.0.dist-info/WHEEL +4 -0
  4. socialcli-0.3.0.dist-info/entry_points.txt +3 -0
  5. socialcli-0.3.0.dist-info/licenses/LICENSE +21 -0
  6. socialctl/__init__.py +0 -0
  7. socialctl/__main__.py +6 -0
  8. socialctl/adapters/__init__.py +6 -0
  9. socialctl/adapters/base.py +68 -0
  10. socialctl/adapters/errores.py +56 -0
  11. socialctl/adapters/facebook.py +180 -0
  12. socialctl/adapters/instagram.py +442 -0
  13. socialctl/adapters/tiktok.py +535 -0
  14. socialctl/adapters/youtube.py +331 -0
  15. socialctl/agent_errors.py +176 -0
  16. socialctl/agent_guide.py +23 -0
  17. socialctl/approval_cli.py +27 -0
  18. socialctl/auth.py +139 -0
  19. socialctl/authflow.py +344 -0
  20. socialctl/brands.py +194 -0
  21. socialctl/capabilities.py +57 -0
  22. socialctl/capabilities.yml +5945 -0
  23. socialctl/cli.py +1766 -0
  24. socialctl/connection_service/__init__.py +0 -0
  25. socialctl/connection_service/__main__.py +33 -0
  26. socialctl/connection_service/app.py +496 -0
  27. socialctl/connection_service/assets/PLATFORM-LOGOS-LICENSE.txt +50 -0
  28. socialctl/connection_service/assets/connection.css +75 -0
  29. socialctl/connection_service/assets/facebook.webp +0 -0
  30. socialctl/connection_service/assets/instagram.svg +194 -0
  31. socialctl/connection_service/assets/socialcli-logo.png +0 -0
  32. socialctl/connection_service/assets/tiktok.png +0 -0
  33. socialctl/connection_service/assets/youtube.svg +1 -0
  34. socialctl/connection_service/landing.py +146 -0
  35. socialctl/connection_service/legacy.py +48 -0
  36. socialctl/connection_service/peer.py +13 -0
  37. socialctl/connection_service/providers.py +324 -0
  38. socialctl/connection_service/settings.py +76 -0
  39. socialctl/connection_service/store.py +124 -0
  40. socialctl/connections/__init__.py +0 -0
  41. socialctl/connections/account_cli.py +438 -0
  42. socialctl/connections/cli.py +379 -0
  43. socialctl/connections/client.py +126 -0
  44. socialctl/connections/credential_cli.py +224 -0
  45. socialctl/connections/credential_store.py +49 -0
  46. socialctl/connections/file_store.py +193 -0
  47. socialctl/connections/keychain.py +78 -0
  48. socialctl/connections/meta_user.py +81 -0
  49. socialctl/connections/rotation_cli.py +116 -0
  50. socialctl/connections/saas_client.py +91 -0
  51. socialctl/connections/temporary_media.py +62 -0
  52. socialctl/delivery_options.py +70 -0
  53. socialctl/engagement.py +200 -0
  54. socialctl/engagement_cli.py +50 -0
  55. socialctl/executor.py +135 -0
  56. socialctl/formatter.py +291 -0
  57. socialctl/hosted_media.py +152 -0
  58. socialctl/identity.py +200 -0
  59. socialctl/inbox.py +298 -0
  60. socialctl/inbox_cli.py +46 -0
  61. socialctl/inventory.py +155 -0
  62. socialctl/inventory_cli.py +171 -0
  63. socialctl/inventory_sources.py +101 -0
  64. socialctl/management/__init__.py +1 -0
  65. socialctl/management/batch_cli.py +115 -0
  66. socialctl/management/batches.py +325 -0
  67. socialctl/management/changes.py +485 -0
  68. socialctl/management/cli.py +227 -0
  69. socialctl/management/comments_cli.py +352 -0
  70. socialctl/management/community_batch_cli.py +63 -0
  71. socialctl/management/community_batches.py +294 -0
  72. socialctl/management/community_changes.py +370 -0
  73. socialctl/management/community_cli.py +142 -0
  74. socialctl/management/community_schema.py +119 -0
  75. socialctl/management/facebook_groups.py +1118 -0
  76. socialctl/management/groups_cli.py +274 -0
  77. socialctl/management/inbox_readers.py +87 -0
  78. socialctl/management/meta_changes.py +197 -0
  79. socialctl/management/meta_cli.py +342 -0
  80. socialctl/management/meta_client.py +631 -0
  81. socialctl/management/meta_comment_inbox.py +565 -0
  82. socialctl/management/meta_comments.py +587 -0
  83. socialctl/management/meta_errors.py +59 -0
  84. socialctl/management/meta_pagination.py +52 -0
  85. socialctl/management/meta_schema.py +128 -0
  86. socialctl/management/meta_webhooks.py +147 -0
  87. socialctl/management/metadata_parts.py +144 -0
  88. socialctl/management/owned_assets.py +85 -0
  89. socialctl/management/owned_changes.py +470 -0
  90. socialctl/management/owned_cli.py +128 -0
  91. socialctl/management/owned_reorder.py +62 -0
  92. socialctl/management/owned_schema.py +368 -0
  93. socialctl/management/reply_context.py +24 -0
  94. socialctl/management/reply_evidence.py +80 -0
  95. socialctl/management/resource_changes.py +407 -0
  96. socialctl/management/resource_cli.py +136 -0
  97. socialctl/management/snippet_readback.py +26 -0
  98. socialctl/management/stories.py +713 -0
  99. socialctl/management/stories_cli.py +172 -0
  100. socialctl/management/youtube.py +323 -0
  101. socialctl/management/youtube_community.py +326 -0
  102. socialctl/management/youtube_metadata.py +122 -0
  103. socialctl/management/youtube_owned.py +259 -0
  104. socialctl/management/youtube_resources.py +305 -0
  105. socialctl/media.py +269 -0
  106. socialctl/media_cli.py +183 -0
  107. socialctl/media_ingress.py +211 -0
  108. socialctl/media_preflight.py +114 -0
  109. socialctl/media_registry.py +301 -0
  110. socialctl/media_requirements.py +52 -0
  111. socialctl/metricas/__init__.py +6 -0
  112. socialctl/metricas/almacen.py +133 -0
  113. socialctl/metricas/analytics_changes.py +295 -0
  114. socialctl/metricas/analytics_cli.py +191 -0
  115. socialctl/metricas/analytics_client.py +304 -0
  116. socialctl/metricas/analytics_ingest.py +260 -0
  117. socialctl/metricas/analytics_schema.py +174 -0
  118. socialctl/metricas/base.py +147 -0
  119. socialctl/metricas/facebook.py +418 -0
  120. socialctl/metricas/insights.py +227 -0
  121. socialctl/metricas/instagram.py +133 -0
  122. socialctl/metricas/modelos.py +87 -0
  123. socialctl/metricas/piezas.py +134 -0
  124. socialctl/metricas/reach.py +94 -0
  125. socialctl/metricas/reporting_client.py +112 -0
  126. socialctl/metricas/resumen.py +251 -0
  127. socialctl/metricas/slugs.py +89 -0
  128. socialctl/metricas/tiktok.py +185 -0
  129. socialctl/metricas/youtube.py +403 -0
  130. socialctl/migration_cli.py +60 -0
  131. socialctl/migration_files.py +68 -0
  132. socialctl/models.py +150 -0
  133. socialctl/native_schedule/__init__.py +13 -0
  134. socialctl/native_schedule/api_delivery.py +15 -0
  135. socialctl/native_schedule/api_reconcile.py +40 -0
  136. socialctl/native_schedule/approval.py +160 -0
  137. socialctl/native_schedule/cadence.py +73 -0
  138. socialctl/native_schedule/cli.py +510 -0
  139. socialctl/native_schedule/dispatcher.py +120 -0
  140. socialctl/native_schedule/facebook.py +112 -0
  141. socialctl/native_schedule/facebook_reels.py +140 -0
  142. socialctl/native_schedule/first_comment.py +181 -0
  143. socialctl/native_schedule/handoff.py +193 -0
  144. socialctl/native_schedule/instagram_delivery.py +224 -0
  145. socialctl/native_schedule/instagram_runtime.py +66 -0
  146. socialctl/native_schedule/inventory.py +70 -0
  147. socialctl/native_schedule/migration.py +310 -0
  148. socialctl/native_schedule/models.py +103 -0
  149. socialctl/native_schedule/reconcile.py +83 -0
  150. socialctl/native_schedule/remote_data.py +177 -0
  151. socialctl/native_schedule/report.py +151 -0
  152. socialctl/native_schedule/store.py +560 -0
  153. socialctl/native_schedule/verification.py +32 -0
  154. socialctl/native_schedule/youtube.py +339 -0
  155. socialctl/operation_output.py +87 -0
  156. socialctl/plantillas/accounts.yml +33 -0
  157. socialctl/plantillas/brand.md +95 -0
  158. socialctl/plantillas/estrategia.md +64 -0
  159. socialctl/postfile.py +240 -0
  160. socialctl/preflight.py +73 -0
  161. socialctl/publication_policy.py +34 -0
  162. socialctl/publication_status.py +75 -0
  163. socialctl/publication_steps.py +398 -0
  164. socialctl/publish_output.py +96 -0
  165. socialctl/publisher.py +445 -0
  166. socialctl/queue_migration.py +302 -0
  167. socialctl/read_budget.py +224 -0
  168. socialctl/read_reports.py +91 -0
  169. socialctl/rutas.py +103 -0
  170. socialctl/saas/__init__.py +1 -0
  171. socialctl/saas/access.py +75 -0
  172. socialctl/saas/auth.py +213 -0
  173. socialctl/saas/backup.py +124 -0
  174. socialctl/saas/billing.py +327 -0
  175. socialctl/saas/billing_enrollment.py +1237 -0
  176. socialctl/saas/billing_enrollment_schema.py +519 -0
  177. socialctl/saas/billing_inventory.py +775 -0
  178. socialctl/saas/billing_reconcile.py +127 -0
  179. socialctl/saas/billing_routes.py +27 -0
  180. socialctl/saas/budget.py +66 -0
  181. socialctl/saas/connections.py +374 -0
  182. socialctl/saas/consent.py +52 -0
  183. socialctl/saas/lifecycle.py +341 -0
  184. socialctl/saas/mail.py +89 -0
  185. socialctl/saas/maintenance.py +68 -0
  186. socialctl/saas/meta_user.py +298 -0
  187. socialctl/saas/meta_webhooks.py +358 -0
  188. socialctl/saas/migration.py +107 -0
  189. socialctl/saas/models.py +8 -0
  190. socialctl/saas/operations.py +102 -0
  191. socialctl/saas/pages.py +126 -0
  192. socialctl/saas/pairing.py +158 -0
  193. socialctl/saas/permissions.py +57 -0
  194. socialctl/saas/reminders.py +128 -0
  195. socialctl/saas/renewal.py +74 -0
  196. socialctl/saas/restore_guard.py +229 -0
  197. socialctl/saas/revocation.py +97 -0
  198. socialctl/saas/rotation.py +114 -0
  199. socialctl/saas/routes.py +221 -0
  200. socialctl/saas/settings.py +34 -0
  201. socialctl/saas/store.py +106 -0
  202. socialctl/saas/temporary_media.py +245 -0
  203. socialctl/saas/webhooks.py +76 -0
  204. socialctl/schedule_remote.py +262 -0
  205. socialctl/schedule_sqlite.py +44 -0
  206. socialctl/scheduler.py +581 -0
  207. socialctl/scheduler_launchd.py +319 -0
  208. socialctl/scheduler_systemd.py +167 -0
  209. socialctl/source_links.py +119 -0
  210. socialctl/ui/__init__.py +1 -0
  211. socialctl/ui/assets/agent-manual.md +850 -0
  212. socialctl/ui/assets/agents.md +27 -0
  213. socialctl/ui/assets/components.css +20 -0
  214. socialctl/ui/assets/connection-layout.css +53 -0
  215. socialctl/ui/assets/otp.template.html +10 -0
  216. socialctl/ui/assets/tokens.css +1 -0
  217. socialctl/ui/assets/trial.template.html +11 -0
  218. socialctl/ui/assets/web-layout.css +38 -0
  219. socialctl/ui/email/otp.html +10 -0
  220. socialctl/ui/email/trial.html +11 -0
  221. socialctl/upload_consent.py +13 -0
  222. socialctl/workspace.py +8 -0
  223. socialctl/youtube_data.py +471 -0
  224. socialctl/youtube_data_service.py +225 -0
@@ -0,0 +1,142 @@
1
+ """Named community reads and exact approved intents; audit never writes."""
2
+ import json
3
+ from pathlib import Path
4
+
5
+ import httpx
6
+ import typer
7
+
8
+ from socialctl.approval_cli import read_approval
9
+
10
+ from socialctl.read_budget import active_read_budget, bounded_read, read_client
11
+
12
+ from socialctl.management.changes import ChangeError
13
+ from socialctl.operation_output import OperationOutput
14
+ from socialctl.management.community_schema import load_edit
15
+ from socialctl.management.community_changes import CommunityStore, prepare_community, apply_community, reconcile_community
16
+ from socialctl.management.youtube_community import YouTubeCommunityClient
17
+
18
+
19
+ def make_http_client():
20
+ budget = active_read_budget.get()
21
+ if budget is not None:
22
+ return read_client(budget, progress=lambda: typer.echo('Reading provider data...', err=True))
23
+ return httpx.Client(timeout=60.0, follow_redirects=False)
24
+
25
+
26
+ def _json(value):
27
+ return json.dumps(value, ensure_ascii=False, indent=2, sort_keys=True)
28
+
29
+
30
+ def render_preview(change):
31
+ return ("DRY-RUN — FULL PREVIEW of YouTube community\n" + _json(change.model_dump(mode="json"))
32
+ + "\nExact approval of actor, target, text, reason and each displayed effect. No --yes option."
33
+ + f"\nExact fingerprint for approval: {change.fingerprint}")
34
+
35
+
36
+ def register(content_app, default_root):
37
+ from socialctl.management.cli import _brand, _fail
38
+ community = typer.Typer(help="YouTube community: bounded reads and approved explicit interactions.")
39
+ content_app.add_typer(community, name="youtube-community")
40
+
41
+ def read(root, brand, callback, timeout, json_output):
42
+ from socialctl.read_reports import load_read_brand, annotate_partial_read
43
+ selected = load_read_brand(root, brand) if json_output else _brand(root, brand)
44
+ try:
45
+ with bounded_read(timeout), make_http_client() as http:
46
+ result = callback(YouTubeCommunityClient(selected, http))
47
+ except (ChangeError, OSError, ValueError, httpx.HTTPError) as exc:
48
+ from socialctl.read_reports import fail_read
49
+ fail_read(exc, json_output=json_output)
50
+ typer.echo(_json(annotate_partial_read(result)))
51
+ if isinstance(result, dict) and result.get("complete") is False:
52
+ raise typer.Exit(1)
53
+
54
+ @community.command("threads-list")
55
+ def threads_list(video_id: str, brand: str = typer.Option(..., "--brand"), root: Path = typer.Option(default_root, "--root"),
56
+ moderation_status: str = typer.Option("published", "--moderation-status"), max_pages: int = typer.Option(100, "--max-pages", min=1, max=100), timeout: float = typer.Option(120, "--timeout"), json_output: bool = typer.Option(False, "--json", help="Explicit JSON mode (already the default).")):
57
+ read(root, brand, lambda c: c.list_threads(video_id, moderation_status=moderation_status, max_pages=max_pages), timeout=timeout, json_output=json_output)
58
+
59
+ @community.command("replies-list")
60
+ def replies_list(video_id: str, thread_id: str, parent_id: str, brand: str = typer.Option(..., "--brand"), root: Path = typer.Option(default_root, "--root"),
61
+ max_pages: int = typer.Option(100, "--max-pages", min=1, max=100), timeout: float = typer.Option(120, "--timeout"), json_output: bool = typer.Option(False, "--json", help="Explicit JSON mode (already the default).")):
62
+ read(root, brand, lambda c: c.list_replies(video_id, thread_id, parent_id, max_pages=max_pages), timeout=timeout, json_output=json_output)
63
+
64
+ @community.command("comment-show")
65
+ def comment_show(video_id: str, thread_id: str, comment_id: str, brand: str = typer.Option(..., "--brand"), root: Path = typer.Option(default_root, "--root"),
66
+ parent_id: str | None = typer.Option(None, "--parent-id"), timeout: float = typer.Option(120, "--timeout"), json_output: bool = typer.Option(False, "--json", help="Explicit JSON mode (already the default).")):
67
+ read(root, brand, lambda c: c.find_comment(video_id, thread_id, comment_id, parent_id), timeout=timeout, json_output=json_output)
68
+
69
+ @community.command("subscriptions-list")
70
+ def subscriptions_list(brand: str = typer.Option(..., "--brand"), root: Path = typer.Option(default_root, "--root"),
71
+ max_pages: int = typer.Option(100, "--max-pages", min=1, max=100), timeout: float = typer.Option(120, "--timeout"), json_output: bool = typer.Option(False, "--json", help="Explicit JSON mode (already the default).")):
72
+ read(root, brand, lambda c: c.list_subscriptions(max_pages=max_pages), timeout=timeout, json_output=json_output)
73
+
74
+ @community.command("rating-show")
75
+ def rating_show(video_id: str, brand: str = typer.Option(..., "--brand"), root: Path = typer.Option(default_root, "--root"), timeout: float = typer.Option(120, "--timeout"), json_output: bool = typer.Option(False, "--json", help="Explicit JSON mode (already the default).")):
76
+ read(root, brand, lambda c: c.get_rating(video_id), timeout=timeout, json_output=json_output)
77
+
78
+ @community.command("search")
79
+ def search(query: str, brand: str = typer.Option(..., "--brand"), root: Path = typer.Option(default_root, "--root"),
80
+ kind: str = typer.Option("video", "--type"), order: str = typer.Option("relevance", "--order"),
81
+ channel_id: str | None = typer.Option(None, "--channel-id"), max_pages: int = typer.Option(100, "--max-pages", min=1, max=100), timeout: float = typer.Option(120, "--timeout"), json_output: bool = typer.Option(False, "--json", help="Explicit JSON mode (already the default).")):
82
+ read(root, brand, lambda c: c.search(query, kind=kind, order=order, channel_id=channel_id, max_pages=max_pages), timeout=timeout, json_output=json_output)
83
+
84
+ @community.command("activities-list")
85
+ def activities_list(brand: str = typer.Option(..., "--brand"), root: Path = typer.Option(default_root, "--root"),
86
+ published_after: str | None = typer.Option(None, "--published-after"), published_before: str | None = typer.Option(None, "--published-before"),
87
+ max_pages: int = typer.Option(100, "--max-pages", min=1, max=100), timeout: float = typer.Option(120, "--timeout"), json_output: bool = typer.Option(False, "--json", help="Explicit JSON mode (already the default).")):
88
+ read(root, brand, lambda c: c.activities(published_after=published_after, published_before=published_before, max_pages=max_pages), timeout=timeout, json_output=json_output)
89
+
90
+ @community.command("catalog")
91
+ def catalog(name: str, brand: str = typer.Option(..., "--brand"), root: Path = typer.Option(default_root, "--root"),
92
+ language: str = typer.Option("en", "--language"), region: str | None = typer.Option(None, "--region"), timeout: float = typer.Option(120, "--timeout"), json_output: bool = typer.Option(False, "--json", help="Explicit JSON mode (already the default).")):
93
+ read(root, brand, lambda c: c.catalog(name, language=language, region=region), timeout=timeout, json_output=json_output)
94
+
95
+ @community.command("prepare")
96
+ def prepare(file: Path = typer.Option(..., "--file"), brand: str = typer.Option(..., "--brand"), root: Path = typer.Option(default_root, "--root"),
97
+ dry_run: bool = typer.Option(False, "--dry-run")):
98
+ selected = _brand(root, brand)
99
+ try:
100
+ edit = load_edit(file)
101
+ with make_http_client() as http:
102
+ change = prepare_community(YouTubeCommunityClient(selected, http), CommunityStore(selected.raiz), edit)
103
+ except (ChangeError, OSError, ValueError) as exc:
104
+ _fail(str(exc))
105
+ typer.echo(render_preview(change))
106
+
107
+ @community.command("status")
108
+ def status(change_id: str, brand: str = typer.Option(..., "--brand"), root: Path = typer.Option(default_root, "--root"),
109
+ json_output: bool = typer.Option(False, "--json", help="JSON result with recovery action.")):
110
+ with OperationOutput(json_output, read_only=True) as output:
111
+ selected = output.brand(root, brand)
112
+ change = CommunityStore(selected.raiz).load(change_id)
113
+ output.emit(change)
114
+
115
+ @community.command("apply")
116
+ def apply(change_id: str, brand: str = typer.Option(..., "--brand"), root: Path = typer.Option(default_root, "--root"),
117
+ json_output: bool = typer.Option(False, "--json", help="JSON result; full preview and exact approval prompt on stderr."),
118
+ approval_fingerprint: str | None = typer.Option(None, "--approval-fingerprint", "--approval", "--approval-digest", help="Exact fingerprint from the approved complete preview."),
119
+ approval_stdin: bool = typer.Option(False, "--approval-stdin", help="Explicitly read one approval line from stdin.")):
120
+ with OperationOutput(json_output) as output:
121
+ selected = output.brand(root, brand)
122
+ store = CommunityStore(selected.raiz)
123
+ change = store.load(change_id)
124
+ approval = output.approve(change, render_preview(change), approval_fingerprint, approval_stdin=approval_stdin)
125
+ with make_http_client() as http:
126
+ client = YouTubeCommunityClient(selected, http)
127
+ output.begin_write()
128
+ result = apply_community(client, store, change_id, approval)
129
+ output.emit(result)
130
+ if result.status != "verified":
131
+ raise typer.Exit(1)
132
+
133
+ @community.command("reconcile")
134
+ def reconcile(change_id: str, brand: str = typer.Option(..., "--brand"), root: Path = typer.Option(default_root, "--root"),
135
+ json_output: bool = typer.Option(False, "--json", help="JSON result with recovery action; no remote writes.")):
136
+ with OperationOutput(json_output, read_only=True) as output:
137
+ selected = output.brand(root, brand)
138
+ store = CommunityStore(selected.raiz)
139
+ output.change_id = store.load(change_id).id
140
+ with make_http_client() as http:
141
+ result = reconcile_community(YouTubeCommunityClient(selected, http), store, change_id)
142
+ output.emit(result)
@@ -0,0 +1,119 @@
1
+ """Exact supplied community intents; no generic endpoints or automatic targets."""
2
+ from pathlib import Path
3
+ from typing import Literal
4
+
5
+ import yaml
6
+ from pydantic import BaseModel, ConfigDict, Field, ValidationError, model_validator
7
+
8
+ from socialctl.management.youtube_resources import ResourceError, resource_id
9
+
10
+
11
+ def opaque_id(value):
12
+ # Comment/reply/thread/subscription IDs are opaque (replies can contain dots).
13
+ # Always query/JSON data, never URL paths; comma is a provider batch separator.
14
+ if (not isinstance(value, str) or not 0 < len(value) <= 512 or "://" in value
15
+ or any(not c.isprintable() or c.isspace() or c in ",?#" for c in value)):
16
+ raise ResourceError("invalid opaque ID")
17
+ return value
18
+
19
+
20
+ class CommentTarget(BaseModel):
21
+ model_config = ConfigDict(extra="forbid", strict=True)
22
+ video_id: str
23
+ thread_id: str
24
+ comment_id: str
25
+ parent_id: str | None = None
26
+
27
+ @model_validator(mode="before")
28
+ @classmethod
29
+ def no_null(cls, raw):
30
+ if not isinstance(raw, dict) or any(v is None for v in raw.values()):
31
+ raise ResourceError("target requires supplied nonnull fields")
32
+ return raw
33
+
34
+ @model_validator(mode="after")
35
+ def validate_ids(self):
36
+ resource_id(self.video_id)
37
+ for value in (self.thread_id, self.comment_id, self.parent_id):
38
+ if value is not None:
39
+ opaque_id(value)
40
+ return self
41
+
42
+
43
+ BASE = {"video_id", "thread_id", "comment_id"}
44
+ ACTIONS = {
45
+ "add": ({"video_id", "text"}, {"video_id", "text"}),
46
+ "reply": ({"video_id", "thread_id", "parent_id", "text"}, {"video_id", "thread_id", "parent_id", "text"}),
47
+ "edit": (BASE | {"text"}, BASE | {"text", "parent_id"}),
48
+ "delete": (BASE, BASE | {"parent_id"}),
49
+ "moderate": ({"targets", "moderation_status", "ban_author"}, {"targets", "moderation_status", "ban_author"}),
50
+ "subscribe": ({"channel_id"}, {"channel_id"}),
51
+ "unsubscribe": ({"channel_id", "subscription_id"}, {"channel_id", "subscription_id"}),
52
+ "rate": ({"video_id", "rating"}, {"video_id", "rating"}),
53
+ "report-abuse": ({"video_id", "reason_id"}, {"video_id", "reason_id", "secondary_reason_id", "comments", "language"}),
54
+ }
55
+
56
+
57
+ class CommunityEdit(BaseModel):
58
+ model_config = ConfigDict(extra="forbid", strict=True)
59
+ version: Literal[1] = 1
60
+ action: str
61
+ video_id: str | None = None
62
+ thread_id: str | None = None
63
+ comment_id: str | None = None
64
+ parent_id: str | None = None
65
+ text: str | None = Field(default=None, min_length=1, max_length=10000)
66
+ channel_id: str | None = None
67
+ subscription_id: str | None = None
68
+ targets: list[CommentTarget] | None = Field(default=None, min_length=1, max_length=50)
69
+ moderation_status: Literal["heldForReview", "published", "rejected"] | None = None
70
+ ban_author: bool | None = None
71
+ rating: Literal["like", "dislike", "none"] | None = None
72
+ reason_id: str | None = None
73
+ secondary_reason_id: str | None = None
74
+ comments: str | None = Field(default=None, max_length=10000)
75
+ language: str | None = Field(default=None, min_length=1, max_length=35)
76
+
77
+ @model_validator(mode="before")
78
+ @classmethod
79
+ def exact_fields(cls, raw):
80
+ if not isinstance(raw, dict) or not isinstance(raw.get("action"), str) or raw["action"] not in ACTIONS:
81
+ raise ResourceError("unsupported action; spam/pin/heart/polls/cards/end-screens/related Shorts are not emulated")
82
+ required, allowed = ACTIONS[raw["action"]]
83
+ present = set(raw) - {"version", "action"}
84
+ if required - present or present - allowed or any(raw[k] is None for k in present):
85
+ raise ResourceError("missing/extra/null action fields")
86
+ for key in present & {"video_id", "channel_id"}:
87
+ resource_id(raw[key])
88
+ for key in present & {"thread_id", "comment_id", "parent_id", "subscription_id", "reason_id", "secondary_reason_id"}:
89
+ opaque_id(raw[key])
90
+ if "text" in present and (not isinstance(raw["text"], str) or not raw["text"].strip()):
91
+ raise ResourceError("supplied text is empty")
92
+ return raw
93
+
94
+ @model_validator(mode="after")
95
+ def moderation(self):
96
+ if self.action == "moderate":
97
+ if len({t.comment_id for t in self.targets}) != len(self.targets):
98
+ raise ResourceError("duplicate moderation IDs")
99
+ if self.ban_author and self.moderation_status != "rejected":
100
+ raise ResourceError("banAuthor accepted only with rejected")
101
+ return self
102
+
103
+
104
+ def edit_raw(edit):
105
+ return edit.model_dump(mode="python", exclude_none=True)
106
+
107
+
108
+ def validate_edit(edit):
109
+ try:
110
+ return CommunityEdit.model_validate(edit_raw(edit) if isinstance(edit, CommunityEdit) else edit)
111
+ except ValidationError as exc:
112
+ raise ResourceError(f"invalid community proposal: {exc}") from None
113
+
114
+
115
+ def load_edit(path):
116
+ try:
117
+ return validate_edit(yaml.safe_load(Path(path).read_text(encoding="utf-8")))
118
+ except (OSError, yaml.YAMLError):
119
+ raise ResourceError("failed to read supplied YAML") from None