File size: 4,472 Bytes
10aa8bf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | from playwright.sync_api import sync_playwright
import json
import time
from datetime import datetime
class RealtimeJSONInterceptor:
def init(self):
self.collected_data = {}
self.output_file = "fmp_data.json" # Simple file name
def handle_response(self, response):
"""Network Response တိုင်းကို ကြားဖြတ်ဖမ်းယူမည့် Function"""
try:
# Content-Type က JSON ဟုတ်/မဟုတ် စစ်ဆေးခြင်း
content_type = response.headers.get('content-type', '')
if 'json' not in content_type.lower():
return
# URL က api.fmp.live/query ဟုတ်/မဟုတ် စစ်ဆေးခြင်း
if 'api.fmp.live/query' not in response.url:
return
# Response ကို JSON အဖြစ် ဖတ်ခြင်း
data = response.json()
# Data ထဲမှာ "data" key ပါ/မပါ စစ်ဆေးခြင်း
if 'data' not in data:
return
# Data ထဲက key တွေကို ကျွန်တော်တို့ dictionary ထဲ ထည့်ခြင်း
for key, value in data['data'].items():
# အရင် data နဲ့ တူ/မတူ စစ်ဆေးခြင်း (Update ဖြစ်မှသာ ပြမယ်)
if key not in self.collected_data or self.collected_data[key] != value:
self.collected_data[key] = value
# Console မှာ Live Update ပြသခြင်း
timestamp = datetime.now().strftime("%H:%M:%S")
print(f"[{timestamp}] ✅ {key} - {self._get_summary(value)}")
# JSON file ကို အမြဲ update လုပ်ခြင်း
self.save_to_file()
except Exception as e:
pass
def _get_summary(self, value):
"""Data အကျဉ်းချုပ်ကို ပြသရန်"""
if isinstance(value, list):
return f"{len(value)} items"
elif isinstance(value, dict):
return f"{len(value)} keys"
else:
return str(value)[:30]
def save_to_file(self):
"""Collected data ကို JSON file အဖြစ် save လုပ်မည်"""
try:
with open(self.output_file, 'w', encoding='utf-8') as f:
json.dump(self.collected_data, f, indent=2, ensure_ascii=False)
except Exception as e:
print(f"❌ File save error: {e}")
def run(self):
"""Main function - Browser ကို ဖွင့်ပြီး monitoring စတင်မည်"""
print("="*60)
print("🚀 FMP Live Real-time JSON Interceptor")
print("="*60)
print(f"📁 Data ကို '{self.output_file}' တွင် save လုပ်နေပါမည်")
print("⏹️ ရပ်ရန် Ctrl+C ကို နှိပ်ပါ\n")
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
page = browser.new_page()
page.on("response", self.handle_response)
print("🌐 fmp.live သို့ ချိတ်ဆက်နေပါသည်...")
try:
page.goto("https://fmp.live/", wait_until="networkidle")
print("✅ Website load ဖြစ်ပါပြီ")
print("👀 Real-time monitoring စတင်ပါပြီ...\n")
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\n\n⏹️ User မှ ရပ်ဆိုင်းလိုက်ပါသည်")
except Exception as e:
print(f"\n❌ Error: {e}")
finally:
self.save_to_file()
print(f"\n✅ နောက်ဆုံး data ကို '{self.output_file}' တွင် သိမ်းဆည်းပြီးပါပြီ")
browser.close()
if name == "main":
interceptor = RealtimeJSONInterceptor()
interceptor.run() |