cakranala 0.0.0-stage → 0.1.0

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.
Files changed (43) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +113 -2
  3. package/bin/cakranala.js +15 -0
  4. package/dist/cli/index.js +23 -0
  5. package/dist/collect/browser.d.ts +24 -0
  6. package/dist/collect/facebook.d.ts +19 -0
  7. package/dist/collect/index.d.ts +66 -0
  8. package/dist/collect/index.js +4 -0
  9. package/dist/collect/instagram.d.ts +20 -0
  10. package/dist/collect/telegram.d.ts +2 -0
  11. package/dist/collect/tiktok.d.ts +28 -0
  12. package/dist/collect/x.d.ts +37 -0
  13. package/dist/collect/youtube.d.ts +14 -0
  14. package/dist/embed/embed.d.ts +13 -0
  15. package/dist/embed/index.d.ts +2 -0
  16. package/dist/embed/index.js +2 -0
  17. package/dist/embed/reason.d.ts +10 -0
  18. package/dist/engine/detect/consensus.d.ts +40 -0
  19. package/dist/engine/detect/forensics.d.ts +20 -0
  20. package/dist/engine/detect/graph.d.ts +44 -0
  21. package/dist/engine/detect/index.d.ts +52 -0
  22. package/dist/engine/detect/layers.d.ts +32 -0
  23. package/dist/engine/evaluate.d.ts +23 -0
  24. package/dist/engine/index.d.ts +13 -0
  25. package/dist/engine/ports.d.ts +10 -0
  26. package/dist/engine/privacy.d.ts +2 -0
  27. package/dist/engine/schema.d.ts +40 -0
  28. package/dist/engine/shield.d.ts +57 -0
  29. package/dist/engine/simulate.d.ts +45 -0
  30. package/dist/engine/text.d.ts +9 -0
  31. package/dist/engine/util.d.ts +26 -0
  32. package/dist/home.d.ts +2 -0
  33. package/dist/index.d.ts +1 -0
  34. package/dist/index.js +17 -0
  35. package/dist/web/apple-touch-icon-5nx1caa0.png +0 -0
  36. package/dist/web/chunk-0p6gbr4g.css +1 -0
  37. package/dist/web/chunk-8rhsy347.js +430 -0
  38. package/dist/web/favicon-vjre31pg.ico +0 -0
  39. package/dist/web/favicon-ze8spk31.svg +1 -0
  40. package/dist/web/index.html +19 -0
  41. package/dist/web/index.js +186 -0
  42. package/package.json +92 -4
  43. package/scenarios/seruni.json +486 -0
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md CHANGED
@@ -1,3 +1,114 @@
1
- # Temporary Holding Version
1
+ # Cakra Nala
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Cakra Nala mendeteksi operasi pengaruh terkoordinasi di media sosial (buzzer bayaran, bot farm, swarm LLM) dari seberapa bergantung akun satu sama lain, lalu menghitung berapa suara independen yang sebenarnya ada di balik sebuah narasi.
4
+
5
+ ```bash
6
+ npm install cakranala
7
+ ```
8
+
9
+ ```js
10
+ import { analyze, simulate } from "cakranala";
11
+ import seruni from "cakranala/scenarios/seruni.json" with { type: "json" };
12
+
13
+ const report = await analyze(simulate(seruni, 1));
14
+
15
+ for (const n of report.narratives) {
16
+ console.log(n.verdict, n.supporters, "akun,", n.sie.value.toFixed(1), "suara independen:", n.sample);
17
+ }
18
+ ```
19
+
20
+ `analyze` menerima dataset berisi akun dan aktivitasnya (`post`, `reply`, `quote`, `reshare`, `comment`) dari platform mana pun. Hasilnya berupa klaster akun terkoordinasi, skor koordinasi per akun, dan narasi beserta dukungan nyatanya. Engine tidak butuh training, label, maupun network, dan berjalan di Node maupun Bun.
21
+
22
+ ## Dashboard
23
+
24
+ Untuk analis, Cakra Nala punya dashboard operasi: pantau target di X, YouTube, Telegram, TikTok, Instagram dan Facebook, lalu hasil deteksi berubah menjadi kasus dengan bukti, graf jaringan, keputusan analis, laporan PDF, ekspor STIX 2.1 dan pemetaan DISARM. Ada juga Perisai, pemeriksa pesan untuk publik.
25
+
26
+ Butuh [Bun](https://bun.sh).
27
+
28
+ ```bash
29
+ bunx cakranala install # model e5, Typst untuk PDF, Chrome untuk Instagram/Facebook
30
+ bunx cakranala serve # http://localhost:3000
31
+ ```
32
+
33
+ Pengunjung pertama membuat akun admin. `bunx cakranala serve --demo` memutar skenario fiktif Pulau Seruni supaya dashboard langsung terisi. `bunx cakranala doctor` menunjukkan fitur mana yang siap dan apa yang kurang.
34
+
35
+ Data disimpan di `./.cakranala`. Kredensial platform dan proxy diatur dari halaman `/credentials`, penjelasannya ada di [docs/platforms.md](docs/platforms.md).
36
+
37
+ ## Mengumpulkan data
38
+
39
+ ```js
40
+ import { createCollector } from "cakranala/collect";
41
+
42
+ const collector = createCollector({ pool });
43
+ const dataset = await collector.collect({ platform: "telegram", kind: "account", target: "kanal_satu", pages: 2 });
44
+ ```
45
+
46
+ `pool` adalah sumber kredensial dengan method `count`, `acquire`, `ok`, `limited`, `failed`, `identity` dan `setIdentity`. Telegram dan YouTube jalan tanpa kredensial. Modul ini butuh Bun.
47
+
48
+ ## Cara kerja
49
+
50
+ Engine membangun graf ketergantungan antar akun dari lima layer bukti:
51
+
52
+ - Co-action: membalas, mengutip, membagikan URL atau tagar yang sama dalam 15 menit
53
+ - Semantic co-framing: makna sama dengan kata berbeda, lewat embedding e5 multibahasa
54
+ - Copypasta: teks hampir identik, lewat n-gram karakter, MinHash dan LSH
55
+ - Rhythm: jam aktif yang sama melebihi peluang
56
+ - Account lifecycle: akun dibuat di hari yang sama dengan pola nama serupa
57
+
58
+ Setiap layer dibandingkan dengan null model yang dibangun dari data itu sendiri. Interaksi diacak di dalam strata waktu pendek, sehingga berita besar yang memicu banyak akun sekaligus tidak dianggap koordinasi. Ambang klaster dipilih dari data agar decoy FDR tetap di bawah 5 persen, dan setiap klaster harus lolos uji permutasi. Verdict narasi memakai klaster yang ditemukan di separuh waktu lainnya, jadi data yang sama tidak menemukan sekaligus memvonis.
59
+
60
+ Dukungan sebuah narasi dihitung sebagai suara independen efektif (SIE):
61
+
62
+ ```
63
+ SIE = organik + Σ n_c / (1 + (n_c − 1) · ρ_c)
64
+ ```
65
+
66
+ dengan `n_c` jumlah akun di klaster `c` dan `ρ_c` korelasi di dalamnya. Seribu akun dari satu klaster yang sangat terkoordinasi bernilai hampir sama dengan satu suara.
67
+
68
+ Layer semantic co-framing aktif kalau `@huggingface/transformers` terpasang (`cakranala install` mengurusnya). Tanpa itu engine tetap berjalan dengan empat layer.
69
+
70
+ ## Akurasi
71
+
72
+ Diuji pada arsip berlabel operasi pengaruh negara yang nyata. Peringkat akun mencapai average precision 0,76 dan 0,94, dibanding 0,22 dan 0,75 kalau akun hanya diurutkan berdasarkan jumlah post. Tidak ada akun kontrol yang masuk klaster, dan tidak ada klaster yang muncul pada data yang waktunya digeser. Pada simulasi, bot farm dan buzzer bayaran hampir semua tertangkap tanpa akun organik ikut ditandai. Swarm LLM yang sabar masih kasus tersulit.
73
+
74
+ ## Konfigurasi
75
+
76
+ Dibaca dari environment atau file `.env` di folder kerja.
77
+
78
+ | Variabel | Fungsi |
79
+ |---|---|
80
+ | `LLM_BASE_URL`, `LLM_MODEL`, `LLM_API_KEY` | Endpoint kompatibel OpenAI untuk judul kasus, ringkasan, laporan dan Perisai. Opsional, bisa model lokal seperti Ollama. |
81
+ | `PSEUDONYM_SECRET` | Kunci HMAC untuk pseudonim akun. Dibuat otomatis kalau kosong. |
82
+ | `CAKRANALA_DATA` | Folder data, default `./.cakranala`. |
83
+ | `CAKRANALA_CACHE` | Folder model, Typst dan Chrome, default `~/.cache/cakranala`. |
84
+ | `TIKTOK_SIDECAR_URL` | Sidecar untuk TikTok, lihat [docs/platforms.md](docs/platforms.md). |
85
+ | `LIBRARY_FEED` | RSS cek fakta untuk Perisai, default TurnBackHoax.id dari MAFINDO. |
86
+ | `REPORT_CLASSIFICATION` | Tanda klasifikasi di laporan, default `TERBATAS`. |
87
+ | `SHIELD_PER_MINUTE` | Batas permintaan Perisai per alamat per menit, default 20. |
88
+
89
+ ## Docker
90
+
91
+ ```bash
92
+ cp .env.example .env
93
+ docker compose up -d --build
94
+ ```
95
+
96
+ Image sudah berisi model, Typst dan Chrome. Untuk server dengan HTTPS, isi `DOMAIN` di `.env` lalu jalankan `docker compose -f compose.yaml -f docker/compose.prod.yaml up -d --build`. TikTok memakai profile tambahan: `docker compose --profile tiktok up -d`.
97
+
98
+ ## Pengembangan
99
+
100
+ ```bash
101
+ bun install
102
+ bun run dev # dashboard dengan hot reload
103
+ bun test
104
+ bun run typecheck
105
+ bun run build
106
+ ```
107
+
108
+ ## Data dan privasi
109
+
110
+ Hanya data publik yang dikumpulkan, dan handle diganti pseudonim HMAC sebelum disimpan. Hasil deteksi adalah indikasi pola perilaku terkoordinasi, bukan tuduhan terhadap individu, dan keputusan akhir ada di analis. Setiap perubahan tercatat di audit log yang setiap entrinya memuat hash SHA-256 entri sebelumnya.
111
+
112
+ ## Lisensi
113
+
114
+ Apache 2.0. Pemetaan DISARM di `web/config/disarm.json` berasal dari DISARM Framework dengan lisensi CC BY-SA 4.0.
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env node
2
+ import { spawnSync } from "node:child_process";
3
+ import { fileURLToPath } from "node:url";
4
+
5
+ const entry = fileURLToPath(new URL("../dist/cli/index.js", import.meta.url));
6
+
7
+ if (process.versions.bun) await import(entry);
8
+ else {
9
+ const r = spawnSync("bun", [entry, ...process.argv.slice(2)], { stdio: "inherit" });
10
+ if (r.error) {
11
+ console.error("Cakra Nala CLI berjalan di Bun. Pasang dulu: curl -fsSL https://bun.sh/install | bash");
12
+ process.exit(1);
13
+ }
14
+ process.exit(r.status ?? 1);
15
+ }
@@ -0,0 +1,23 @@
1
+ // @bun
2
+ var h={name:"cakranala",version:"0.1.0",description:"Deteksi operasi pengaruh terkoordinasi di media sosial dari ketergantungan antar akun.",license:"Apache-2.0",type:"module",repository:"github:pajarori/cakranala",homepage:"https://github.com/pajarori/cakranala#readme",keywords:["coordinated inauthentic behavior","influence operations","buzzer","bot detection","social media","network analysis","osint"],bin:{cakranala:"./bin/cakranala.js"},exports:{".":{types:"./dist/index.d.ts",default:"./dist/index.js"},"./collect":{types:"./dist/collect/index.d.ts",default:"./dist/collect/index.js"},"./embed":{types:"./dist/embed/index.d.ts",default:"./dist/embed/index.js"},"./scenarios/*":"./scenarios/*"},files:["dist","bin","scenarios"],sideEffects:!1,engines:{node:">=20",bun:">=1.2.17"},scripts:{dev:"bun --hot web/src/index.ts",build:"bun scripts/build.ts",prepublishOnly:"bun run typecheck && bun test && bun run build",test:"bun test",typecheck:"tsc --noEmit"},dependencies:{"@puppeteer/browsers":"3.2.6",graphology:"^0.26.0","graphology-communities-louvain":"^2.0.2","puppeteer-core":"25.16.0","x-client-transaction-id":"^0.3.3","youtubei.js":"18.3.0"},peerDependencies:{"@huggingface/transformers":"^3.7.0"},peerDependenciesMeta:{"@huggingface/transformers":{optional:!0}},devDependencies:{"@huggingface/transformers":"^3.7.0","@tanstack/react-query":"^5.107.1","@tanstack/react-router":"^1.170.46","@tanstack/react-table":"^9.3.1","@types/bun":"latest","@types/react":"^19","@types/react-dom":"^19","bun-plugin-tailwind":"^0.1.2","class-variance-authority":"^0.7.1",clsx:"^2.1.1",cmdk:"^1.1.1",cytoscape:"^3.35.0","lucide-react":"^1","radix-ui":"^1.8.1",react:"^19","react-dom":"^19",recharts:"3.8.0","simple-icons":"16.36.0",sonner:"^2.0.8","tailwind-merge":"^3.7.0",tailwindcss:"^4.1.11","tw-animate-css":"^1.4.0",typescript:"^5.9.0"}};import{existsSync as ae}from"fs";import{join as oe}from"path";import{Browser as se,computeExecutablePath as ie}from"@puppeteer/browsers";import ce from"puppeteer-core";import{PUPPETEER_REVISIONS as pe}from"puppeteer-core/internal/revisions.js";import{homedir as ne}from"os";import{join as z,resolve as F}from"path";var L=()=>F(process.env.CAKRANALA_DATA??".cakranala"),m=()=>F(process.env.CAKRANALA_CACHE??z(process.env.XDG_CACHE_HOME??z(ne(),".cache"),"cakranala"));class de extends Error{}class me extends Error{}var w={browser:se.CHROME,buildId:pe.chrome,cacheDir:oe(m(),"chrome")};function D(){let e=ie(w);return ae(e)?e:void 0}var S;async function ue(){let e=await S?.catch(()=>{return});if(e?.connected)return e;let t=D();if(!t)throw Error("Chrome belum terpasang, jalankan: cakranala install");return S=ce.launch({executablePath:t,headless:!0,defaultViewport:null,protocolTimeout:120000,args:["--no-sandbox","--disable-dev-shm-usage","--disable-blink-features=AutomationControlled"]}),S}var le=(e,t)=>e.split(";").map((r)=>r.trim()).filter((r)=>r.includes("=")).map((r)=>({name:r.slice(0,r.indexOf("=")).trim(),value:r.slice(r.indexOf("=")+1).trim(),domain:t,path:"/",secure:!0})),He=(e)=>new Promise((t)=>setTimeout(t,e*(0.6+Math.random()*0.8)));function fe(e){let t=e.replace(/^for \(;;\);/,"").trim();if(!t||t[0]!=="{"&&t[0]!=="[")return[];try{return[JSON.parse(t)]}catch{return t.split(`
3
+ `).flatMap((r)=>{try{return[JSON.parse(r)]}catch{return[]}})}}function*Ke(e){let t=[e];while(t.length){let r=t.pop();if(!r||typeof r!=="object")continue;if(!Array.isArray(r))yield r;for(let a of Object.values(r))if(a&&typeof a==="object")t.push(a)}}function qe(e,t){let r=[e];while(r.length){let a=r.shift();if(!a||typeof a!=="object")continue;if(!Array.isArray(a)){let n=t(a);if(n!==void 0)return n}for(let n of Object.values(a))if(n&&typeof n==="object")r.push(n)}return}async function Ve(e,t){let r=await e.goto(t,{waitUntil:"domcontentloaded",timeout:60000}).catch((a)=>{throw Error(/ERR_(PROXY|TUNNEL|CONNECTION|TIMED_OUT|NAME_NOT_RESOLVED)/.test(a.message)?`proxy gagal: ${a.message}`:a.message)});if(!r)throw Error(`proxy gagal: tidak ada respons dari ${new URL(t).host}`);if(r.status()===407)throw Error("proxy gagal: 407 Proxy Authentication Required");return r}var Ye=(e)=>/\/(login|checkpoint|challenge|two_step_verification)/.test(new URL(e).pathname);async function We(e,t,r){let a=await ue(),n=e.proxy?new URL(e.proxy):void 0,s=await a.createBrowserContext(n?{proxyServer:`${n.protocol}//${n.host}`}:{});try{let o=await s.newPage();if(n?.username)await o.authenticate({username:decodeURIComponent(n.username),password:decodeURIComponent(n.password)});await o.setUserAgent({userAgent:e.userAgent}),await o.setExtraHTTPHeaders({"accept-language":"id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7"}),await o.setViewport({width:1366,height:900}),await o.evaluateOnNewDocument(()=>Object.defineProperty(navigator,"webdriver",{get:()=>{return}})),await s.setCookie(...le(e.cookie,t));let p=[];return o.on("response",(c)=>{let l=c.headers()["content-type"]??"";if(!/json|javascript|text\/html/.test(l)||!/graphql|\/api\//.test(c.url()))return;c.text().then((A)=>p.push(...fe(A))).catch(()=>{return})}),await r(o,p)}finally{await s.close().catch(()=>{return})}}import{existsSync as ye}from"fs";import{join as we}from"path";import{existsSync as H}from"fs";import{createRequire as ge}from"module";import{join as P}from"path";import{pathToFileURL as he}from"url";var K="Xenova/multilingual-e5-small";async function O(){let e=await import("@huggingface/transformers").catch(()=>{return});if(e)return e;let t=P(m(),"deps");if(!H(P(t,"node_modules","@huggingface","transformers")))return;return import(he(ge(P(t,"package.json")).resolve("@huggingface/transformers")).href)}var _=(e=K)=>H(P(m(),"models",e));async function q(e={}){let t=await O();if(!t)throw Error("layer semantik butuh @huggingface/transformers, jalankan: cakranala install");t.env.cacheDir=P(m(),"models");let r=t.pipeline,a=e.progress&&((o)=>o.status==="progress"&&e.progress(o.file??"",o.loaded??0,o.total??0)),n=await r("feature-extraction",e.model??K,{dtype:e.dtype??"q8",progress_callback:a}),s=e.batch??64;return{async embed(o){let p=[];for(let c=0;c<o.length;c+=s){let l=await n(o.slice(c,c+s).map((k)=>`query: ${k}`),{pooling:"mean",normalize:!0}),[A,B]=l.dims,te=l.data;for(let k=0;k<A;k++)p.push(te.slice(k*B,(k+1)*B))}return p}}}var b="0.15.1";function x(){let e=we(m(),"bin",process.platform==="win32"?"typst.exe":"typst");return process.env.TYPST_BIN??(ye(e)?e:Bun.which("typst")??void 0)}async function C(){let e=!!await O();return{semantic:e,model:e&&_(),pdf:!!x(),chrome:!!D(),unzip:!!Bun.which("unzip")}}var M=!!process.stderr.isTTY&&!process.env.NO_COLOR&&process.env.TERM!=="dumb",E=(e)=>(t)=>M?`\x1B[${e}m${t}\x1B[0m`:t,v=E(1),d=E(2),j=E(31),N=E(32),ct=E(33),u=E(36),i=(e="")=>process.stderr.write(`${e}
4
+ `),V=(e)=>`${(e/1e6).toFixed(1)} MB`;function U(e,t,r="",a=""){i(` ${e} ${t.padEnd(9)} ${r}${a?` ${d(a)}`:""}`)}var f=(e,t="")=>U(N("\u2713"),e,d(t)),T=(e,t,r="")=>U(j("\u2717"),e,t,r),I=(e,t)=>U(d("-"),e,d(t));function Y(e){let t=0,r="",a=(s)=>{if(!M)return;r=s,process.stderr.write(`\r\x1B[2K ${u("\u2193")} ${e.padEnd(9)} ${s}`)},n=()=>M&&r&&process.stderr.write("\r\x1B[2K");return{note:(s)=>M?a(d(s)):i(` ${e} ${s}`),progress(s,o){let p=Date.now();if(p-t<80&&s<o)return;t=p;let c=o?Math.min(24,Math.round(24*s/o)):0;a(`${"\u2501".repeat(c)}${d("\u2501".repeat(24-c))} ${V(s)}${o?` / ${V(o)}`:""}`)},done:(s="")=>(n(),f(e,s)),fail:(s)=>(n(),T(e,j(s)))}}async function W(){let e=await C();if(i(`${d(`bun ${Bun.version}, ${process.platform}-${process.arch}`)}
5
+ `),f("engine","5 layer deteksi"),!e.semantic)T("semantik","belum terpasang","cakranala install model");else if(!e.model)T("semantik","model belum diunduh","cakranala install model");else f("semantik","multilingual-e5-small");if(e.pdf)f("PDF",x().startsWith(m())?`typst ${b}`:x());else T("PDF","Typst belum terpasang","cakranala install typst");if(e.chrome)f("chrome",`${w.buildId}, untuk Instagram dan Facebook`);else T("chrome","belum terpasang","cakranala install chrome");if(e.unzip)f("unzip","impor arsip zip");else I("unzip","tidak ada, impor arsip zip nonaktif");if(process.env.LLM_BASE_URL&&process.env.LLM_MODEL)f("LLM",`${process.env.LLM_MODEL} di ${new URL(process.env.LLM_BASE_URL).host}`);else I("LLM","LLM_BASE_URL dan LLM_MODEL tidak diatur, judul dan ringkasan memakai template");if(process.env.TIKTOK_SIDECAR_URL)f("tiktok",process.env.TIKTOK_SIDECAR_URL);else I("tiktok","TIKTOK_SIDECAR_URL tidak diatur");return i(`
6
+ ${"data".padEnd(11)}${d(L())}
7
+ ${"cache".padEnd(11)}${d(m())}`),0}import{chmod as ke,mkdir as J,mkdtemp as be,rename as xe,rm as $e,writeFile as G}from"fs/promises";import{tmpdir as Ee}from"os";import{join as y}from"path";import{install as ve}from"@puppeteer/browsers";var Te={"linux-x64":"x86_64-unknown-linux-musl","linux-arm64":"aarch64-unknown-linux-musl","darwin-x64":"x86_64-apple-darwin","darwin-arm64":"aarch64-apple-darwin","win32-x64":"x86_64-pc-windows-msvc"};async function Re(e,t,r){let a=await fetch(e);if(!a.ok||!a.body)throw Error(`unduhan gagal: ${a.status}`);let n=Number(a.headers.get("content-length")??0),s=[],o=0,p=a.body.getReader();for(let c=await p.read();!c.done;c=await p.read())s.push(c.value),o+=c.value.length,r.progress(o,n);await G(t,Buffer.concat(s))}async function Ae(e){if(!await O()){e.note("memasang @huggingface/transformers");let n=y(m(),"deps");await J(n,{recursive:!0}),await G(y(n,"package.json"),JSON.stringify({name:"cakranala-deps",private:!0}));let s=Bun.spawn(["bun","add",`@huggingface/transformers@${h.peerDependencies["@huggingface/transformers"]}`],{cwd:n,stdout:"ignore",stderr:"pipe"});if(await s.exited!==0)throw Error((await new Response(s.stderr).text()).trim().split(`
8
+ `).at(-1)??"bun add gagal")}let t=_(),r=new Map;return await(await q({progress(n,s,o){r.set(n,[s,o]);let p=[...r.values()].reduce((c,[l,A])=>[c[0]+l,c[1]+A],[0,0]);e.progress(p[0],p[1])}})).embed(["siap"]),t?"sudah ada":"multilingual-e5-small"}async function Le(e){let t=x();if(t)return t.startsWith(m())?`${b}, sudah ada`:`memakai ${t}`;let r=Te[`${process.platform}-${process.arch}`];if(!r)throw Error(`Typst tidak tersedia untuk ${process.platform}-${process.arch}`);let a=process.platform==="win32",n=await be(y(Ee(),"cakranala-typst-"));try{let s=y(n,a?"typst.zip":"typst.tar.xz");await Re(`https://github.com/typst/typst/releases/download/v${b}/typst-${r}.${a?"zip":"tar.xz"}`,s,e),e.note("mengekstrak");let o=Bun.spawn(["tar","-xf",s,"-C",n],{stderr:"pipe"});if(await o.exited!==0)throw Error((await new Response(o.stderr).text()).trim()||"ekstrak gagal");let p=y(m(),"bin");await J(p,{recursive:!0});let c=a?"typst.exe":"typst";return await xe(y(n,`typst-${r}`,c),y(p,c)),await ke(y(p,c),493),b}finally{await $e(n,{recursive:!0,force:!0})}}async function De(e){if(D())return`${w.buildId}, sudah ada`;return await ve({...w,downloadProgressCallback:(t,r)=>e.progress(t,r)}),w.buildId}var R={model:{size:"\xB1380 MB",purpose:"layer semantik dan Perisai",run:Ae},typst:{size:"\xB160 MB",purpose:"laporan PDF",run:Le},chrome:{size:"\xB1400 MB",purpose:"Instagram dan Facebook",run:De}};async function X(e){let t=e.filter((n)=>!(n in R));if(t.length)return i(`Tidak dikenal: ${t.join(", ")}. Pilihan: ${Object.keys(R).join(", ")}`),2;let r=e.length?e:Object.keys(R);i(`${v("Cakra Nala")} ${d(`menyiapkan fitur di ${m()}`)}
9
+ `);for(let n of r)i(` ${n.padEnd(9)} ${d(`${R[n].purpose}, ${R[n].size} di disk`)}`);i();let a=[];for(let n of r){let s=Y(n);try{s.done(await R[n].run(s))}catch(o){a.push(n),s.fail(o instanceof Error?o.message:String(o))}}if(i(),a.length)return i(`Ulangi yang gagal: ${u(`cakranala install ${a.join(" ")}`)}`),1;return i(`Semua siap. Jalankan ${u("cakranala serve")}`),0}import{fileURLToPath as Pe}from"url";import{parseArgs as Oe}from"util";async function Q(e){let{values:t}=Oe({args:e,options:{port:{type:"string",short:"p"},data:{type:"string",short:"d"},demo:{type:"boolean"}}});if(t.port)process.env.PORT=t.port;if(t.data)process.env.CAKRANALA_DATA=t.data;if(t.demo)process.env.DEMO_MODE="1";process.env.NODE_ENV??="production";let r=performance.now();process.env.CAKRANALA_DATA=L(),process.env.CAKRANALA_CACHE=m();let a=new URL("../web/",import.meta.url);process.chdir(Pe(a));let{server:n,firstRun:s}=await import(new URL("index.js",a).href),o=await C(),p=(c,l)=>c?N(l):d(`${j("\u2717")} ${l}`);if(i(`
10
+ ${v("Cakra Nala")} ${d(`v${h.version}`)} ${d(`siap dalam ${Math.round(performance.now()-r)} ms`)}
11
+ `),i(` ${"Dashboard".padEnd(11)}${u(n.url.href)}`),i(` ${"Data".padEnd(11)}${d(L())}`),i(` ${"Fitur".padEnd(11)}${[p(o.semantic,"semantik"),p(o.pdf,"PDF"),p(o.chrome,"Instagram/Facebook")].join(" ")}`),!o.semantic||!o.pdf||!o.chrome)i(` ${"".padEnd(11)}${d(`lengkapi dengan ${u("cakranala install")}`)}`);if(process.env.DEMO_MODE)i(` ${"Demo".padEnd(11)}${d("replay skenario Pulau Seruni")}`);if(s)i(`
12
+ Buka dashboard untuk membuat akun pertama.`);return i(),new Promise(()=>{return})}var Z=`${v("Cakra Nala")} ${d(`v${h.version}`)} deteksi operasi pengaruh terkoordinasi
13
+
14
+ ${u("cakranala install")} siapkan semua fitur: model e5, Typst, Chrome
15
+ ${u("cakranala serve")} jalankan dashboard di http://localhost:3000
16
+ ${u("cakranala doctor")} cek fitur yang siap dan yang kurang
17
+
18
+ serve --port 3000 --data .cakranala --demo
19
+ install [model] [typst] [chrome]
20
+
21
+ Library: import { analyze } from "cakranala"
22
+ Docs: ${h.homepage}`,[g,..._e]=process.argv.slice(2),ee={install:X,serve:Q,doctor:W};if(g==="--version"||g==="-v")console.log(h.version);else if(!g||g==="--help"||g==="-h"||g==="help")i(Z);else if(!ee[g])i(`Perintah tidak dikenal: ${g}
23
+ `),i(Z),process.exitCode=2;else process.exitCode=await ee[g](_e).catch((e)=>(i(e.message),1));
@@ -0,0 +1,24 @@
1
+ import { Browser as Kind } from "@puppeteer/browsers";
2
+ import { type Page } from "puppeteer-core";
3
+ export interface Session {
4
+ cookie: string;
5
+ userAgent: string;
6
+ proxy?: string;
7
+ }
8
+ export declare class Checkpoint extends Error {
9
+ }
10
+ export declare class Limited extends Error {
11
+ }
12
+ export declare const CHROME: {
13
+ browser: Kind;
14
+ buildId: "156.0.8078.47";
15
+ cacheDir: string;
16
+ };
17
+ export declare function chromePath(): string;
18
+ export declare const pause: (ms: number) => Promise<unknown>;
19
+ export declare function parseJson(body: string): unknown[];
20
+ export declare function walk(value: unknown): Generator<Record<string, any>>;
21
+ export declare function find<T>(value: unknown, pick: (o: Record<string, any>) => T | undefined): T | undefined;
22
+ export declare function go(page: Page, url: string): Promise<import("puppeteer-core").HTTPResponse>;
23
+ export declare const signedOut: (url: string) => boolean;
24
+ export declare function withPage<T>(session: Session, domain: string, run: (page: Page, captured: unknown[]) => Promise<T>): Promise<T>;
@@ -0,0 +1,19 @@
1
+ import { type Dataset } from "../engine";
2
+ import { type Session } from "./browser";
3
+ export declare const facebookUrl: (t: string) => string;
4
+ export declare function createFacebookCollector(): {
5
+ me: (session: Session) => Promise<{
6
+ id: string;
7
+ handle: any;
8
+ name: any;
9
+ }>;
10
+ page: (session: Session, target: string, o: {
11
+ pages: number;
12
+ }) => Promise<Dataset>;
13
+ post: (session: Session, target: string, o: {
14
+ pages: number;
15
+ }) => Promise<Dataset>;
16
+ search: (session: Session, query: string, o: {
17
+ pages: number;
18
+ }) => Promise<Dataset>;
19
+ };
@@ -0,0 +1,66 @@
1
+ import type { Dataset } from "../engine";
2
+ import { type Profile } from "./x";
3
+ import { type SidecarConfig } from "./tiktok";
4
+ export { createXCollector, sessionFromEnv, type XSession, type XRateState, type Profile } from "./x";
5
+ export { createYoutubeCollector } from "./youtube";
6
+ export { collectTelegram } from "./telegram";
7
+ export { createTiktokCollector, sidecarFromEnv, type SidecarConfig } from "./tiktok";
8
+ export { chromePath } from "./browser";
9
+ export { createInstagramCollector } from "./instagram";
10
+ export { createFacebookCollector } from "./facebook";
11
+ export type Platform = "x" | "youtube" | "telegram" | "tiktok" | "instagram" | "facebook";
12
+ export type Kind = "topic" | "account" | "post";
13
+ export declare const PLATFORM_KINDS: Record<Platform, Kind[]>;
14
+ export declare const CREDENTIAL_FIELDS: Record<Exclude<Platform, "telegram">, {
15
+ fields: string[];
16
+ required: string[];
17
+ }>;
18
+ export interface Probe {
19
+ ok: boolean;
20
+ profile?: Profile & {
21
+ signedIn?: boolean;
22
+ };
23
+ error?: string;
24
+ }
25
+ export declare function probe(platform: Platform, fields: Record<string, string>, o?: {
26
+ tiktok?: SidecarConfig;
27
+ proxy?: string;
28
+ }): Promise<Probe>;
29
+ export interface Target {
30
+ platform: Platform;
31
+ kind: Kind;
32
+ target: string;
33
+ pages: number;
34
+ }
35
+ export interface Lease {
36
+ id: string;
37
+ fields: Record<string, string>;
38
+ proxy?: {
39
+ id: string;
40
+ url: string;
41
+ };
42
+ }
43
+ export interface CredentialPool {
44
+ count(platform: Platform): number;
45
+ acquire(platform: Platform, exclude: string[]): Promise<Lease | undefined>;
46
+ ok(id: string): void;
47
+ limited(id: string, until: number): void;
48
+ failed(id: string, error: string): void;
49
+ identity(id: string): string | undefined;
50
+ setIdentity(id: string, identity: string): void;
51
+ proxyFailed(id: string, error: string, credentialId: string): void;
52
+ }
53
+ export declare function xQuery(kind: Kind, target: string): string;
54
+ export declare function validate(t: Pick<Target, "platform" | "kind" | "target">): void;
55
+ export declare function createCollector(o: {
56
+ pool: CredentialPool;
57
+ tiktok?: SidecarConfig;
58
+ cooldownMs?: number;
59
+ }): {
60
+ rate: {
61
+ remaining: number | null;
62
+ reset: number | null;
63
+ };
64
+ tiktok: boolean;
65
+ collect(t: Target): Promise<Dataset>;
66
+ };
@@ -0,0 +1,4 @@
1
+ // @bun
2
+ import{ClientTransaction as ot,fetchXDocument as at}from"x-client-transaction-id";var v=1;var St=(t)=>Number((BigInt(t)>>22n)+1288834974657n),j=(t)=>new Promise((n)=>setTimeout(n,t)),$t=20000,F=(t,n,r)=>fetch(t,{headers:n,proxy:r,signal:AbortSignal.timeout($t)});async function Rt(t){let n=t.userAgent,r=await(await F("https://x.com/home",{"user-agent":n,cookie:`auth_token=${t.authToken}; ct0=${t.ct0}`},t.proxy)).text(),a=r.match(/https:\/\/abs\.twimg\.com\/responsive-web\/client-web\/main\.[a-z0-9]+\.js/)?.[0];if(!a)throw Error("x.com main bundle not found, the session may be logged out");let u=await(await F(a,{"user-agent":n},t.proxy)).text(),d=(l)=>u.match(new RegExp(`queryId:"([^"]+)",operationName:"${l}"`))?.[1],s=u.match(/"(AAAAAAAAAAAAAAAAAAAAA[^"]+)"/)?.[1],g=d("SearchTimeline");if(!g||!s)throw Error("x.com query id or bearer not found, the front end may have changed");let i=async(l,c)=>{let m=new Map,e=new Map;for(let o of r.matchAll(/(\d+):"([^"]+)"/g))(/^[a-f0-9]{5,}$/.test(o[2])?e:m).set(o[1],o[2]);for(let[o,p]of m){if(!e.has(o)||!c.test(p))continue;let h=(await(await F(`https://abs.twimg.com/responsive-web/client-web/${p}.${e.get(o)}a.js`,{"user-agent":n},t.proxy)).text()).match(new RegExp(`queryId:"([^"]+)",operationName:"${l}"`))?.[1];if(h)return h}return};return{bearer:s,search:g,viewer:d("Viewer"),detail:d("TweetDetail"),retweeters:d("Retweeters")??await i("Retweeters",/Tweet|Engagement|Retweet/i)}}var G=(t)=>t?.__typename==="TweetWithVisibilityResults"?t.tweet:t;function W(t){if(!t?.rest_id)return;let n=t.core?.created_at??t.legacy?.created_at;return{id:`x:${t.rest_id}`,platform:"x",handle:t.core?.screen_name??t.legacy?.screen_name,createdAt:n?Date.parse(n):void 0,followers:t.relationship_counts?.followers??t.legacy?.followers_count,following:t.relationship_counts?.following??t.legacy?.friends_count,posts:t.tweet_counts?.tweets??t.legacy?.statuses_count}}function st(t){let n=G(t),r=n?.legacy,a=W(n?.core?.user_results?.result);if(!r||!a)return;let u=G(r.retweeted_status_result?.result)?.rest_id,d=G(n.quoted_status_result?.result)?.rest_id,s=r.in_reply_to_status_id_str,g=u??s??d;return{actor:a,event:{id:`x:${n.rest_id}`,actor:a.id,action:u?"reshare":s?"reply":d?"quote":"post",ts:St(n.rest_id),precision:"ms",target:g?`x:${g}`:void 0,text:n.note_tweet?.note_tweet_results?.result?.text??r.full_text,tags:(r.entities?.hashtags??[]).map((i)=>i.text),urls:(r.entities?.urls??[]).map((i)=>i.expanded_url),lang:r.lang}}}async function H(t){let{bearer:n,search:r,viewer:a,detail:u,retweeters:d}=await Rt(t),s=await ot.create(await at()),g={},i={remaining:null,reset:null};async function l(e,o,p){if(i.remaining===0&&i.reset&&Date.now()<i.reset)return{limited:!0};let f=!1;for(let h=0;h<5;h++){let w=await F(`https://x.com${e}?variables=${encodeURIComponent(JSON.stringify(o))}&features=${encodeURIComponent(JSON.stringify(g))}`,{"user-agent":t.userAgent,authorization:`Bearer ${n}`,"x-csrf-token":t.ct0,cookie:`auth_token=${t.authToken}; ct0=${t.ct0}`,"x-twitter-auth-type":"OAuth2Session","x-twitter-active-user":"yes","x-client-transaction-id":await s.generateTransactionId("GET",e),referer:p},t.proxy).catch((k)=>k);if(w instanceof Error){await j(Math.min(16000,1000*2**h));continue}i.remaining=Number(w.headers.get("x-rate-limit-remaining")??NaN)||(w.status===429?0:i.remaining),i.reset=Number(w.headers.get("x-rate-limit-reset")??NaN)*1000||i.reset;let y=await w.text();if(w.status===400&&/cannot be null/.test(y)){for(let k of y.matchAll(/cannot be null: ([^"]+)/g))for(let x of k[1].split(","))g[x.trim()]=!1;continue}if(w.status===429)return{limited:!0};if(w.status===404&&!f){s=await ot.create(await at()),f=!0;continue}if(w.status>=500){await j(Math.min(16000,1000*2**h));continue}if(!w.ok)throw Error(`x ${w.status}: ${y.slice(0,160)}`);return JSON.parse(y)}throw Error("x request failed after retries")}let c=(e,o)=>l(`/i/api/graphql/${r}/SearchTimeline`,{rawQuery:e,count:20,querySource:"typed_query",product:"Latest",...o?{cursor:o}:{}},"https://x.com/search"),m=(e,o,p,f)=>({version:v,actors:[...e.values()],events:[...o.values()].sort((h,w)=>h.ts-w.ts),meta:{source:"x",query:p,coverage:f,collectedAt:Date.now()}});return{rate:i,async search(e,o={}){let p=o.pages??10,[f,h]=o.delayMs??[2000,4500],w=`${e}${o.since?` since:${o.since}`:""}`,y=new Map,k=new Map,x,S=0;for(let T=0;T<p;T++){if(i.remaining===0)break;let q=await c(w,x);if(q.limited)break;S++;let A=(q.data?.search_by_raw_query?.search_timeline?.timeline?.instructions??[]).flatMap((b)=>b.entries??(b.entry?[b.entry]:[])),L=0;x=void 0;for(let b of A){let _=st(b.content?.itemContent?.tweet_results?.result);if(_&&(!o.lang||_.event.lang===o.lang))y.set(_.actor.id,_.actor),k.set(_.event.id,_.event),L++;if(b.content?.cursorType==="Bottom")x=b.content.value}if(o.onPage?.({page:T,tweets:L,remaining:i.remaining}),!x||!A.length)break;if(T<p-1)await j(f+Math.random()*(h-f))}return m(y,k,w,S/p)},async me(){if(!a)throw Error("x.com Viewer query id not found");let e=await l(`/i/api/graphql/${a}/Viewer`,{withCommunitiesMemberships:!1},"https://x.com/home");if(e.limited)throw Error("rate limited");let o=e.data?.viewer?.user_results?.result,p=W(o);if(!p)throw Error("session is not signed in");return{id:p.id,handle:`@${p.handle}`,name:o.core?.name??o.legacy?.name??p.handle??"",followers:p.followers,createdAt:p.createdAt}},async retweeters(e,o=1){if(!d)throw Error("x.com Retweeters query id not found");let p=new Map,f;for(let h=0;h<o;h++){let w=await l(`/i/api/graphql/${d}/Retweeters`,{tweetId:e,count:100,includePromotedContent:!1,...f?{cursor:f}:{}},`https://x.com/i/status/${e}/retweets`);if(w.limited)break;f=void 0;for(let y of(w.data?.retweeters_timeline?.timeline?.instructions??[]).flatMap((k)=>k.entries??[])){let k=W(y.content?.itemContent?.user_results?.result);if(k)p.set(k.id,{...k,reshared:[`x:${e}`]});if(y.content?.cursorType==="Bottom")f=y.content.value}if(!f)break}return[...p.values()]},async conversation(e,o={}){if(!u)throw Error("x.com TweetDetail query id not found");let p=o.pages??5,[f,h]=o.delayMs??[2000,4500],w=new Map,y=new Map,k,x=0;for(let S=0;S<p;S++){let T=await l(`/i/api/graphql/${u}/TweetDetail`,{focalTweetId:e,with_rux_injections:!1,rankingMode:"Recency",includePromotedContent:!1,withCommunity:!0,withQuickPromoteEligibilityTweetFields:!1,withBirdwatchNotes:!1,withVoice:!0,...k?{cursor:k,referrer:"tweet"}:{}},`https://x.com/i/status/${e}`);if(T.limited)break;x++;let q=(T.data?.threaded_conversation_with_injections_v2?.instructions??[]).flatMap((A)=>A.entries??[]);k=void 0;for(let A of q){let L=[A.content?.itemContent,...(A.content?.items??[]).map((b)=>b.item?.itemContent)];for(let b of L){let _=st(b?.tweet_results?.result);if(_)w.set(_.actor.id,_.actor),y.set(_.event.id,_.event);if(b?.cursorType==="Bottom"||b?.cursorType==="ShowMoreThreads")k=b.value}if(A.content?.cursorType==="Bottom")k=A.content.value}if(!k)break;if(S<p-1)await j(f+Math.random()*(h-f))}return m(w,y,`conversation:${e}`,x/p)}}}function Ct(t=process.env){if(!t.X_AUTH_TOKEN||!t.X_CT0)return;return{authToken:t.X_AUTH_TOKEN,ct0:t.X_CT0,userAgent:t.X_USER_AGENT??"Mozilla/5.0"}}import{Innertube as Pt}from"youtubei.js";var Mt={second:1000,minute:60000,hour:3600000,day:86400000,week:604800000,month:2592000000,year:31536000000},Ot=(t,n)=>{let r=t.match(/(\d+)\s*(second|minute|hour|day|week|month|year)/i);return r?n-Number(r[1])*Mt[r[2].toLowerCase()]:n};async function M(t,n){let r=(s,g)=>fetch(s,{...g,proxy:n}),a=await Pt.create({retrieve_player:!1,cookie:t,fetch:n?r:void 0});async function u(s,g,i,l,c){let m;for(let e of["NEWEST_FIRST","NEWEST_FIRST","TOP_COMMENTS"])m??=await a.getComments(s,e).catch(()=>{return});for(let e=0;e<g&&m;e++){for(let o of m.contents??[]){let p=o.comment;if(!p?.comment_id||!p.author?.id)continue;let f=`yt:${p.author.id}`;l.set(f,{id:f,platform:"youtube",handle:p.author.name}),c.set(`yt:${p.comment_id}`,{id:`yt:${p.comment_id}`,actor:f,action:"comment",ts:Ot(String(p.published_time??""),i),precision:"approx",target:`yt:${s}`,text:p.content?.toString()})}m=m.has_continuation?await m.getContinuation().catch(()=>{return}):void 0}}let d=(s,g,i)=>({version:v,actors:[...s.values()],events:[...g.values()].sort((l,c)=>l.ts-c.ts),meta:{source:"youtube",query:i,coverage:1,collectedAt:Date.now()}});return{async me(){if(!a.session.logged_in)throw Error("cookie is not signed in to YouTube");let s=await a.account.getInfo(),g=s?.contents?.contents?.[0]??s?.contents?.[0],i=g?.account_name?.toString()??"",l=g?.channel_handle?.toString()??i;if(!l)throw Error("YouTube account details are not available");return{id:`yt:${l}`,handle:l,name:i}},async search(s,g={}){let i=Date.now(),l=new Map,c=new Map,m=await a.search(s,{type:"video",upload_date:"week",prioritize:"popularity"});for(let e of(m.videos??[]).slice(0,g.videos??3)){let o=e.id??e.video_id;if(o)await u(o,g.pages??2,i,l,c).catch(()=>{return})}return d(l,c,s)},async video(s,g={}){let i=new Map,l=new Map;return await u(s,g.pages??3,Date.now(),i,l),d(i,l,s)}}}var It=(t)=>t.match(/t\.me\/(?:s\/)?([A-Za-z0-9_]{4,})/)?.[1]?.toLowerCase();async function Dt(t,n){let r=await fetch(`https://t.me/s/${t}${n?`?before=${n}`:""}`,{signal:AbortSignal.timeout(20000)});if(!r.ok)throw Error(`telegram ${t} ${r.status}`);let a=[],u,d=0;return await new HTMLRewriter().on("div.tgme_widget_message[data-post]",{element(s){u={post:s.getAttribute("data-post"),text:"",ts:0,urls:[]},a.push(u)}}).on("div.tgme_widget_message_text",{element(s){d++,s.onEndTag(()=>void d--)},text(s){if(u&&d>0)u.text+=s.text}}).on("div.tgme_widget_message_text a[href]",{element(s){let g=s.getAttribute("href");if(u&&g?.startsWith("http"))u.urls.push(g)}}).on("div.tgme_widget_message_text br",{element(){if(u)u.text+=`
3
+ `}}).on(".tgme_widget_message_forwarded_from_name",{element(s){let g=s.getAttribute("href");if(u&&g)u.forwarded=It(g)}}).on("a.tgme_widget_message_date time[datetime]",{element(s){if(u)u.ts=Date.parse(s.getAttribute("datetime"))}}).transform(r).text(),a.filter((s)=>s.ts)}var Nt=(t)=>t.replace(/&amp;/g,"&").replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&quot;/g,'"').replace(/&#0?39;/g,"'");async function Q(t,n=1){let r=new Map,a=new Map;for(let u of t){let d=u.replace(/^@/,"").toLowerCase(),s;for(let g=0;g<n;g++){let i=await Dt(d,s);if(!i.length)break;for(let l of i){let[c,m]=l.post.split("/"),e=`tg:${c.toLowerCase()}`,o=Nt(l.text).trim();r.set(e,{id:e,platform:"telegram",handle:c}),a.set(`tg:${l.post}`,{id:`tg:${l.post}`,actor:e,action:l.forwarded?"reshare":"post",ts:l.ts,precision:"s",target:l.forwarded?`tg:${l.forwarded}`:void 0,text:o||void 0,tags:[...o.matchAll(/#([\p{L}\p{N}_]+)/gu)].map((p)=>p[1].toLowerCase()),urls:l.urls}),s=s===void 0?Number(m):Math.min(s,Number(m))}await Bun.sleep(1500)}}return{version:v,actors:[...r.values()],events:[...a.values()].sort((u,d)=>u.ts-d.ts),meta:{source:"telegram",query:t.join(" "),coverage:1,collectedAt:Date.now()}}}var it=60000;function Ut(t=process.env){if(!t.TIKTOK_SIDECAR_URL||!t.TIKTOK_SIDECAR_PASSWORD)return;return{baseUrl:t.TIKTOK_SIDECAR_URL.replace(/\/$/,""),username:t.TIKTOK_SIDECAR_USER??"cakranala",password:t.TIKTOK_SIDECAR_PASSWORD}}async function ct(t,n){let a=(await(await fetch("https://www.tiktok.com/passport/web/account/info/?aid=1459&app_language=en",{headers:{cookie:t,"user-agent":n??"Mozilla/5.0",referer:"https://www.tiktok.com/"},signal:AbortSignal.timeout(15000)})).json().catch(()=>({}))).data;if(!a?.username)return;return{id:`tt:${a.user_id_str??a.username}`,handle:`@${a.username}`,name:a.screen_name??""}}function X(t){let n;async function r(){let i=await fetch(`${t.baseUrl}/api/v1/auth/login`,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify({username:t.username,password:t.password}),signal:AbortSignal.timeout(it)}),l=i.headers.getSetCookie().find((c)=>c.startsWith("dtk_session="));if(!i.ok||!l)throw Error(`tiktok sidecar login failed with ${i.status}`);n=l.split(";")[0]}async function a(i,l,c={},m){for(let e=0;e<6;e++){if(!n)await r();let o=new URLSearchParams(Object.entries(c).flatMap(([w,y])=>y===void 0?[]:[[w,String(y)]])),p=await fetch(`${t.baseUrl}${l}${o.size?`?${o}`:""}`,{method:i,headers:{cookie:n,...m?{"content-type":"application/json"}:{}},body:m?JSON.stringify(m):void 0,signal:AbortSignal.timeout(it)});if(p.status===401){n=void 0;continue}let f=await p.json().catch(()=>({}));if(f.success){let w=f.data;if(w?.task_id&&!("items"in w)&&!("id"in w))return u(w.task_id);return w}let h=f.error?.retry_after;if(h&&e<5){await Bun.sleep(Math.min(30,Number(h))*1000);continue}throw Error(`tiktok ${f.error?.code??p.status}: ${f.error?.message??"request failed"}`)}throw Error("tiktok sidecar request failed after retries")}async function u(i){for(let l=0;l<20;l++){let c=await a("GET",`/api/v1/tasks/${i}`);if(c?.state==="succeeded"||c?.state==="completed"||c?.result)return c.result??c;if(c?.state==="failed")throw Error(`tiktok task failed: ${c.error?.message??i}`);await Bun.sleep(3000)}throw Error("tiktok task timed out")}let d=(i,l)=>{let c=`tt:${i.uid}`;return l.set(c,{id:c,platform:"tiktok",handle:i.unique_id??i.nickname}),c};async function s(i,l,c,m,e){let o;for(let p=0;p<l;p++){let f=await a("GET","/api/v1/tiktok/video/comments",{aweme_id:i,cursor:o,count:30,wait:30,identity:c});for(let h of f.items??[]){if(!h.created_at)continue;e.set(`tt:${h.comment_id}`,{id:`tt:${h.comment_id}`,actor:d(h.author,m),action:h.parent_id?"reply":"comment",ts:Date.parse(h.created_at),precision:"s",target:h.parent_id?`tt:${h.parent_id}`:`tt:${i}`,text:h.text})}if(!f.has_more||!f.cursor)break;o=f.cursor}}let g=(i,l,c)=>({version:v,actors:[...i.values()],events:[...l.values()].sort((m,e)=>m.ts-e.ts),meta:{source:"tiktok",query:c,coverage:1,collectedAt:Date.now()}});return{async check(i,l){let m=(await a("POST","/api/v1/admin/identities/import",{},{platform:"tiktok",cookies:i,user_agent:l,dry_run:!0})).report;if(m.missing_required?.length)throw Error(`missing cookies: ${m.missing_required.join(", ")}`);if(m.usable===!1)throw Error(m.warning_codes?.includes("unknown_browser")?"add the exact User-Agent of the browser this cookie was copied from":"the sidecar cannot use this cookie set");return{authenticated:!!m.authenticated,browser:m.browser_family??void 0}},async importIdentity(i,l){let c=await a("POST","/api/v1/admin/identities/import",{},{platform:"tiktok",cookies:i,user_agent:l,note:"cakranala vault"});if(!c.identity_id)throw Error("tiktok sidecar did not store the identity");return c.identity_id},async search(i,l={}){let c=new Map,m=new Map,e=await a("GET","/api/v1/tiktok/search/posts",{keyword:i,count:20,wait:30,identity:l.identity});for(let o of e.items??[])if(o.created_at)m.set(`tt:${o.content_id}`,{id:`tt:${o.content_id}`,actor:d(o.author,c),action:"post",ts:Date.parse(o.created_at),precision:"s",text:o.description??o.title,tags:o.tags});for(let o of(e.items??[]).slice(0,l.videos??3))await s(o.content_id,l.pages??2,l.identity,c,m);return g(c,m,i)},async video(i,l={}){let c=new Map,m=new Map;return await s(i,l.pages??3,l.identity,c,m),g(c,m,i)}}}import{existsSync as Lt}from"fs";import{join as jt}from"path";import{Browser as Ft,computeExecutablePath as Ht}from"@puppeteer/browsers";import Xt from"puppeteer-core";import{PUPPETEER_REVISIONS as Kt}from"puppeteer-core/internal/revisions.js";import{homedir as qt}from"os";import{join as ut,resolve as dt}from"path";var fe=()=>dt(process.env.CAKRANALA_DATA??".cakranala"),pt=()=>dt(process.env.CAKRANALA_CACHE??ut(process.env.XDG_CACHE_HOME??ut(qt(),".cache"),"cakranala"));class E extends Error{}class R extends Error{}var Bt={browser:Ft.CHROME,buildId:Kt.chrome,cacheDir:jt(pt(),"chrome")};function lt(){let t=Ht(Bt);return Lt(t)?t:void 0}var Z;async function Vt(){let t=await Z?.catch(()=>{return});if(t?.connected)return t;let n=lt();if(!n)throw Error("Chrome belum terpasang, jalankan: cakranala install");return Z=Xt.launch({executablePath:n,headless:!0,defaultViewport:null,protocolTimeout:120000,args:["--no-sandbox","--disable-dev-shm-usage","--disable-blink-features=AutomationControlled"]}),Z}var Yt=(t,n)=>t.split(";").map((r)=>r.trim()).filter((r)=>r.includes("=")).map((r)=>({name:r.slice(0,r.indexOf("=")).trim(),value:r.slice(r.indexOf("=")+1).trim(),domain:n,path:"/",secure:!0})),C=(t)=>new Promise((n)=>setTimeout(n,t*(0.6+Math.random()*0.8)));function O(t){let n=t.replace(/^for \(;;\);/,"").trim();if(!n||n[0]!=="{"&&n[0]!=="[")return[];try{return[JSON.parse(n)]}catch{return n.split(`
4
+ `).flatMap((r)=>{try{return[JSON.parse(r)]}catch{return[]}})}}function*I(t){let n=[t];while(n.length){let r=n.pop();if(!r||typeof r!=="object")continue;if(!Array.isArray(r))yield r;for(let a of Object.values(r))if(a&&typeof a==="object")n.push(a)}}function P(t,n){let r=[t];while(r.length){let a=r.shift();if(!a||typeof a!=="object")continue;if(!Array.isArray(a)){let u=n(a);if(u!==void 0)return u}for(let u of Object.values(a))if(u&&typeof u==="object")r.push(u)}return}async function D(t,n){let r=await t.goto(n,{waitUntil:"domcontentloaded",timeout:60000}).catch((a)=>{throw Error(/ERR_(PROXY|TUNNEL|CONNECTION|TIMED_OUT|NAME_NOT_RESOLVED)/.test(a.message)?`proxy gagal: ${a.message}`:a.message)});if(!r)throw Error(`proxy gagal: tidak ada respons dari ${new URL(n).host}`);if(r.status()===407)throw Error("proxy gagal: 407 Proxy Authentication Required");return r}var N=(t)=>/\/(login|checkpoint|challenge|two_step_verification)/.test(new URL(t).pathname);async function K(t,n,r){let a=await Vt(),u=t.proxy?new URL(t.proxy):void 0,d=await a.createBrowserContext(u?{proxyServer:`${u.protocol}//${u.host}`}:{});try{let s=await d.newPage();if(u?.username)await s.authenticate({username:decodeURIComponent(u.username),password:decodeURIComponent(u.password)});await s.setUserAgent({userAgent:t.userAgent}),await s.setExtraHTTPHeaders({"accept-language":"id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7"}),await s.setViewport({width:1366,height:900}),await s.evaluateOnNewDocument(()=>Object.defineProperty(navigator,"webdriver",{get:()=>{return}})),await d.setCookie(...Yt(t.cookie,n));let g=[];return s.on("response",(i)=>{let l=i.headers()["content-type"]??"";if(!/json|javascript|text\/html/.test(l)||!/graphql|\/api\//.test(i.url()))return;i.text().then((c)=>g.push(...O(c))).catch(()=>{return})}),await r(s,g)}finally{await d.close().catch(()=>{return})}}var Jt="https://www.instagram.com",zt="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",gt=3000,et=(t)=>t.match(/instagram\.com\/(?:[\w.]+\/)?(?:p|reel|reels|tv)\/([\w-]+)/)?.[1]??t.match(/^[\w-]{10,12}$/)?.[0],Gt=(t)=>[...t.slice(0,11)].reduce((n,r)=>n*64n+BigInt(zt.indexOf(r)),0n).toString(),mt=(t)=>[...t.matchAll(/#([\p{L}\p{N}_]+)/gu)].map((n)=>n[1].toLowerCase()),ft=(t)=>[...t.matchAll(/https?:\/\/\S+/g)].map((n)=>n[0]),Wt=(t)=>String(t.pk??t.id);async function U(t,n,r,a){if(a.length=0,(await D(t,`${Jt}${n}`)).status()===429)throw new R("Instagram membatasi laju permintaan");if(N(t.url()))throw new E("sesi Instagram perlu verifikasi atau login ulang");await C(gt);for(let g=0;g<r;g++)await t.evaluate(()=>window.scrollBy(0,window.innerHeight*2)),await C(gt);let d=await t.content();if(/class="[^"]*not-logged-in/.test(d))throw new E("sesi Instagram tidak masuk ke akun");if(/Please wait a few minutes/i.test(d))throw new R("Instagram membatasi laju permintaan");return[...(await t.$$eval('script[type="application/json"]',(g)=>g.map((i)=>i.textContent??""))).flatMap(O),...a]}function tt(){let t=new Map,n=new Map,r=new Map,a=new Map,u=(i)=>{let l=`ig:${Wt(i)}`,c=t.get(l);return t.set(l,{...c,id:l,platform:"instagram",handle:`@${i.username}`,followers:i.follower_count??c?.followers}),l};return{harvest:(i,l)=>{let c=i.flatMap((e)=>[...I(e)]),m=new Set;for(let e of c){if(typeof e.code!=="string"||typeof e.taken_at!=="number"||!e.user?.username)continue;let o=`ig:${String(e.pk??String(e.id).split("_")[0])}`;if(e.caption)m.add(e.caption);if(r.set(o,e.code),typeof e.comment_count==="number")a.set(o,e.comment_count);let p=e.caption?.text??"";if(!n.get(o)?.text)n.set(o,{id:o,actor:u(e.user),action:"post",ts:e.taken_at*1000,precision:"s",text:p||void 0,tags:mt(p),urls:ft(p)})}for(let e of c){if(m.has(e)||typeof e.code==="string"||typeof e.text!=="string"||typeof e.created_at!=="number"||!e.user?.username||(e.pk??e.id)===void 0)continue;let o=e.parent_comment_id?`ig:${e.parent_comment_id}`:void 0,p=o??(e.media_id?`ig:${String(e.media_id).split("_")[0]}`:l),f=`ig:${e.pk??e.id}`;n.set(f,{id:f,actor:u(e.user),action:o?"reply":"comment",ts:e.created_at*1000,precision:"s",target:p,text:e.text,tags:mt(e.text),urls:ft(e.text)})}},posts:()=>[...r.entries()].filter(([i])=>(a.get(i)??1)>0).map(([i,l])=>({id:i,code:l})),dataset:(i)=>({version:v,actors:[...t.values()],events:[...n.values()],meta:{source:"instagram",query:i,collectedAt:Date.now()}})}}async function wt(t,n,r){for(let a of r.posts())r.harvest(await U(t,`/p/${a.code}/`,1,n),a.id)}function B(){let t=(n,r)=>K(n,".instagram.com",r);return{me:(n)=>t(n,async(r,a)=>{let u=await U(r,"/",0,a),d=(await r.cookies()).find((g)=>g.name==="ds_user_id")?.value;if(!d)throw new E("cookie Instagram tidak masuk ke akun");let s=u.flatMap((g)=>[...I(g)]).find((g)=>String(g.pk??g.id)===d&&typeof g.username==="string");if(!s){let g=await r.content(),i=g.match(new RegExp(`"(?:pk|id)":"${d}"[^}]{0,400}?"username":"([\\w.]+)"`))?.[1]??g.match(new RegExp(`"username":"([\\w.]+)"[^}]{0,400}?"(?:pk|id)":"${d}"`))?.[1];if(i)s={pk:d,username:i}}return{id:`ig:${d}`,handle:s?`@${s.username}`:`ig:${d}`,name:s?.full_name??""}}),account:(n,r,a)=>t(n,async(u,d)=>{let s=tt();return s.harvest(await U(u,`/${encodeURIComponent(r)}/`,a.pages,d)),await wt(u,d,s),s.dataset(`@${r}`)}),post:(n,r,a)=>t(n,async(u,d)=>{let s=tt();return s.harvest(await U(u,`/p/${r}/`,a.pages,d),`ig:${Gt(r)}`),s.dataset(r)}),tag:(n,r,a)=>t(n,async(u,d)=>{let s=tt(),g=r.replace(/^#/,"").trim().toLowerCase();return s.harvest(await U(u,`/explore/tags/${encodeURIComponent(g)}/`,a.pages,d)),await wt(u,d,s),s.dataset(`#${g}`)})}}var Y="https://www.facebook.com",ht=3000,yt=(t)=>[...t.matchAll(/#([\p{L}\p{N}_]+)/gu)].map((n)=>n[1].toLowerCase()),kt=(t)=>[...t.matchAll(/https?:\/\/\S+/g)].map((n)=>n[0]),J=(t)=>{let n=t.trim();if(/^https?:\/\/(www\.|m\.|web\.)?facebook\.com\//.test(n))return n.replace(/^https?:\/\/(m|web)\./,"https://www.");if(/^[\w.]{3,}$/.test(n))return`${Y}/${n}`;return};function Qt(t){let n=t.url??t.profile_url,r=n?new URL(n,Y).pathname.split("/").filter(Boolean)[0]:void 0;return r&&r!=="profile.php"&&r!=="people"?`@${r}`:t.name??void 0}function nt(){let t=new Map,n=new Map,r=new Map,a=new Map,u=(c)=>{let m=`fb:${c.id}`;if(!t.has(m))t.set(m,{id:m,platform:"facebook",handle:Qt(c)});return m},d=(c)=>{let m=`fb:${c.post_id}`,e=P(c,(y)=>Array.isArray(y.actors)&&y.actors[0]?.id?y.actors[0]:void 0),o=P(c,(y)=>typeof y.creation_time==="number"?y.creation_time:void 0),p=P(c,(y)=>y.feedback&&typeof y.feedback.id==="string"?y.feedback.id:void 0);if(p)r.set(p,m);let f=P(c,(y)=>typeof y.wwwURL==="string"?y.wwwURL:void 0);if(f)a.set(m,f);if(!e||!o||n.get(m)?.text)return;let h=P(c,(y)=>y.message&&typeof y.message.text==="string"?y.message.text:void 0)??"",w=c.attached_story?.post_id;n.set(m,{id:m,actor:u(e),action:w?"reshare":"post",ts:o*1000,precision:"s",target:w?`fb:${w}`:void 0,text:h||void 0,tags:yt(h),urls:kt(h)})},s=(c,m)=>{let e=c.body?.text??c.preferred_body?.text,o=c.created_time;if(typeof e!=="string"||typeof o!=="number"||!c.author?.id)return;let p=`fb:${c.id}`,f=c.comment_parent?.id,h=f?`fb:${f}`:r.get(c.parent_feedback?.id)??m;n.set(p,{id:p,actor:u(c.author),action:f?"reply":"comment",ts:o*1000,precision:"s",target:h,text:e,tags:yt(e),urls:kt(e)})};return{harvest:(c,m)=>{let e=c.flatMap((o)=>[...I(o)]);for(let o of e)if(o.__typename==="Story"&&typeof o.post_id==="string")d(o);for(let o of e)if(o.__typename==="Comment")s(o,m)},dataset:(c)=>({version:v,actors:[...t.values()],events:[...n.values()],meta:{source:"facebook",query:c,collectedAt:Date.now()}}),posts:()=>[...n.values()].filter((c)=>c.action==="post"||c.action==="reshare").map((c)=>({id:c.id,url:a.get(c.id)}))}}async function V(t,n,r,a){if(a.length=0,await D(t,n),N(t.url()))throw new E("sesi Facebook perlu verifikasi atau login ulang");await C(ht);for(let d=0;d<r;d++)await t.evaluate(()=>window.scrollBy(0,window.innerHeight*2)),await C(ht);return[...(await t.$$eval('script[type="application/json"]',(d)=>d.map((s)=>s.textContent??""))).flatMap(O),...a]}async function bt(t,n,r){for(let a of r.posts()){if(!a.url)continue;r.harvest(await V(t,a.url,1,n),a.id)}}function z(){let t=(n,r)=>K(n,".facebook.com",r);return{me:(n)=>t(n,async(r)=>{if(await D(r,`${Y}/me`),N(r.url()))throw new E("sesi Facebook perlu verifikasi atau login ulang");let a=await r.content(),u=a.match(/"USER_ID":"(\d+)"/)?.[1];if(!u||u==="0")throw new E("cookie Facebook tidak masuk ke akun");let d=a.match(/"NAME":"((?:[^"\\]|\\.)*)"/)?.[1],s=new URL(r.url()).pathname.split("/").filter(Boolean)[0];return{id:`fb:${u}`,handle:s&&s!=="profile.php"?`@${s}`:d?JSON.parse(`"${d}"`):u,name:d?JSON.parse(`"${d}"`):""}}),page:(n,r,a)=>t(n,async(u,d)=>{let s=J(r);if(!s)throw Error("pantauan Facebook butuh URL atau nama Page");let g=nt();return g.harvest(await V(u,s,a.pages,d)),await bt(u,d,g),g.dataset(s)}),post:(n,r,a)=>t(n,async(u,d)=>{let s=J(r);if(!s)throw Error("pantauan Facebook butuh URL post");let g=nt(),i=await V(u,s,a.pages,d);return g.harvest(i),g.harvest(i,g.posts()[0]?.id),g.dataset(s)}),search:(n,r,a)=>t(n,async(u,d)=>{let s=nt();return s.harvest(await V(u,`${Y}/search/posts/?q=${encodeURIComponent(r)}`,a.pages,d)),await bt(u,d,s),s.dataset(r)})}}var Zt={x:["topic","account","post"],youtube:["topic","post"],telegram:["account"],tiktok:["topic","post"],instagram:["topic","account","post"],facebook:["topic","account","post"]},Ue={x:{fields:["authToken","ct0","userAgent"],required:["authToken","ct0"]},youtube:{fields:["cookie"],required:["cookie"]},tiktok:{fields:["cookie","userAgent"],required:["cookie","userAgent"]},instagram:{fields:["cookie","userAgent"],required:["cookie","userAgent"]},facebook:{fields:["cookie","userAgent"],required:["cookie","userAgent"]}},vt=(t,n)=>({cookie:t.cookie,userAgent:t.userAgent,proxy:n}),te=/proxy|tunnel|ERR_TUNNEL|ERR_PROXY|ECONNREFUSED|ECONNRESET|ETIMEDOUT|ENOTFOUND|socket|407|unable to connect|fetch failed/i;async function qe(t,n,r={}){let{tiktok:a,proxy:u}=r;try{if(t==="instagram"||t==="facebook")return{ok:!0,profile:{...await(t==="instagram"?B():z()).me(vt(n,u)),signedIn:!0}};if(t==="x")return{ok:!0,profile:await(await H({authToken:n.authToken,ct0:n.ct0,userAgent:n.userAgent||"Mozilla/5.0",proxy:u})).me()};if(t==="youtube")return{ok:!0,profile:await(await M(n.cookie,u)).me()};if(t==="tiktok"){if(!a)throw Error("sidecar TikTok belum dikonfigurasi");let d=await X(a).check(n.cookie,n.userAgent),s=d.authenticated?await ct(n.cookie,n.userAgent).catch(()=>{return}):void 0;return{ok:!0,profile:s?{...s,signedIn:!0}:{id:"tiktok",handle:d.authenticated?"Signed in session":"Anonymous session",name:d.browser??"",signedIn:d.authenticated}}}return{ok:!0}}catch(d){return{ok:!1,error:d instanceof Error?d.message:String(d)}}}var xt=(t)=>t.match(/(?:v=|youtu\.be\/|shorts\/)([\w-]{11})/)?.[1]??t.match(/^[\w-]{11}$/)?.[0],At=(t)=>t.match(/video\/(\d{8,})/)?.[1]??t.match(/^\d{8,}$/)?.[0],Et=(t)=>t.trim().replace(/^@/,"").replace(/^https?:\/\/(www\.)?instagram\.com\//,"").split(/[/?]/)[0].match(/^[\w.]{1,30}$/)?.[0],Tt=(t)=>t.split(/[\s,]+/).map((n)=>n.replace(/^@/,"").replace(/^https?:\/\/t\.me\/(s\/)?/,"").split(/[/?]/)[0]).filter((n)=>/^[A-Za-z0-9_]{4,}$/.test(n));function rt(t,n){let r=n.trim();if(t==="account"){let a=r.replace(/^@/,"").replace(/^https?:\/\/(x|twitter)\.com\//,"").split(/[/?]/)[0];return`(to:${a} OR @${a})`}if(t==="post"){let a=r.match(/status\/(\d+)/)?.[1]??r.match(/^\d+$/)?.[0];if(!a)throw Error("pantauan post butuh URL atau ID post");return`conversation_id:${a}`}return r}function ee(t){if(!Zt[t.platform]?.includes(t.kind))throw Error(`${t.platform} tidak mendukung pantauan ${t.kind}`);if(t.platform==="x")rt(t.kind,t.target);if(t.platform==="youtube"&&t.kind==="post"&&!xt(t.target))throw Error("pantauan YouTube butuh URL atau ID video");if(t.platform==="telegram"&&!Tt(t.target).length)throw Error("pantauan Telegram butuh nama kanal publik");if(t.platform==="tiktok"&&t.kind==="post"&&!At(t.target))throw Error("pantauan TikTok butuh URL atau ID video");if(t.platform==="instagram"&&t.kind==="post"&&!et(t.target))throw Error("pantauan Instagram butuh URL post atau reel");if(t.platform==="instagram"&&t.kind==="account"&&!Et(t.target))throw Error("pantauan Instagram butuh nama akun");if(t.platform==="instagram"&&t.kind==="topic"&&!t.target.replace(/^#/,"").trim())throw Error("pantauan Instagram butuh tagar");if(t.platform==="facebook"&&t.kind!=="topic"&&!J(t.target))throw Error("pantauan Facebook butuh URL atau nama Page")}var _t=()=>{let t=new Map;return(n,r)=>{let a=t.get(n);if(!a)a=r().catch((u)=>{throw t.delete(n),u}),t.set(n,a);return a}};function Le(t){let n={remaining:null,reset:null},r=_t(),a=_t(),u;async function d(e,o){let p=[],f=new Set,h;for(;;){let w=await t.pool.acquire(e,p);if(!w)break;p.push(w.id);try{let{value:y,limitedUntil:k}=await o(w);if(k){t.pool.limited(w.id,k),h=Error("terkena batas laju");continue}return t.pool.ok(w.id),y}catch(y){if(h=y instanceof Error?y:Error(String(y)),w.proxy&&te.test(h.message)){if(t.pool.proxyFailed(w.proxy.id,h.message,w.id),!f.has(w.id))f.add(w.id),p.pop()}else t.pool.failed(w.id,h.message)}}throw h??Error(`tidak ada kredensial ${e} yang siap`)}let s=async(e)=>t.pool.count("youtube")?d("youtube",async(o)=>({value:await e(await a(`${o.id}:${o.proxy?.id??""}`,()=>M(o.fields.cookie,o.proxy?.url)))})):e(await a("anonymous",()=>M())),g=(e)=>d("x",async(o)=>{let p=await r(`${o.id}:${o.proxy?.id??""}`,()=>H({authToken:o.fields.authToken,ct0:o.fields.ct0,userAgent:o.fields.userAgent||"Mozilla/5.0",proxy:o.proxy?.url})),f=await e(p);Object.assign(n,p.rate);let h=p.rate.reset&&p.rate.reset>Date.now()?p.rate.reset:Date.now()+900000;if(f.meta?.coverage===0)return{value:f,limitedUntil:h};if(p.rate.remaining===0)t.pool.limited(o.id,h);return{value:f}}),i=(e)=>{if(!t.tiktok)throw Error("sidecar TikTok belum dikonfigurasi");let o=u??=X(t.tiktok);if(!t.pool.count("tiktok"))return e(o);return d("tiktok",async(p)=>{let f=t.pool.identity(p.id);if(!f)f=await o.importIdentity(p.fields.cookie,p.fields.userAgent),t.pool.setIdentity(p.id,f);return{value:await e(o,f)}})},l=(e,o)=>d(e,async(p)=>{try{return{value:await o(vt(p.fields,p.proxy?.url))}}catch(f){if(f instanceof R)return{value:void 0,limitedUntil:Date.now()+(t.cooldownMs??1800000)};throw f}}),c=B(),m=z();return{rate:n,tiktok:!!t.tiktok,async collect(e){if(ee(e),e.platform==="youtube")return s((o)=>e.kind==="post"?o.video(xt(e.target),{pages:e.pages}):o.search(e.target,{videos:e.pages,pages:2}));if(e.platform==="telegram")return Q(Tt(e.target),e.pages);if(e.platform==="instagram")return l("instagram",(o)=>e.kind==="post"?c.post(o,et(e.target),{pages:e.pages}):e.kind==="account"?c.account(o,Et(e.target),{pages:e.pages}):c.tag(o,e.target,{pages:e.pages}));if(e.platform==="facebook")return l("facebook",(o)=>e.kind==="post"?m.post(o,e.target,{pages:e.pages}):e.kind==="account"?m.page(o,e.target,{pages:e.pages}):m.search(o,e.target,{pages:e.pages}));if(e.platform==="tiktok")return i((o,p)=>e.kind==="post"?o.video(At(e.target),{pages:e.pages,identity:p}):o.search(e.target,{videos:e.pages,pages:2,identity:p}));return g(async(o)=>{if(e.kind!=="post")return o.search(rt(e.kind,e.target),{pages:e.pages,lang:e.kind==="topic"?"in":void 0});let p=rt(e.kind,e.target).replace("conversation_id:",""),f=await o.conversation(p,{pages:e.pages}),h=new Set(f.actors.map((y)=>y.id)),w=(await o.retweeters(p).catch(()=>[])).filter((y)=>!h.has(y.id));return{...f,actors:[...f.actors,...w]}})}}}export{Ue as CREDENTIAL_FIELDS,Zt as PLATFORM_KINDS,lt as chromePath,Q as collectTelegram,Le as createCollector,z as createFacebookCollector,B as createInstagramCollector,X as createTiktokCollector,H as createXCollector,M as createYoutubeCollector,qe as probe,Ct as sessionFromEnv,Ut as sidecarFromEnv,ee as validate,rt as xQuery};
@@ -0,0 +1,20 @@
1
+ import { type Dataset } from "../engine";
2
+ import { type Session } from "./browser";
3
+ export declare const shortcode: (t: string) => string;
4
+ export declare const mediaPk: (code: string) => string;
5
+ export declare function createInstagramCollector(): {
6
+ me: (session: Session) => Promise<{
7
+ id: string;
8
+ handle: string;
9
+ name: string;
10
+ }>;
11
+ account: (session: Session, username: string, o: {
12
+ pages: number;
13
+ }) => Promise<Dataset>;
14
+ post: (session: Session, code: string, o: {
15
+ pages: number;
16
+ }) => Promise<Dataset>;
17
+ tag: (session: Session, tag: string, o: {
18
+ pages: number;
19
+ }) => Promise<Dataset>;
20
+ };
@@ -0,0 +1,2 @@
1
+ import { type Dataset } from "../engine";
2
+ export declare function collectTelegram(channels: string[], pages?: number): Promise<Dataset>;