This repository was archived by the owner on Aug 19, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
685 lines (619 loc) · 31.9 KB
/
Copy pathserver.py
File metadata and controls
685 lines (619 loc) · 31.9 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
import os
import re
import time
import json
import requests
import threading
import logging
import random
from datetime import datetime
from flask import Flask, jsonify, request, Response
from flask_cors import CORS
from flask_socketio import SocketIO, emit
from functools import wraps
app = Flask(__name__)
CORS(app)
socketio = SocketIO(app, cors_allowed_origins="*", async_mode='eventlet')
HTML_CONTENT = None
JS_CONTENT = None
SERVICES_CONTENT = None
NEWS_API_KEY = os.environ.get('NEWS_API_KEY', '')
USE_NEWS_API = os.environ.get('USE_NEWS_API', 'false').lower() == 'true'
LLM_API_KEY = os.environ.get('LLM_API_KEY', '')
USER_AGENTS = [
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/119.0'
]
def get_headers():
return {
'User-Agent': random.choice(USER_AGENTS),
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'en-US,en;q=0.9',
}
class RateLimiter:
def __init__(self):
self.calls = {}
self.failures = {}
self.circuit_open = {}
def check_limit(self, key, max_calls=10, window=60):
now = time.time()
if key not in self.calls:
self.calls[key] = []
self.calls[key] = [t for t in self.calls[key] if now - t < window]
if len(self.calls[key]) >= max_calls:
return False
self.calls[key].append(now)
return True
def record_failure(self, key):
self.failures[key] = self.failures.get(key, 0) + 1
if self.failures[key] >= 3:
self.circuit_open[key] = time.time() + 60
def is_circuit_open(self, key):
if key in self.circuit_open:
if time.time() > self.circuit_open[key]:
del self.circuit_open[key]
self.failures[key] = 0
return False
return True
return False
rate_limiter = RateLimiter()
FALLBACK_EVENTS = [
{'id': 'fallback_1', 'category': 'news', 'title': 'Global Watch - System Operational', 'description': 'Real-time monitoring active. Data refresh scheduled.', 'lat': 40.7, 'lng': -74.0, 'source': 'System', 'time': int(time.time()*1000), 'severity': 'low'},
{'id': 'fallback_2', 'category': 'news', 'title': 'Monitoring Active - Regions Online', 'description': 'All data sources connected. Live tracking enabled.', 'lat': 51.5, 'lng': -0.12, 'source': 'System', 'time': int(time.time()*1000)-3600000, 'severity': 'low'}
]
USGS_API = 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_day.geojson'
NOAA_HAZARDS_API = 'https://geo.weather.gov/hazards/v1/public/active'
COUNTRY_COORDS = {
'US': {'lat': 37.09, 'lng': -95.71, 'name': 'United States'},
'CN': {'lat': 35.86, 'lng': 104.19, 'name': 'China'},
'RU': {'lat': 61.52, 'lng': 105.31, 'name': 'Russia'},
'IN': {'lat': 20.59, 'lng': 78.96, 'name': 'India'},
'BR': {'lat': -14.23, 'lng': -51.92, 'name': 'Brazil'},
'GB': {'lat': 55.37, 'lng': -3.43, 'name': 'United Kingdom'},
'FR': {'lat': 46.22, 'lng': 2.21, 'name': 'France'},
'DE': {'lat': 51.16, 'lng': 10.45, 'name': 'Germany'},
'JP': {'lat': 36.20, 'lng': 138.25, 'name': 'Japan'},
'IL': {'lat': 31.04, 'lng': 34.85, 'name': 'Israel'},
'UA': {'lat': 48.37, 'lng': 31.16, 'name': 'Ukraine'},
'IR': {'lat': 32.42, 'lng': 53.68, 'name': 'Iran'},
'KP': {'lat': 40.33, 'lng': 127.51, 'name': 'North Korea'},
'PK': {'lat': 30.37, 'lng': 69.34, 'name': 'Pakistan'},
'TR': {'lat': 38.96, 'lng': 35.24, 'name': 'Turkey'},
'SA': {'lat': 23.88, 'lng': 45.07, 'name': 'Saudi Arabia'},
'ZA': {'lat': -30.55, 'lng': 22.93, 'name': 'South Africa'},
'AU': {'lat': -25.27, 'lng': 133.77, 'name': 'Australia'},
'CA': {'lat': 56.13, 'lng': -106.34, 'name': 'Canada'},
'MX': {'lat': 23.63, 'lng': -102.55, 'name': 'Mexico'},
'EG': {'lat': 26.82, 'lng': 30.80, 'name': 'Egypt'},
'KR': {'lat': 35.90, 'lng': 127.76, 'name': 'South Korea'},
'AF': {'lat': 33.93, 'lng': 67.70, 'name': 'Afghanistan'},
'SY': {'lat': 34.80, 'lng': 38.99, 'name': 'Syria'},
'IQ': {'lat': 33.22, 'lng': 43.67, 'name': 'Iraq'},
'LY': {'lat': 26.33, 'lng': 17.22, 'name': 'Libya'},
'YE': {'lat': 15.55, 'lng': 48.51, 'name': 'Yemen'},
'SD': {'lat': 12.86, 'lng': 30.21, 'name': 'Sudan'},
'ET': {'lat': 9.14, 'lng': 40.48, 'name': 'Ethiopia'},
'NG': {'lat': 9.08, 'lng': 8.67, 'name': 'Nigeria'},
'CO': {'lat': 4.57, 'lng': -74.29, 'name': 'Colombia'},
'VE': {'lat': 6.42, 'lng': -66.58, 'name': 'Venezuela'},
'AR': {'lat': -38.41, 'lng': -63.61, 'name': 'Argentina'},
'ID': {'lat': -0.78, 'lng': 113.92, 'name': 'Indonesia'},
'TH': {'lat': 15.87, 'lng': 100.99, 'name': 'Thailand'},
'MM': {'lat': 21.91, 'lng': 95.95, 'name': 'Myanmar'},
'VN': {'lat': 14.05, 'lng': 108.27, 'name': 'Vietnam'},
'PH': {'lat': 12.87, 'lng': 121.77, 'name': 'Philippines'},
'MY': {'lat': 4.21, 'lng': 101.97, 'name': 'Malaysia'},
'SG': {'lat': 1.35, 'lng': 103.81, 'name': 'Singapore'},
'NZ': {'lat': -40.90, 'lng': 174.88, 'name': 'New Zealand'},
'GR': {'lat': 39.07, 'lng': 21.82, 'name': 'Greece'},
'IT': {'lat': 41.87, 'lng': 12.56, 'name': 'Italy'},
'ES': {'lat': 40.46, 'lng': -3.74, 'name': 'Spain'},
'PL': {'lat': 51.91, 'lng': 19.14, 'name': 'Poland'},
'SE': {'lat': 60.12, 'lng': 18.64, 'name': 'Sweden'},
'NO': {'lat': 60.47, 'lng': 8.46, 'name': 'Norway'},
'FI': {'lat': 61.92, 'lng': 25.74, 'name': 'Finland'},
'NL': {'lat': 52.13, 'lng': 5.29, 'name': 'Netherlands'},
'BE': {'lat': 50.50, 'lng': 4.46, 'name': 'Belgium'},
'CH': {'lat': 46.81, 'lng': 8.22, 'name': 'Switzerland'},
'AT': {'lat': 47.51, 'lng': 14.55, 'name': 'Austria'},
'CZ': {'lat': 49.81, 'lng': 15.47, 'name': 'Czech Republic'},
'HU': {'lat': 47.16, 'lng': 19.50, 'name': 'Hungary'},
'RO': {'lat': 45.94, 'lng': 24.96, 'name': 'Romania'},
'BG': {'lat': 42.73, 'lng': 25.48, 'name': 'Bulgaria'},
'RS': {'lat': 44.01, 'lng': 21.00, 'name': 'Serbia'},
'HR': {'lat': 45.10, 'lng': 15.20, 'name': 'Croatia'},
'TW': {'lat': 23.69, 'lng': 120.96, 'name': 'Taiwan'},
}
class GlobalWatchData:
MAX_EVENTS = 200
def __init__(self):
self.events = []
self.regions = {}
self.last_update = 0
self.cache_ttl = 60
self.source_last_fetch = {}
self._init_regions()
def _safe_request(self, url, headers=None, timeout=10):
if rate_limiter.is_circuit_open(url):
return None
try:
h = headers or get_headers()
resp = requests.get(url, headers=h, timeout=timeout)
resp.raise_for_status()
return resp
except Exception as e:
logging.error(f"Request to {url} failed: {e}")
rate_limiter.record_failure(url)
return None
def _prune_events(self):
if len(self.events) > self.MAX_EVENTS:
self.events = sorted(self.events, key=lambda x: x.get('time', 0), reverse=True)
self.events = self.events[:self.MAX_EVENTS]
logging.info(f'Pruned events, kept {len(self.events)}')
def _init_regions(self):
for code, info in COUNTRY_COORDS.items():
self.regions[code] = {
'code': code,
'name': info['name'],
'lat': info['lat'],
'lng': info['lng'],
'score': 0,
'events': [],
'categories': {'news': 0, 'earthquake': 0, 'conflict': 0, 'tech': 0}
}
def _deduplicate(self):
seen = {}
unique = []
for e in self.events:
key = e.get('title', '')[:50].lower()
existing = seen.get(key)
if existing is None:
seen[key] = e
unique.append(e)
else:
sev_order = {'critical': 4, 'high': 3, 'medium': 2, 'low': 1}
if sev_order.get(e.get('severity'), 0) > sev_order.get(existing.get('severity'), 0):
seen[key] = e
unique[-1] = e
self.events = unique
def ensure_fresh(self, force=False):
if force or not self.events or (time.time() - self.last_update) > self.cache_ttl:
success = False
try:
self._fetch_earthquakes()
success = True
except Exception as e:
logging.error(f"Earthquake fetch failed: {e}")
try:
self._fetch_news()
success = True
except Exception as e:
logging.error(f"News fetch failed: {e}")
self._deduplicate()
if not success and len(self.events) == 0:
logging.warning('All APIs failed, using fallback events')
self.events = FALLBACK_EVENTS.copy()
self._calculate_scores()
self._prune_events()
self.last_update = time.time()
self.source_last_fetch['all'] = self.last_update
socketio.emit('update', {'events': self.events, 'regions': self.regions, 'timestamp': self.last_update})
def _fetch_earthquakes(self):
logging.info('Fetching earthquakes from USGS')
resp = self._safe_request(USGS_API)
if resp and resp.status_code == 200:
try:
data = resp.json()
self.events = [e for e in self.events if e.get('category') != 'earthquake']
for quake in data.get('features', [])[:20]:
props = quake['properties']
coords = quake['geometry']['coordinates']
mag = props['mag']
depth = coords[2]
tsunami = 'Tsunami WATCH' if props.get('tsunami', 0) == 2 else 'Tsunami WARNING' if props.get('tsunami', 0) == 1 else 'No tsunami threat'
event = {
'id': f"quake_{quake['id']}",
'category': 'earthquake',
'title': f"M{mag} Earthquake - {props['place']}",
'description': f"Sent at depth of {depth:.1f}km. {tsunami}. Felt: {props.get('felt', 0)}. Sig: {props.get('sig', 0)}.",
'lat': coords[1], 'lng': coords[0],
'magnitude': mag, 'source': 'USGS',
'url': props.get('url', ''), 'time': props['time'],
'severity': 'critical' if mag >= 6 else 'high' if mag >= 5 else 'medium'
}
self.events.append(event)
self._assign_to_region(event)
except Exception as e:
logging.error(f"Earthquake parse error: {e}")
def _fetch_news(self):
logging.info('Fetching news')
self.events = [e for e in self.events if e.get('category') == 'earthquake']
self._fetch_hackernews()
self._fetch_worldnews()
def _fetch_worldnews(self):
resp = self._safe_request('https://www.reddit.com/r/worldnews/hot.json?limit=15')
if resp and resp.status_code == 200:
try:
data = resp.json()
posts = data.get('data', {}).get('children', [])
for post in posts[:12]:
p = post.get('data', {})
if not p.get('title'): continue
lat, lng = self._guess_location_from_title(p['title'])
category = self._categorize_news({'title': p['title'], 'description': ''})
desc = f"{p.get('score', 0)} upvotes • {p.get('num_comments', 0)} comments"
if p.get('url') and 'reddit.com' not in p['url']:
article = extract_article_text(p['url'])
if article: desc = article
event = {
'id': f"reddit_{p['id']}", 'category': category,
'title': p['title'][:120], 'description': desc,
'lat': lat or 0, 'lng': lng or 0,
'source': f'r/{p.get("subreddit", "worldnews")}',
'url': f"https://reddit.com{p.get('permalink', '')}",
'time': int(p.get('created_utc', time.time())) * 1000,
'severity': self._assess_severity(p['title'], '')
}
self.events.append(event)
self._assign_to_region(event)
except Exception as e: logging.error(f"Reddit parse error: {e}")
def _fetch_hackernews(self):
try:
resp = self._safe_request('https://hacker-news.firebaseio.com/v0/topstories.json')
if not resp: return
top_stories = resp.json()[:15]
for story_id in top_stories:
s_resp = self._safe_request(f'https://hacker-news.firebaseio.com/v0/item/{story_id}.json')
if not s_resp: continue
story = s_resp.json()
if not story or not story.get('title'): continue
lat, lng = self._guess_location_from_title(story['title'])
desc = f"{story.get('score', 0)} points • {story.get('descendants', 0)} comments"
if story.get('url'):
article = extract_article_text(story['url'])
if article: desc = article
event = {
'id': f"hn_{story_id}", 'category': 'tech',
'title': story['title'][:120], 'description': desc,
'lat': lat or 37.77, 'lng': lng or -122.41,
'source': 'Hacker News', 'url': story.get('url', f'https://news.ycombinator.com/item?id={story_id}'),
'time': story.get('time', 0) * 1000, 'severity': 'medium' if story.get('score', 0) > 100 else 'low'
}
self.events.append(event)
self._assign_to_region(event)
except Exception as e: logging.error(f"HN error: {e}")
def _guess_location_from_title(self, text):
text_lower = text.lower()
# Expanded geocoder (Task 2)
location_map = {
'israel': (31.04, 34.85), 'gaza': (31.35, 34.30), 'palestine': (31.95, 35.15),
'ukraine': (48.37, 31.16), 'russia': (61.52, 105.31), 'moscow': (55.75, 37.61),
'china': (35.86, 104.19), 'beijing': (39.90, 116.40), 'shanghai': (31.23, 121.47),
'usa': (37.09, -95.71), 'washington': (38.90, -77.03), 'new york': (40.71, -74.00),
'uk': (55.37, -3.43), 'london': (51.50, -0.12), 'france': (46.22, 2.21), 'paris': (48.85, 2.35),
'germany': (51.16, 10.45), 'berlin': (52.52, 13.40), 'japan': (36.20, 138.25), 'tokyo': (35.67, 139.65),
'india': (20.59, 78.96), 'delhi': (28.61, 77.20), 'mumbai': (19.07, 72.87),
'iran': (32.42, 53.68), 'tehran': (35.68, 51.38), 'iraq': (33.22, 43.67), 'baghdad': (33.31, 44.36),
'syria': (34.80, 38.99), 'turkey': (38.96, 35.24), 'istanbul': (41.00, 28.97),
'north korea': (40.33, 127.51), 'south korea': (35.90, 127.76), 'seoul': (37.56, 126.97),
'taiwan': (23.69, 120.96), 'australia': (-25.27, 133.77), 'brazil': (-14.23, -51.92),
'ghana': (7.94, -1.02), 'accra': (5.60, -0.18), 'nigeria': (9.08, 8.67), 'lagos': (6.52, 3.37),
'egypt': (26.82, 30.80), 'cairo': (30.04, 31.23), 'south africa': (-30.55, 22.93),
'mexico': (23.63, -102.55), 'canada': (56.13, -106.34), 'toronto': (43.65, -79.38),
'afghanistan': (33.93, 67.70), 'kabul': (34.55, 69.20), 'algeria': (28.03, 1.65),
'argentina': (-38.41, -63.61), 'buenos aires': (-34.60, -58.38), 'austria': (47.51, 14.55),
'vienna': (48.20, 16.37), 'bangladesh': (23.68, 90.35), 'dhaka': (23.81, 90.41),
'belgium': (50.85, 4.35), 'brussels': (50.85, 4.35), 'bolivia': (-16.29, -63.58),
'colombia': (4.57, -74.29), 'bogota': (4.71, -74.07), 'croatia': (45.10, 15.20),
'cuba': (21.52, -77.78), 'havana': (23.11, -82.36), 'czech': (49.81, 15.47),
'prague': (50.07, 14.43), 'denmark': (56.26, 9.50), 'copenhagen': (55.67, 12.56),
'ecuador': (-1.83, -78.18), 'ethiopia': (9.14, 40.48), 'addis ababa': (9.03, 38.74),
'finland': (61.92, 25.74), 'helsinki': (60.16, 24.93), 'greece': (39.07, 21.82),
'athens': (37.98, 23.71), 'hong kong': (22.31, 114.16), 'hungary': (47.16, 19.50),
'budapest': (47.49, 19.04), 'iceland': (64.96, -19.02), 'indonesia': (-0.78, 113.92),
'jakarta': (-6.20, 106.81), 'ireland': (53.14, -8.24), 'dublin': (53.34, -6.26),
'italy': (41.87, 12.56), 'rome': (41.89, 12.48), 'milan': (45.46, 9.18),
'jordan': (30.58, 36.23), 'amman': (31.94, 35.93), 'kazakhstan': (48.01, 66.92),
'kenya': (-0.02, 37.90), 'nairobi': (-1.28, 36.81), 'kuwait': (29.31, 47.48),
'lebanon': (33.85, 35.86), 'beirut': (33.88, 35.49), 'libya': (26.33, 17.22),
'malaysia': (4.21, 101.97), 'kuala lumpur': (3.15, 101.70), 'morocco': (31.79, -7.09),
'rabat': (34.01, -6.83), 'myanmar': (21.91, 95.95), 'nepal': (28.39, 84.12),
'netherlands': (52.13, 5.29), 'amsterdam': (52.36, 4.90), 'new zealand': (-40.90, 174.88),
'norway': (60.47, 8.46), 'oslo': (59.91, 10.75), 'oman': (21.47, 55.97),
'pakistan': (30.37, 69.34), 'islamabad': (33.68, 73.05), 'karachi': (24.86, 67.00),
'peru': (-9.18, -75.01), 'lima': (-12.04, -77.04), 'philippines': (12.87, 121.77),
'manila': (14.59, 120.98), 'poland': (51.91, 19.13), 'warsaw': (52.22, 21.01),
'portugal': (39.39, -8.22), 'lisbon': (38.72, -9.13), 'qatar': (25.34, 51.18),
'doha': (25.28, 51.51), 'romania': (45.94, 24.96), 'bucharest': (44.43, 26.10),
'saudi arabia': (23.88, 45.07), 'riyadh': (24.71, 46.67), 'serbia': (44.01, 20.91),
'belgrade': (44.78, 20.45), 'singapore': (1.35, 103.81), 'slovakia': (48.66, 19.69),
'somalia': (5.15, 46.19), 'mogadishu': (2.04, 45.34), 'spain': (40.46, -3.74),
'madrid': (40.41, -3.70), 'barcelona': (41.38, 2.17), 'sudan': (12.86, 30.21),
'sweden': (60.12, 18.64), 'stockholm': (59.32, 18.06), 'switzerland': (46.81, 8.22),
'zurich': (47.37, 8.54), 'thailand': (15.87, 100.99), 'bangkok': (13.73, 100.49),
'tunisia': (33.88, 9.53), 'tunis': (36.80, 10.18), 'uae': (23.42, 53.84),
'dubai': (25.26, 55.29), 'abudhabi': (24.45, 54.37), 'uganda': (1.37, 32.29),
'kampu': (0.31, 32.56), 'vietnam': (14.05, 108.27), 'hanoi': (21.02, 105.84),
'yemen': (15.55, 48.51), 'sanaa': (15.35, 44.20), 'zimbabwe': (-19.01, 29.15),
}
for loc, coords in location_map.items():
if loc in text_lower: return coords
return None, None
def _categorize_news(self, article):
t = (article.get('title', '') + ' ' + article.get('context', '')).lower()
if any(w in t for w in ['war', 'conflict', 'attack', 'military', 'killed']): return 'conflict'
if any(w in t for w in ['ai', 'tech', 'startup', 'software', 'nvidia']): return 'tech'
return 'news'
def _assess_severity(self, title, desc):
t = (title + ' ' + desc).lower()
if any(w in t for w in ['war', 'attack', 'deadly', 'killing', 'disaster']): return 'critical'
if any(w in t for w in ['tension', 'threat', 'warning']): return 'high'
return 'medium'
def _haversine_km(self, lat1, lon1, lat2, lon2):
import math
R = 6371.0
dlat, dlon = math.radians(lat2-lat1), math.radians(lon2-lon1)
a = math.sin(dlat/2)**2 + math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) * math.sin(dlon/2)**2
return R * 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
def _assign_to_region(self, event):
lat, lng = event.get('lat'), event.get('lng')
if lat is None or lng is None: return
for code, reg in self.regions.items():
if self._haversine_km(lat, lng, reg['lat'], reg['lng']) < 800:
reg['events'].append(event['id'])
event['region'] = code
def _calculate_scores(self):
for reg in self.regions.values():
score = len(reg['events']) * 10
reg['score'] = min(100, score)
def get_events(self, category=None, lat=None, lng=None, radius=500, search=''):
events = self.events
if category: events = [e for e in events if e.get('category') == category]
if search:
s = search.lower()
events = [e for e in events if s in e.get('title', '').lower()]
return sorted(events, key=lambda x: x.get('time', 0), reverse=True)
def get_regions(self): return list(self.regions.values())
def get_region(self, code): return self.regions.get(code.upper())
data = GlobalWatchData()
logging.basicConfig(level=logging.INFO)
data.ensure_fresh(force=True)
def extract_article_text(url, timeout=8):
try:
resp = requests.get(url, timeout=timeout, headers=get_headers())
if resp.status_code != 200: return None
text = resp.text
# Strip script/style tags
text = re.sub(r'<script[^>]*>.*?</script>', '', text, flags=re.DOTALL)
text = re.sub(r'<style[^>]*>.*?</style>', '', text, flags=re.DOTALL)
# Strip HTML tags
text = re.sub(r'<[^>]+>', ' ', text)
# Collapse whitespace
text = re.sub(r'\s+', ' ', text).strip()
# Get first meaningful paragraph (skip nav/menu text)
paragraphs = [p.strip() for p in text.split('. ') if len(p.strip()) > 60]
if paragraphs:
return paragraphs[0][:300]
return text[:300]
except: return None
class PredictionEngine:
def analyze(self, events, hours=24):
if not events: return {'predictions': [], 'confidence': 'low'}
predictions = []
now = time.time() * 1000
cutoff = now - hours * 3600 * 1000
recent = [e for e in events if e.get('time', 0) > cutoff]
if not recent:
return {'predictions': [], 'confidence': 'low'}
# Count by category and region
cat_counts = {}
region_counts = {}
severity_counts = {}
for e in recent:
c = e.get('category', 'unknown')
cat_counts[c] = cat_counts.get(c, 0) + 1
r = e.get('region', 'unknown')
region_counts[r] = region_counts.get(r, 0) + 1
s = e.get('severity', 'low')
severity_counts[s] = severity_counts.get(s, 0) + 1
total = len(recent)
# Category trend predictions
for cat, count in sorted(cat_counts.items(), key=lambda x: -x[1])[:3]:
ratio = count / max(total, 1)
prob = min(0.9, 0.3 + ratio * 0.5)
predictions.append({
'type': f'{cat.title()} Activity',
'title': f'Increased {cat} events detected',
'description': f'{count} {cat} events in the last {hours}h period. Trend suggests continued activity.',
'probability': round(prob, 2),
'severity': 'high' if ratio > 0.4 else 'medium',
'timeframe': f'Next {hours}h'
})
# Region hotspot predictions
for reg, count in sorted(region_counts.items(), key=lambda x: -x[1])[:2]:
if reg == 'unknown': continue
prob = min(0.85, 0.2 + count * 0.1)
predictions.append({
'type': 'Regional Alert',
'title': f'H otspot: {reg}',
'description': f'{count} events recorded in {reg}. Monitoring for escalation.',
'probability': round(prob, 2),
'severity': 'high' if count > 5 else 'medium',
'timeframe': f'Next {hours}h'
})
# Severity-based escalation prediction
if severity_counts.get('critical', 0) >= 2:
predictions.append({
'type': 'Severity Escalation',
'title': 'Critical event threshold reached',
'description': f'{severity_counts["critical"]} critical events in {hours}h. Further escalation possible.',
'probability': 0.75,
'severity': 'critical',
'timeframe': f'Next 12h'
})
confidence = 'high' if total > 20 else 'medium' if total > 5 else 'low'
return {'predictions': predictions, 'confidence': confidence}
prediction_engine = PredictionEngine()
@app.route('/health')
def health():
return jsonify({'status': 'ok', 'timestamp': data.last_update})
@app.route('/')
def index():
base_dir = os.path.dirname(os.path.abspath(__file__))
with open(os.path.join(base_dir, 'index.html'), 'r') as f: return f.read()
@app.route('/favicon.ico')
def favicon():
base_dir = os.path.dirname(os.path.abspath(__file__))
return open(os.path.join(base_dir, 'favicon.ico'), 'rb').read(), 200, {'Content-Type': 'image/x-icon'}
@app.route('/assets/<path:filename>')
def serve_asset(filename):
base_dir = os.path.dirname(os.path.abspath(__file__))
return open(os.path.join(base_dir, 'assets', filename), 'rb').read(), 200
@app.route('/app.js')
def app_js():
base_dir = os.path.dirname(os.path.abspath(__file__))
with open(os.path.join(base_dir, 'app.js'), 'r') as f: return f.read(), 200, {'Content-Type': 'application/javascript'}
@app.route('/services.js')
def services_js():
base_dir = os.path.dirname(os.path.abspath(__file__))
with open(os.path.join(base_dir, 'services.js'), 'r') as f: return f.read(), 200, {'Content-Type': 'application/javascript'}
@app.route('/api/weather/<lat>/<lng>')
def get_weather(lat, lng):
try:
url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lng}¤t_weather=true"
resp = requests.get(url, timeout=5)
if resp.status_code == 200:
data = resp.json()
current = data.get('current_weather', {})
return jsonify({
'temperature': current.get('temperature'),
'wind_speed': current.get('windspeed'),
'weather_code': current.get('weathercode')
})
return jsonify({'error': 'Weather data unavailable'}), 404
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/events')
def get_events():
cat = request.args.get('category')
return jsonify(data.get_events(cat))
@app.route('/api/regions')
def get_regions(): return jsonify(data.get_regions())
@app.route('/api/stats')
def get_stats():
events = data.events
cats = {}
sevs = {}
for e in events:
c = e.get('category', 'other')
cats[c] = cats.get(c, 0) + 1
s = e.get('severity', 'low')
sevs[s] = sevs.get(s, 0) + 1
return jsonify({'categories': cats, 'severities': sevs, 'total': len(events)})
@app.route('/api/predict')
def get_predictions():
hours = request.args.get('hours', 24, type=int)
return jsonify(prediction_engine.analyze(data.events, hours))
@app.route('/api/article')
def get_article():
url = request.args.get('url')
if not url: return jsonify({'error': 'No URL provided'}), 400
text = extract_article_text(url)
if text: return jsonify({'content': text})
return jsonify({'error': 'Could not fetch article'}), 404
@app.route('/api/summarize', methods=['POST'])
def summarize_event():
event_id = request.json.get('event_id')
if not event_id: return jsonify({'summary': 'No event specified'})
event = next((e for e in data.events if e['id'] == event_id), None)
if not event: return jsonify({'summary': 'Event not found'})
title = event.get('title', '')
desc = event.get('description', '')
text = f"{title}. {desc}"
if len(text) > 300:
text = text[:300] + '...'
return jsonify({'summary': text})
@app.route('/api/sentiment', methods=['POST'])
def analyze_sentiment():
event_id = request.json.get('event_id')
if not event_id: return jsonify({'score': 0, 'sentiment': 'neutral'})
event = next((e for e in data.events if e['id'] == event_id), None)
if not event: return jsonify({'score': 0, 'sentiment': 'neutral'})
text = (event.get('title', '') + ' ' + event.get('description', '')).lower()
pos = ['safe', 'recover', 'stable', 'improve', 'success', 'peace', 'aid', 'rescue']
neg = ['attack', 'deadly', 'kill', 'war', 'destruct', 'crisis', 'disaster', 'fatal']
score = (sum(w in text for w in pos) - sum(w in text for w in neg)) / max(len(text.split()), 1) * 10
score = max(-1, min(1, score))
sentiment = 'positive' if score > 0.1 else 'negative' if score < -0.1 else 'neutral'
return jsonify({'score': round(score, 2), 'sentiment': sentiment})
@app.route('/api/export/csv')
def export_csv():
events = data.events
import csv, io
out = io.StringIO()
w = csv.writer(out)
w.writerow(['id','title','description','category','severity','source','url','lat','lng','time','region','magnitude','depth'])
for e in events:
w.writerow([e.get('id',''), e.get('title',''), e.get('description',''), e.get('category',''),
e.get('severity',''), e.get('source',''), e.get('url',''), e.get('lat',0), e.get('lng',0),
e.get('time',0), e.get('region',''), e.get('magnitude',''), e.get('depth','')])
return Response(out.getvalue(), mimetype='text/csv', headers={'Content-Disposition': 'attachment; filename=events.csv'})
@app.route('/api/export/geojson')
def export_geojson():
features = []
for e in data.events:
lat, lng = e.get('lat'), e.get('lng')
if not lat or not lng: continue
features.append({
'type': 'Feature',
'geometry': {'type': 'Point', 'coordinates': [lng, lat]},
'properties': {
'id': e.get('id'), 'title': e.get('title'), 'category': e.get('category'),
'severity': e.get('severity'), 'source': e.get('source'), 'url': e.get('url'),
'time': e.get('time'), 'region': e.get('region'), 'description': e.get('description','')[:100]
}
})
return jsonify({'type': 'FeatureCollection', 'features': features})
@app.route('/api/feed.rss')
def rss_feed():
events = sorted(data.events, key=lambda x: x.get('time', 0), reverse=True)[:50]
items = ''
for e in events:
title = e.get('title', 'Untitled')
desc = e.get('description', '')
url = e.get('url', 'https://gracemanager.onrender.com')
ts = datetime.fromtimestamp(e.get('time', 0) / 1000).strftime('%a, %d %b %Y %H:%M:%S +0000')
items += f'''<item>
<title>{title}</title>
<description>{desc[:200]}</description>
<link>{url}</link>
<guid>{e.get('id', '')}</guid>
<pubDate>{ts}</pubDate>
<category>{e.get('category', '')}</category>
</item>\n'''
rss = f'''<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Global Watch - Real-time Events</title>
<link>https://gracemanager.onrender.com</link>
<description>Real-time worldwide events feed</description>
<lastBuildDate>{datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S +0000')}</lastBuildDate>
{items}</channel></rss>'''
return Response(rss, mimetype='application/rss+xml')
@app.route('/api/sources')
def get_sources():
return jsonify({
'sources': [
{'name': 'USGS Earthquakes', 'url': USGS_API, 'status': 'active'},
{'name': 'Reddit r/worldnews', 'url': 'https://www.reddit.com/r/worldnews/hot.json', 'status': 'active'},
{'name': 'Hacker News', 'url': 'https://hacker-news.firebaseio.com/v0/', 'status': 'active'},
],
'last_update': data.last_update
})
@socketio.on('connect')
def handle_connect():
emit('update', {'events': data.events, 'regions': data.regions, 'timestamp': data.last_update})
if __name__ == '__main__':
socketio.run(app, host='0.0.0.0', port=5000, allow_unsafe_werkzeug=True)