1322-python 0.1.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.
@@ -0,0 +1,146 @@
1
+ """Instagram: REST management API + WebSocket post payload.
2
+
3
+ Ground truth: https://1322.io/docs ("Instagram" reference).
4
+
5
+ - REST base: https://1322.io
6
+ - WebSocket: wss://1322.io/IG?key={api_key} -- the REST API key doubles as
7
+ the WebSocket key, there is no separate ws_path/ws_key round trip.
8
+ - Auth: header Authorization: Bearer <key> or X-API-Key: <key> (REST).
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ from dataclasses import dataclass
14
+ from typing import Any, Literal, TypedDict
15
+
16
+ from ._base import BaseAdapter
17
+
18
+ REST_BASE = "https://1322.io"
19
+ WS_URL_TEMPLATE = "wss://1322.io/IG?key={api_key}"
20
+
21
+ PostType = Literal["post", "reel", "story", "carousel"]
22
+ MediaKind = Literal["image", "video"]
23
+ MediaKindV2 = Literal["image", "video_cover", "video"]
24
+
25
+
26
+ class InstagramLocation(TypedDict, total=False):
27
+ name: str
28
+ lat: float
29
+ lng: float
30
+
31
+
32
+ class InstagramMediaAttachment(TypedDict, total=False):
33
+ kind: MediaKind
34
+ kind_v2: MediaKindV2
35
+ media_role: MediaKindV2
36
+ is_video_cover: bool
37
+ media_index: int
38
+ carousel_index: int
39
+ video_index: int
40
+ cover_media_index: int
41
+ cover_url: str
42
+ covers_video_media_index: int
43
+ covers_video_url: str
44
+ url: str
45
+ width: int
46
+ height: int
47
+ created_at: str
48
+
49
+
50
+ @dataclass(slots=True)
51
+ class InstagramPostEvent:
52
+ """An ``InstagramPost`` WebSocket payload (raw frame, no envelope)."""
53
+
54
+ platform: str
55
+ username: str
56
+ display_name: str
57
+ user_avatar: str
58
+ user_followers: int
59
+ user_id: str
60
+ key: str
61
+ timestamp: str
62
+ seen_at: str
63
+ text: str
64
+ post_type: PostType
65
+ media_count: int | None
66
+ hashtags: list[str] | None
67
+ mentions: list[str] | None
68
+ media: list[InstagramMediaAttachment]
69
+ is_collab: bool | None
70
+ collab_with: list[str] | None
71
+ is_pinned: bool | None
72
+ is_paid_partnership: bool | None
73
+ location: InstagramLocation | None
74
+ raw: dict[str, Any]
75
+
76
+
77
+ def parse_instagram_event(raw: dict[str, Any]) -> InstagramPostEvent:
78
+ """Parse one decoded Instagram WebSocket frame (a full ``InstagramPost``)."""
79
+ return InstagramPostEvent(
80
+ platform=raw.get("platform", "instagram"),
81
+ username=raw.get("username", ""),
82
+ display_name=raw.get("display_name", ""),
83
+ user_avatar=raw.get("user_avatar", ""),
84
+ user_followers=raw.get("user_followers", 0),
85
+ user_id=raw.get("user_id", ""),
86
+ key=raw.get("key", ""),
87
+ timestamp=raw.get("timestamp", ""),
88
+ seen_at=raw.get("seen_at", ""),
89
+ text=raw.get("text", ""),
90
+ post_type=raw.get("post_type", "post"),
91
+ media_count=raw.get("media_count"),
92
+ hashtags=raw.get("hashtags"),
93
+ mentions=raw.get("mentions"),
94
+ media=raw.get("media") or [],
95
+ is_collab=raw.get("is_collab"),
96
+ collab_with=raw.get("collab_with"),
97
+ is_pinned=raw.get("is_pinned"),
98
+ is_paid_partnership=raw.get("is_paid_partnership"),
99
+ location=raw.get("location"),
100
+ raw=raw,
101
+ )
102
+
103
+
104
+ class InstagramAdapter(BaseAdapter):
105
+ """Instagram connection + REST management API."""
106
+
107
+ rest_base = REST_BASE
108
+
109
+ def _ws_url(self) -> str:
110
+ return WS_URL_TEMPLATE.format(api_key=self.api_key)
111
+
112
+ def _ws_headers(self) -> dict[str, str]:
113
+ return {}
114
+
115
+ def _rest_headers(self) -> dict[str, str]:
116
+ return {"Authorization": f"Bearer {self.api_key}"}
117
+
118
+ def _parse(self, raw: dict[str, Any]) -> InstagramPostEvent:
119
+ return parse_instagram_event(raw)
120
+
121
+ # -- REST: GET /v1/health, GET /v1/limits, GET /v1/list, POST /v1/track,
122
+ # POST /v1/untrack
123
+
124
+ async def health(self) -> dict[str, Any]:
125
+ return await self._request("GET", "/v1/health")
126
+
127
+ async def limits(self) -> dict[str, Any]:
128
+ return await self._request("GET", "/v1/limits")
129
+
130
+ async def list_tracked(self) -> dict[str, Any]:
131
+ return await self._request("GET", "/v1/list")
132
+
133
+ async def track(self, username: str) -> dict[str, Any]:
134
+ return await self._request("POST", "/v1/track", json={"username": username})
135
+
136
+ async def untrack(self, username: str) -> dict[str, Any]:
137
+ return await self._request("POST", "/v1/untrack", json={"username": username})
138
+
139
+
140
+ __all__ = [
141
+ "InstagramAdapter",
142
+ "InstagramPostEvent",
143
+ "InstagramMediaAttachment",
144
+ "InstagramLocation",
145
+ "parse_instagram_event",
146
+ ]
@@ -0,0 +1,150 @@
1
+ """News: REST management API + WebSocket article payload.
2
+
3
+ Ground truth: https://1322.io/docs ("News Feed" reference).
4
+
5
+ - REST base: https://newsfeed.1322.io
6
+ - WebSocket: wss://newsfeed.1322.io/{ws_path}?key={ws_key} -- path and key
7
+ come from GET /v1/dashboard. If you don't pass them explicitly, the
8
+ adapter fetches /v1/dashboard once before connecting.
9
+ - Auth: header X-Api-Key (REST). WebSocket: key= query parameter.
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ from dataclasses import dataclass
15
+ from typing import Any, Literal, TypedDict
16
+
17
+ from ._base import BaseAdapter
18
+
19
+ REST_BASE = "https://newsfeed.1322.io"
20
+ WS_HOST = "newsfeed.1322.io"
21
+
22
+
23
+ class NewsMediaAttachment(TypedDict, total=False):
24
+ url: str
25
+ type: Literal["image", "video"]
26
+ caption: str
27
+
28
+
29
+ @dataclass(slots=True)
30
+ class NewsArticleEvent:
31
+ """A ``WebSocketNewsFeedPayload`` frame (raw, no envelope)."""
32
+
33
+ feed: str
34
+ guid: str
35
+ url: str
36
+ title: str
37
+ publish_time: str
38
+ modified_time: str | None
39
+ primary_category: str
40
+ categories: list[str]
41
+ author: str
42
+ keywords: list[str]
43
+ description: str
44
+ summary: str
45
+ media: list[NewsMediaAttachment]
46
+ full_text: str
47
+ language: str | None
48
+ copyright: str | None
49
+ event_type: Literal["LIVE_POST"]
50
+ sent_time: str
51
+ raw: dict[str, Any]
52
+
53
+
54
+ def parse_news_event(raw: dict[str, Any]) -> NewsArticleEvent:
55
+ """Parse one decoded News WebSocket frame (a full article payload)."""
56
+ return NewsArticleEvent(
57
+ feed=raw.get("feed", ""),
58
+ guid=raw.get("guid", ""),
59
+ url=raw.get("url", ""),
60
+ title=raw.get("title", ""),
61
+ publish_time=raw.get("publish_time", ""),
62
+ modified_time=raw.get("modified_time"),
63
+ primary_category=raw.get("primary_category", ""),
64
+ categories=raw.get("categories") or [],
65
+ author=raw.get("author", ""),
66
+ keywords=raw.get("keywords") or [],
67
+ description=raw.get("description", ""),
68
+ summary=raw.get("summary", ""),
69
+ media=raw.get("media") or [],
70
+ full_text=raw.get("full_text", ""),
71
+ language=raw.get("language"),
72
+ copyright=raw.get("copyright"),
73
+ event_type=raw.get("_event_type", "LIVE_POST"),
74
+ sent_time=raw.get("_sent_time", ""),
75
+ raw=raw,
76
+ )
77
+
78
+
79
+ class NewsAdapter(BaseAdapter):
80
+ """News connection + REST management API."""
81
+
82
+ rest_base = REST_BASE
83
+
84
+ def __init__(
85
+ self,
86
+ api_key: str,
87
+ *,
88
+ ws_path: str | None = None,
89
+ ws_key: str | None = None,
90
+ ws_url: str | None = None,
91
+ **kwargs: Any,
92
+ ) -> None:
93
+ super().__init__(api_key, **kwargs)
94
+ self.ws_path = ws_path
95
+ self.ws_key = ws_key
96
+ self._ws_url_override = ws_url
97
+
98
+ async def _resolve_ws(self) -> None:
99
+ if self._ws_url_override:
100
+ return
101
+ if self.ws_path and self.ws_key:
102
+ return
103
+ data = await self.dashboard()
104
+ self.ws_path = self.ws_path or data.get("ws_path")
105
+ self.ws_key = self.ws_key or data.get("ws_key")
106
+
107
+ def _ws_url(self) -> str:
108
+ if self._ws_url_override:
109
+ return self._ws_url_override
110
+ if not (self.ws_path and self.ws_key):
111
+ raise RuntimeError(
112
+ "ws_path/ws_key not resolved yet; call 'await adapter._resolve_ws()' "
113
+ "(done automatically by stream()), or pass ws_path/ws_key/ws_url "
114
+ "explicitly."
115
+ )
116
+ path = self.ws_path if self.ws_path.startswith("/") else f"/{self.ws_path}"
117
+ return f"wss://{WS_HOST}{path}?key={self.ws_key}"
118
+
119
+ def _ws_headers(self) -> dict[str, str]:
120
+ return {}
121
+
122
+ def _rest_headers(self) -> dict[str, str]:
123
+ return {"X-Api-Key": self.api_key}
124
+
125
+ def _parse(self, raw: dict[str, Any]) -> NewsArticleEvent:
126
+ return parse_news_event(raw)
127
+
128
+ async def stream(self): # type: ignore[override]
129
+ await self._resolve_ws()
130
+ async for event in super().stream():
131
+ yield event
132
+
133
+ # -- REST: GET /v1/dashboard, POST /v1/subscribe, POST /v1/unsubscribe
134
+
135
+ async def dashboard(self) -> dict[str, Any]:
136
+ return await self._request("GET", "/v1/dashboard")
137
+
138
+ async def subscribe(self, feed: str) -> dict[str, Any]:
139
+ return await self._request("POST", "/v1/subscribe", json={"feed": feed})
140
+
141
+ async def unsubscribe(self, feed: str) -> dict[str, Any]:
142
+ return await self._request("POST", "/v1/unsubscribe", json={"feed": feed})
143
+
144
+
145
+ __all__ = [
146
+ "NewsAdapter",
147
+ "NewsArticleEvent",
148
+ "NewsMediaAttachment",
149
+ "parse_news_event",
150
+ ]
@@ -0,0 +1,189 @@
1
+ """Truth Social: REST management API + WebSocket post payload.
2
+
3
+ Ground truth: https://1322.io/docs ("Truth Social" reference).
4
+
5
+ - REST base: https://truth.1322.io
6
+ - WebSocket: wss://truth.1322.io/{path}?key={ws_key} -- path and WS key are
7
+ provided in your dashboard configuration. Unlike News and Binance Square,
8
+ 1322's docs do not expose a REST endpoint that returns these for Truth
9
+ Social, so callers must supply them (or a full ``ws_url``).
10
+ - Auth: header X-Api-Key (REST). WebSocket: key= query parameter.
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ from dataclasses import dataclass
16
+ from typing import Any, TypedDict
17
+
18
+ from ..exceptions import ConfigurationError
19
+ from ._base import BaseAdapter
20
+
21
+ REST_BASE = "https://truth.1322.io"
22
+ WS_HOST = "truth.1322.io"
23
+
24
+
25
+ class TruthSocialMediaAttachment(TypedDict, total=False):
26
+ kind: str # 'image' | 'video' | 'gifv' | 'audio' | other
27
+ url: str
28
+ poster_url: str
29
+ created_at: str
30
+ source: str # 'own' | 'quoted' | 'nested_quoted'
31
+
32
+
33
+ class TruthSocialAuthor(TypedDict, total=False):
34
+ id: str
35
+ username: str
36
+ display_name: str
37
+ avatar: str
38
+
39
+
40
+ class TruthSocialCard(TypedDict, total=False):
41
+ url: str
42
+ title: str
43
+ description: str
44
+ image: str
45
+
46
+
47
+ class TruthSocialQuotedInner(TypedDict, total=False):
48
+ key: str
49
+ text: str
50
+ url: str
51
+ author: TruthSocialAuthor
52
+
53
+
54
+ class TruthSocialQuoted(TypedDict, total=False):
55
+ key: str
56
+ text: str
57
+ url: str
58
+ author: TruthSocialAuthor
59
+ quoted: TruthSocialQuotedInner
60
+
61
+
62
+ @dataclass(slots=True)
63
+ class TruthPostEvent:
64
+ """A ``TruthSocialPost`` WebSocket payload (raw frame, no envelope/type field)."""
65
+
66
+ platform: str
67
+ username: str
68
+ display_name: str
69
+ user_avatar: str
70
+ user_following: int
71
+ user_followers: int
72
+ user_id: str
73
+ key: str
74
+ timestamp: str
75
+ seen_at: str
76
+ text: str
77
+ is_quote: bool | None
78
+ quoted: TruthSocialQuoted | None
79
+ is_retruth: bool | None
80
+ retruth_of_id: str | None
81
+ retruth_of: TruthSocialAuthor | None
82
+ card: TruthSocialCard | None
83
+ media: list[TruthSocialMediaAttachment]
84
+ raw: dict[str, Any]
85
+
86
+
87
+ def parse_truth_event(raw: dict[str, Any]) -> TruthPostEvent:
88
+ """Parse one decoded Truth Social WebSocket frame.
89
+
90
+ Every frame is a full ``TruthSocialPost`` object; there is no envelope
91
+ or ``type`` discriminator (unlike X or Binance Square).
92
+ """
93
+ return TruthPostEvent(
94
+ platform=raw.get("platform", "truth"),
95
+ username=raw.get("username", ""),
96
+ display_name=raw.get("display_name", ""),
97
+ user_avatar=raw.get("user_avatar", ""),
98
+ user_following=raw.get("user_following", 0),
99
+ user_followers=raw.get("user_followers", 0),
100
+ user_id=raw.get("user_id", ""),
101
+ key=raw.get("key", ""),
102
+ timestamp=raw.get("timestamp", ""),
103
+ seen_at=raw.get("seen_at", ""),
104
+ text=raw.get("text", ""),
105
+ is_quote=raw.get("is_quote"),
106
+ quoted=raw.get("quoted"),
107
+ is_retruth=raw.get("is_retruth"),
108
+ retruth_of_id=raw.get("retruth_of_id"),
109
+ retruth_of=raw.get("retruth_of"),
110
+ card=raw.get("card"),
111
+ media=raw.get("media") or [],
112
+ raw=raw,
113
+ )
114
+
115
+
116
+ class TruthAdapter(BaseAdapter):
117
+ """Truth Social connection + REST management API."""
118
+
119
+ rest_base = REST_BASE
120
+
121
+ def __init__(
122
+ self,
123
+ api_key: str,
124
+ *,
125
+ ws_path: str | None = None,
126
+ ws_key: str | None = None,
127
+ ws_url: str | None = None,
128
+ **kwargs: Any,
129
+ ) -> None:
130
+ super().__init__(api_key, **kwargs)
131
+ self.ws_path = ws_path
132
+ self.ws_key = ws_key
133
+ self._ws_url_override = ws_url
134
+ if ws_url is None and (ws_path is None or ws_key is None):
135
+ raise ConfigurationError(
136
+ "Truth Social needs ws_path and ws_key from your 1322 dashboard "
137
+ "configuration (1322's docs do not publish a fixed WebSocket "
138
+ "path or a REST endpoint that returns these for Truth Social, "
139
+ "unlike News/Binance Square). Pass ws_path+ws_key, or pass a "
140
+ "full ws_url directly. See https://1322.io/docs."
141
+ )
142
+
143
+ def _ws_url(self) -> str:
144
+ if self._ws_url_override:
145
+ return self._ws_url_override
146
+ assert self.ws_path is not None and self.ws_key is not None
147
+ path = self.ws_path if self.ws_path.startswith("/") else f"/{self.ws_path}"
148
+ return f"wss://{WS_HOST}{path}?key={self.ws_key}"
149
+
150
+ def _ws_headers(self) -> dict[str, str]:
151
+ return {}
152
+
153
+ def _rest_headers(self) -> dict[str, str]:
154
+ return {"X-Api-Key": self.api_key}
155
+
156
+ def _parse(self, raw: dict[str, Any]) -> TruthPostEvent:
157
+ return parse_truth_event(raw)
158
+
159
+ # -- REST: GET /health, GET /v1/status, GET /v1/list, GET /v1/limits,
160
+ # POST /v1/track, POST /v1/untrack
161
+
162
+ async def health(self) -> dict[str, Any]:
163
+ return await self._request("GET", "/health")
164
+
165
+ async def status(self) -> dict[str, Any]:
166
+ return await self._request("GET", "/v1/status")
167
+
168
+ async def list_tracked(self) -> list[dict[str, Any]]:
169
+ return await self._request("GET", "/v1/list")
170
+
171
+ async def limits(self) -> dict[str, Any]:
172
+ return await self._request("GET", "/v1/limits")
173
+
174
+ async def track(self, handle: str) -> dict[str, Any]:
175
+ return await self._request("POST", "/v1/track", json={"handle": handle})
176
+
177
+ async def untrack(self, handle: str) -> dict[str, Any]:
178
+ return await self._request("POST", "/v1/untrack", json={"handle": handle})
179
+
180
+
181
+ __all__ = [
182
+ "TruthAdapter",
183
+ "TruthPostEvent",
184
+ "TruthSocialMediaAttachment",
185
+ "TruthSocialAuthor",
186
+ "TruthSocialCard",
187
+ "TruthSocialQuoted",
188
+ "parse_truth_event",
189
+ ]