Update index.js
Browse files
index.js
CHANGED
|
@@ -171,23 +171,18 @@ function httpPost(url, payload, timeout = 15000) {
|
|
| 171 |
});
|
| 172 |
}
|
| 173 |
|
| 174 |
-
//
|
| 175 |
-
// ✅ الإصلاح هنا: الطريقة الجديدة لجلب بيانات المستخدم
|
| 176 |
-
// =============================================
|
| 177 |
async function getUserDataWithRegion(username) {
|
| 178 |
try {
|
| 179 |
const response = await httpGet('https://www.tiktok.com/@' + username);
|
| 180 |
|
| 181 |
if (response.statusCode === 200) {
|
| 182 |
-
// ✅ الطريقة الجديدة: استخراج JSON من script#__UNIVERSAL_DATA_FOR_REHYDRATION__
|
| 183 |
-
// (نفس الطريقة المستخدمة في مشروع Tiksckren)
|
| 184 |
const pattern = /<script id="__UNIVERSAL_DATA_FOR_REHYDRATION__" type="application\/json">(.*?)<\/script>/s;
|
| 185 |
const match = response.data.match(pattern);
|
| 186 |
|
| 187 |
if (match) {
|
| 188 |
const data = JSON.parse(match[1]);
|
| 189 |
|
| 190 |
-
// ✅ استخدام متغيرات وسيطة لتجنب خطأ SyntaxError
|
| 191 |
const scope = data['__DEFAULT_SCOPE__'] || {};
|
| 192 |
const webapp = scope['webapp.user-detail'] || {};
|
| 193 |
const userInfo = webapp['userInfo'] || {};
|
|
@@ -201,13 +196,17 @@ async function getUserDataWithRegion(username) {
|
|
| 201 |
}
|
| 202 |
}
|
| 203 |
} catch (e) {
|
| 204 |
-
console.log("⚠️ خطأ:", e.message);
|
| 205 |
}
|
| 206 |
return null;
|
| 207 |
}
|
|
|
|
| 208 |
let maxViewers = 0, totalLikes = 0, totalShares = 0, giftersMap = {};
|
| 209 |
let pkActive = false, pkInfo = { opponentName: "", ourLikes: 0, theirLikes: 0 };
|
| 210 |
|
|
|
|
|
|
|
|
|
|
| 211 |
async function sendToTelegram(text, photoUrl = null, buttons = null) {
|
| 212 |
try {
|
| 213 |
const payload = {
|
|
@@ -233,6 +232,22 @@ function getTopGifters(map, top = 5) {
|
|
| 233 |
return Object.values(map).sort((a, b) => b.coins - a.coins).slice(0, top);
|
| 234 |
}
|
| 235 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
let tiktokConnection = new WebcastPushConnection(TIKTOK_USERNAME, {
|
| 237 |
processInitialData: true,
|
| 238 |
enableWebsocketUpgrade: true,
|
|
@@ -245,8 +260,7 @@ let tiktokConnection = new WebcastPushConnection(TIKTOK_USERNAME, {
|
|
| 245 |
|
| 246 |
async function startRadar() {
|
| 247 |
console.log('⏳ جاري البحث: ' + TIKTOK_USERNAME + '...');
|
| 248 |
-
|
| 249 |
-
// ✅ الآن ستعمل هذه الدالة بشكل صحيح!
|
| 250 |
const userData = await getUserDataWithRegion(TIKTOK_USERNAME);
|
| 251 |
const countryInfo = getCountryInfo(userData?.region);
|
| 252 |
|
|
@@ -256,6 +270,7 @@ async function startRadar() {
|
|
| 256 |
totalShares = 0;
|
| 257 |
giftersMap = {};
|
| 258 |
pkActive = false;
|
|
|
|
| 259 |
|
| 260 |
const liveLink = 'https://www.tiktok.com/@' + TIKTOK_USERNAME + '/live';
|
| 261 |
const msg = '━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n' +
|
|
@@ -280,6 +295,7 @@ async function startRadar() {
|
|
| 280 |
});
|
| 281 |
}
|
| 282 |
|
|
|
|
| 283 |
tiktokConnection.on('roomUser', data => {
|
| 284 |
if (data.viewerCount > maxViewers) maxViewers = data.viewerCount;
|
| 285 |
});
|
|
@@ -291,31 +307,88 @@ tiktokConnection.on('like', data => {
|
|
| 291 |
|
| 292 |
tiktokConnection.on('share', () => totalShares++);
|
| 293 |
|
| 294 |
-
|
|
|
|
| 295 |
if (data.giftType === 1 && !data.repeatEnd) return;
|
|
|
|
| 296 |
const giftCost = (data.diamondCount || 0) * (data.repeatCount || 1);
|
|
|
|
|
|
|
|
|
|
| 297 |
if (!giftersMap[data.userId]) {
|
| 298 |
giftersMap[data.userId] = {
|
| 299 |
name: data.nickname,
|
|
|
|
| 300 |
coins: 0,
|
| 301 |
-
region:
|
|
|
|
| 302 |
};
|
| 303 |
}
|
| 304 |
giftersMap[data.userId].coins += giftCost;
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
});
|
| 313 |
|
| 314 |
tiktokConnection.on('pkStart', data => {
|
| 315 |
pkActive = true;
|
| 316 |
pkInfo = { opponentName: data.opponentNickname, ourLikes: 0, theirLikes: 0 };
|
| 317 |
sendToTelegram(
|
| 318 |
-
'🔥 جولة PK!\
|
| 319 |
null,
|
| 320 |
[[{ text: "🚀 دعم البث", url: 'https://www.tiktok.com/@' + TIKTOK_USERNAME + '/live' }]]
|
| 321 |
);
|
|
@@ -330,9 +403,9 @@ tiktokConnection.on('pkEnd', data => {
|
|
| 330 |
pkActive = false;
|
| 331 |
const bar = generateProgressBar(pkInfo.ourLikes, pkInfo.theirLikes);
|
| 332 |
sendToTelegram(
|
| 333 |
-
'🏆 انتهت المواجهة!\
|
| 334 |
-
'\
|
| 335 |
-
formatK(pkInfo.theirLikes) + ' :المنافس 🟥\
|
| 336 |
);
|
| 337 |
});
|
| 338 |
|
|
@@ -340,8 +413,8 @@ tiktokConnection.on('streamEnd', () => {
|
|
| 340 |
const top = getTopGifters(giftersMap, 5);
|
| 341 |
const medals = ['🥇', '🥈', '🥉', '4️⃣', '5️⃣'];
|
| 342 |
let topText = top.map((g, i) =>
|
| 343 |
-
medals[i] + ' ' +
|
| 344 |
-
).join('\
|
| 345 |
|
| 346 |
const countryStats = {};
|
| 347 |
Object.values(giftersMap).forEach(g => {
|
|
@@ -355,15 +428,15 @@ tiktokConnection.on('streamEnd', () => {
|
|
| 355 |
.map(([r, c], i) =>
|
| 356 |
['👑', '🥈', '🥉'][i] + ' ' + getCountryInfo(r).flag + ' ' +
|
| 357 |
getCountryInfo(r).name + ': ' + formatK(c) + ' 🪙'
|
| 358 |
-
).join('\
|
| 359 |
|
| 360 |
sendToTelegram(
|
| 361 |
-
'📊 التقرير النهائي\
|
| 362 |
'📈 أعلى مشاهدات: ' + formatK(maxViewers) + '\n' +
|
| 363 |
'❤️ إعجابات: ' + formatK(totalLikes) + '\n' +
|
| 364 |
-
'↪️ مشاركات: ' + formatK(totalShares) + '\
|
| 365 |
-
'🏆 الداعمين:\
|
| 366 |
-
'🌍 توزيع حسب البلد:\
|
| 367 |
);
|
| 368 |
|
| 369 |
setTimeout(startRadar, 60000);
|
|
|
|
| 171 |
});
|
| 172 |
}
|
| 173 |
|
| 174 |
+
// ✅ الدالة الرئيسية: جلب بيانات المستخدم + الدولة من صفحته
|
|
|
|
|
|
|
| 175 |
async function getUserDataWithRegion(username) {
|
| 176 |
try {
|
| 177 |
const response = await httpGet('https://www.tiktok.com/@' + username);
|
| 178 |
|
| 179 |
if (response.statusCode === 200) {
|
|
|
|
|
|
|
| 180 |
const pattern = /<script id="__UNIVERSAL_DATA_FOR_REHYDRATION__" type="application\/json">(.*?)<\/script>/s;
|
| 181 |
const match = response.data.match(pattern);
|
| 182 |
|
| 183 |
if (match) {
|
| 184 |
const data = JSON.parse(match[1]);
|
| 185 |
|
|
|
|
| 186 |
const scope = data['__DEFAULT_SCOPE__'] || {};
|
| 187 |
const webapp = scope['webapp.user-detail'] || {};
|
| 188 |
const userInfo = webapp['userInfo'] || {};
|
|
|
|
| 196 |
}
|
| 197 |
}
|
| 198 |
} catch (e) {
|
| 199 |
+
console.log("⚠️ خطأ في جلب بيانات " + username + ":", e.message);
|
| 200 |
}
|
| 201 |
return null;
|
| 202 |
}
|
| 203 |
+
|
| 204 |
let maxViewers = 0, totalLikes = 0, totalShares = 0, giftersMap = {};
|
| 205 |
let pkActive = false, pkInfo = { opponentName: "", ourLikes: 0, theirLikes: 0 };
|
| 206 |
|
| 207 |
+
// ✅ كاش لحفظ بيانات المستخدمين - ما نرجع نطلبهم تاني
|
| 208 |
+
const userCache = {};
|
| 209 |
+
|
| 210 |
async function sendToTelegram(text, photoUrl = null, buttons = null) {
|
| 211 |
try {
|
| 212 |
const payload = {
|
|
|
|
| 232 |
return Object.values(map).sort((a, b) => b.coins - a.coins).slice(0, top);
|
| 233 |
}
|
| 234 |
|
| 235 |
+
// ✅ جلب بيانات المستخدم مع الكاش
|
| 236 |
+
async function resolveUser(gifterUsername) {
|
| 237 |
+
if (!gifterUsername || gifterUsername === gifterUserId) return null;
|
| 238 |
+
|
| 239 |
+
// إذا عندنا بياناته من قبل، نوصلها مباشرة
|
| 240 |
+
if (userCache[gifterUsername]) return userCache[gifterUsername];
|
| 241 |
+
|
| 242 |
+
// نطلب بياناته من صفحته
|
| 243 |
+
const userData = await getUserDataWithRegion(gifterUsername);
|
| 244 |
+
if (userData) {
|
| 245 |
+
userCache[gifterUsername] = userData;
|
| 246 |
+
return userData;
|
| 247 |
+
}
|
| 248 |
+
return null;
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
let tiktokConnection = new WebcastPushConnection(TIKTOK_USERNAME, {
|
| 252 |
processInitialData: true,
|
| 253 |
enableWebsocketUpgrade: true,
|
|
|
|
| 260 |
|
| 261 |
async function startRadar() {
|
| 262 |
console.log('⏳ جاري البحث: ' + TIKTOK_USERNAME + '...');
|
| 263 |
+
userCache = {};
|
|
|
|
| 264 |
const userData = await getUserDataWithRegion(TIKTOK_USERNAME);
|
| 265 |
const countryInfo = getCountryInfo(userData?.region);
|
| 266 |
|
|
|
|
| 270 |
totalShares = 0;
|
| 271 |
giftersMap = {};
|
| 272 |
pkActive = false;
|
| 273 |
+
userCache = {};
|
| 274 |
|
| 275 |
const liveLink = 'https://www.tiktok.com/@' + TIKTOK_USERNAME + '/live';
|
| 276 |
const msg = '━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n' +
|
|
|
|
| 295 |
});
|
| 296 |
}
|
| 297 |
|
| 298 |
+
// ✅ أحداث الاتصال
|
| 299 |
tiktokConnection.on('roomUser', data => {
|
| 300 |
if (data.viewerCount > maxViewers) maxViewers = data.viewerCount;
|
| 301 |
});
|
|
|
|
| 307 |
|
| 308 |
tiktokConnection.on('share', () => totalShares++);
|
| 309 |
|
| 310 |
+
// ✅ إشعارات الهدايا مع جلب دولة المرسِل
|
| 311 |
+
tiktokConnection.on('gift', async data => {
|
| 312 |
if (data.giftType === 1 && !data.repeatEnd) return;
|
| 313 |
+
|
| 314 |
const giftCost = (data.diamondCount || 0) * (data.repeatCount || 1);
|
| 315 |
+
const gifterUsername = data.uniqueId || '';
|
| 316 |
+
|
| 317 |
+
// تخزين البيانات في الخريطة
|
| 318 |
if (!giftersMap[data.userId]) {
|
| 319 |
giftersMap[data.userId] = {
|
| 320 |
name: data.nickname,
|
| 321 |
+
username: gifterUsername,
|
| 322 |
coins: 0,
|
| 323 |
+
region: null,
|
| 324 |
+
flag: '🌍'
|
| 325 |
};
|
| 326 |
}
|
| 327 |
giftersMap[data.userId].coins += giftCost;
|
| 328 |
+
|
| 329 |
+
// ✅ جلب بيانات الدولة إذا الهدية >= 100
|
| 330 |
+
if (giftCost >= 100 && gifterUsername) {
|
| 331 |
+
console.log('🔍 جاري جلب بيانات: ' + gifterUsername);
|
| 332 |
+
const userRegionData = await resolveUser(gifterUsername);
|
| 333 |
+
|
| 334 |
+
let region = null;
|
| 335 |
+
let nickname = data.nickname;
|
| 336 |
+
|
| 337 |
+
if (userRegionData) {
|
| 338 |
+
region = userRegionData.region;
|
| 339 |
+
nickname = userRegionData.nickname;
|
| 340 |
+
giftersMap[data.userId].region = region;
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
const gc = getCountryInfo(region);
|
| 344 |
+
giftersMap[data.userId].flag = gc.flag;
|
| 345 |
+
|
| 346 |
+
const giftName = data.giftName || 'هدية';
|
| 347 |
+
const msg = '🎁 هدية جديدة!\n\n' +
|
| 348 |
+
gc.flag + ' ' + nickname + ' @' + gifterUsername + '\n' +
|
| 349 |
+
'🎀 ' + giftName + '\n' +
|
| 350 |
+
'💰 ' + formatK(giftCost) + ' 🪙\n' +
|
| 351 |
+
'🌍 ' + gc.name;
|
| 352 |
+
|
| 353 |
+
sendToTelegram(msg, null, [[
|
| 354 |
+
{ text: "👤 حسابه", url: 'https://www.tiktok.com/@' + gifterUsername },
|
| 355 |
+
{ text: "📺 دخول البث", url: 'https://www.tiktok.com/@' + TIKTOK_USERNAME + '/live' }
|
| 356 |
+
]]);
|
| 357 |
+
}
|
| 358 |
+
});
|
| 359 |
+
|
| 360 |
+
// ✅ دخول مشاهد جديد مع محاولة جلب دولته
|
| 361 |
+
tiktokConnection.on('member', async data => {
|
| 362 |
+
const memberUsername = data.uniqueId || '';
|
| 363 |
+
if (!memberUsername) return;
|
| 364 |
+
|
| 365 |
+
const memberData = await resolveUser(memberUsername);
|
| 366 |
+
let region = null;
|
| 367 |
+
let nickname = data.nickname;
|
| 368 |
+
|
| 369 |
+
if (memberData) {
|
| 370 |
+
region = memberData.region;
|
| 371 |
+
nickname = memberData.nickname;
|
| 372 |
}
|
| 373 |
+
|
| 374 |
+
const gc = getCountryInfo(region);
|
| 375 |
+
const msg = '👤 دخل البث!\n\n' +
|
| 376 |
+
gc.flag + ' ' + nickname + ' @' + memberUsername + '\n' +
|
| 377 |
+
'🌍 ' + gc.name;
|
| 378 |
+
|
| 379 |
+
sendToTelegram(msg);
|
| 380 |
+
});
|
| 381 |
+
|
| 382 |
+
// ✅ دخول مشرف
|
| 383 |
+
tiktokConnection.on('member', async data => {
|
| 384 |
+
// نفس منطق member - إذا حابب تفرق المشرفين، استخدم حدث مناسب
|
| 385 |
});
|
| 386 |
|
| 387 |
tiktokConnection.on('pkStart', data => {
|
| 388 |
pkActive = true;
|
| 389 |
pkInfo = { opponentName: data.opponentNickname, ourLikes: 0, theirLikes: 0 };
|
| 390 |
sendToTelegram(
|
| 391 |
+
'🔥 جولة PK!\n\n🥊 المنافس: ' + data.opponentNickname + '\n\nشدوا الحيل!',
|
| 392 |
null,
|
| 393 |
[[{ text: "🚀 دعم البث", url: 'https://www.tiktok.com/@' + TIKTOK_USERNAME + '/live' }]]
|
| 394 |
);
|
|
|
|
| 403 |
pkActive = false;
|
| 404 |
const bar = generateProgressBar(pkInfo.ourLikes, pkInfo.theirLikes);
|
| 405 |
sendToTelegram(
|
| 406 |
+
'🏆 انتهت المواجهة!\n\n🥇 الفائز: ' + (data.winnerNickname || 'تعادل') +
|
| 407 |
+
'\n\n📊 النتيجة:\n🟦 نحن: ' + formatK(pkInfo.ourLikes) + ' | ' +
|
| 408 |
+
formatK(pkInfo.theirLikes) + ' :المنافس 🟥\n\n' + bar
|
| 409 |
);
|
| 410 |
});
|
| 411 |
|
|
|
|
| 413 |
const top = getTopGifters(giftersMap, 5);
|
| 414 |
const medals = ['🥇', '🥈', '🥉', '4️⃣', '5️⃣'];
|
| 415 |
let topText = top.map((g, i) =>
|
| 416 |
+
medals[i] + ' ' + (g.flag || '🌍') + ' ' + g.name + ': ' + formatK(g.coins) + ' 🪙'
|
| 417 |
+
).join('\n') || '> لا داعمين';
|
| 418 |
|
| 419 |
const countryStats = {};
|
| 420 |
Object.values(giftersMap).forEach(g => {
|
|
|
|
| 428 |
.map(([r, c], i) =>
|
| 429 |
['👑', '🥈', '🥉'][i] + ' ' + getCountryInfo(r).flag + ' ' +
|
| 430 |
getCountryInfo(r).name + ': ' + formatK(c) + ' 🪙'
|
| 431 |
+
).join('\n') || '> لا بيانات';
|
| 432 |
|
| 433 |
sendToTelegram(
|
| 434 |
+
'📊 التقرير النهائي\n\n━━━━━━━━━━━━━━━━\n' +
|
| 435 |
'📈 أعلى مشاهدات: ' + formatK(maxViewers) + '\n' +
|
| 436 |
'❤️ إعجابات: ' + formatK(totalLikes) + '\n' +
|
| 437 |
+
'↪️ مشاركات: ' + formatK(totalShares) + '\n\n' +
|
| 438 |
+
'🏆 الداعمين:\n' + topText + '\n\n' +
|
| 439 |
+
'🌍 توزيع حسب البلد:\n' + countryRanking + '\n\n💤 انتظار...'
|
| 440 |
);
|
| 441 |
|
| 442 |
setTimeout(startRadar, 60000);
|