-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
364 lines (318 loc) · 13.2 KB
/
server.py
File metadata and controls
364 lines (318 loc) · 13.2 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
"""
CognivexAI · Deep Intelligence — Backend Server
Media vs Ground Reality · Any Country, Any State
"""
import os, json, asyncio, re, time, urllib.parse
from datetime import datetime, timezone
from flask import Flask, jsonify, request, send_from_directory
from flask_cors import CORS
from dotenv import load_dotenv
load_dotenv()
API_ID = int(os.getenv('TELEGRAM_API_ID', 0))
API_HASH = os.getenv('TELEGRAM_API_HASH', '')
SESSION = os.getenv('TELEGRAM_SESSION', '')
ENV_PATH = os.path.join(os.path.dirname(__file__), '.env')
app = Flask(__name__, static_folder=os.path.dirname(__file__))
CORS(app)
# ── Telegram channel registry by region ───────────────────────────────────────
CHANNEL_REGISTRY = {
# ── Tamil Nadu, India ──
'IN-TN': {
'media': [
'SunNewsLive', # Sun News Tamil
'vijaytelevision', # Vijay TV
'PolimerNews', # Polimer News
'ThanthiTV', # Thanthi TV
'News18TamilNadu', # News18 Tamil Nadu
'PuthiyaThalaimurai', # Puthiya Thalaimurai TV
'CaptainNewsTamil', # Captain TV
'KalaignarTV', # Kalaignar TV
'RedPix24x7', # Red Pix TV
'MakkalTV', # Makkal TV
'Vendhar_TV', # Vendhar TV
'JayaPlus', # Jaya Plus
'NambikaiTV', # Nambikai TV
],
'party': [
'DMKofficial', # DMK
'AIADMKofficial', # AIADMK
'TVKofficial', # TVK - Vijay
'BJPTamilNadu', # BJP Tamil Nadu
'PMKparty', # PMK
'VCKparty', # VCK
],
},
# ── India National ──
'IN': {
'media': [
'ndtvindia', # NDTV India
'indiatodaychannel', # India Today
'RepublicWorldTv', # Republic TV
'ZeeNews', # Zee News
'AajTakChannel', # Aaj Tak
'TimesNowTv', # Times Now
'TheHinduOfficial', # The Hindu
'ANI_news', # ANI
],
'party': [
'BJP4India', # BJP
'INCIndia', # Congress
'AamAadmiParty', # AAP
],
},
# ── United States ──
'US': {
'media': [
'CNNnewsroom',
'FoxNews',
'nytimes',
'washingtonpost',
'politico',
],
'party': [],
},
# ── United Kingdom ──
'UK': {
'media': [
'bbcnews',
'skynewsbreak',
'guardian',
'telegraph',
],
'party': [],
},
# ── France ──
'FR': {
'media': [
'lemondefr',
'bfmtvnews',
'lefigaro',
'liberation',
],
'party': [],
},
# ── Global / Fallback ──
'GLOBAL': {
'media': [
'aljazeeraenglish',
'reuters',
'bbcnews',
'AP_Top_Stories',
],
'party': [],
},
}
# Default: Tamil Nadu
MEDIA_CHANNELS = CHANNEL_REGISTRY['IN-TN']['media']
PARTY_CHANNELS = CHANNEL_REGISTRY['IN-TN']['party']
ALL_CHANNELS = MEDIA_CHANNELS + PARTY_CHANNELS
def get_channels(region='IN-TN'):
reg = CHANNEL_REGISTRY.get(region, CHANNEL_REGISTRY['GLOBAL'])
media = reg.get('media', [])
party = reg.get('party', [])
return media, party, media + party
# ── Telegram client (singleton) ────────────────────────────────────────────────
_client = None
_loop = None
def get_loop():
global _loop
if _loop is None or _loop.is_closed():
_loop = asyncio.new_event_loop()
return _loop
async def get_client():
global _client
from telethon import TelegramClient
from telethon.sessions import StringSession
if _client is None:
_client = TelegramClient(StringSession(SESSION), API_ID, API_HASH)
if not _client.is_connected():
await _client.connect()
return _client
def run_async(coro):
return get_loop().run_until_complete(coro)
async def fetch_channel(client, channel_username, topic_keywords, limit=30):
from telethon.errors import ChannelPrivateError, UsernameNotOccupiedError, FloodWaitError
results = []
try:
entity = await client.get_entity(channel_username)
async for msg in client.iter_messages(entity, limit=limit):
if not msg.text:
continue
text = msg.text.strip()
lower = text.lower()
if any(kw.lower() in lower for kw in topic_keywords) or not topic_keywords:
results.append({
'channel': channel_username,
'channel_title': entity.title if hasattr(entity, 'title') else channel_username,
'text': text[:300],
'date': msg.date.isoformat() if msg.date else '',
'views': getattr(msg, 'views', 0) or 0,
'forwards': getattr(msg, 'forwards', 0) or 0,
'reactions': sum(r.count for r in (msg.reactions.results if msg.reactions else [])),
})
except (ChannelPrivateError, UsernameNotOccupiedError):
pass
except FloodWaitError as e:
await asyncio.sleep(min(e.seconds, 5))
except Exception as e:
print(f'Error {channel_username}: {e}')
return results
# ── /api/telegram ──────────────────────────────────────────────────────────────
@app.route('/api/telegram')
def telegram_endpoint():
topic = request.args.get('topic', '').strip()
region = request.args.get('region', 'IN-TN')
if not topic:
return jsonify({'error': 'topic param required'}), 400
if not SESSION:
return jsonify({'error': 'Telegram not set up'}), 503
keywords = [w for w in re.split(r'\s+', topic) if len(w) > 2]
media_ch, party_ch, all_ch = get_channels(region)
async def fetch_all():
client = await get_client()
all_messages = []
for ch in all_ch:
msgs = await fetch_channel(client, ch, keywords, limit=30)
all_messages.extend(msgs)
return all_messages
try:
messages = run_async(fetch_all())
except Exception as e:
return jsonify({'error': str(e)}), 500
messages.sort(key=lambda m: m['views'] + m['reactions'] * 3, reverse=True)
media_msgs = [m for m in messages if m['channel'] in media_ch]
party_msgs = [m for m in messages if m['channel'] in party_ch]
return jsonify({
'topic': topic, 'region': region,
'total': len(messages),
'media': media_msgs[:20],
'party': party_msgs[:15],
'channels_hit': list({m['channel_title'] for m in messages}),
'fetched_at': datetime.now(timezone.utc).isoformat(),
})
# ── /api/trends (Google Trends via pytrends) ──────────────────────────────────
@app.route('/api/trends')
def trends_endpoint():
topic = request.args.get('topic', '').strip()
geo = request.args.get('geo', '') # e.g. 'IN-TN', 'US', 'FR'
compare = request.args.get('compare', '') # comma-separated extra keywords
if not topic:
return jsonify({'error': 'topic required'}), 400
try:
from pytrends.request import TrendReq
except ImportError:
return jsonify({'error': 'pytrends not installed — run: pip install pytrends'}), 503
try:
kw_list = [topic]
if compare:
kw_list += [k.strip() for k in compare.split(',') if k.strip()]
kw_list = kw_list[:5]
pt = TrendReq(hl='en-US', tz=330, timeout=(10, 25))
pt.build_payload(kw_list, geo=geo, timeframe='today 3-m')
df = pt.interest_over_time()
if df.empty:
return jsonify({'error': 'No trend data found', 'keywords': kw_list})
labels = [str(d)[:10] for d in df.index.tolist()]
series = {}
for kw in kw_list:
if kw in df.columns:
series[kw] = [int(v) for v in df[kw].tolist()]
# Also get related queries
related = {}
try:
rq = pt.related_queries()
for kw in kw_list:
if kw in rq and rq[kw]['top'] is not None:
related[kw] = rq[kw]['top']['query'].tolist()[:5]
except:
pass
return jsonify({
'keywords': kw_list,
'geo': geo,
'labels': labels,
'series': series,
'related': related,
'fetched_at': datetime.now(timezone.utc).isoformat(),
})
except Exception as e:
return jsonify({'error': str(e)}), 500
# ── /api/twitter (free scraper via nitter) ────────────────────────────────────
NITTER_INSTANCES = [
'nitter.privacydev.net',
'nitter.poast.org',
'nitter.1d4.us',
'nitter.catsarch.com',
]
@app.route('/api/twitter')
def twitter_endpoint():
topic = request.args.get('topic', '').strip()
lang = request.args.get('lang', '') # e.g. 'ta', 'fr', 'en'
limit = int(request.args.get('limit', 40))
if not topic:
return jsonify({'error': 'topic required'}), 400
try:
import requests as req
from bs4 import BeautifulSoup
query = topic
if lang:
query += f' lang:{lang}'
tweets = []
for instance in NITTER_INSTANCES:
try:
url = f'https://{instance}/search?q={urllib.parse.quote(query)}&f=tweets'
r = req.get(url, headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36'
}, timeout=8)
if r.status_code != 200:
continue
soup = BeautifulSoup(r.text, 'html.parser')
for item in soup.select('.tweet-content'):
text = item.get_text(strip=True)
if text and len(text) > 10:
tweets.append({'text': text[:280], 'source': instance})
if len(tweets) >= limit:
break
if tweets:
break
except Exception as e:
print(f'Nitter {instance} failed: {e}')
continue
return jsonify({
'topic': topic,
'lang': lang,
'count': len(tweets),
'tweets': tweets,
'fetched_at': datetime.now(timezone.utc).isoformat(),
})
except ImportError:
return jsonify({'error': 'beautifulsoup4 not installed — run: pip install beautifulsoup4'}), 503
except Exception as e:
return jsonify({'error': str(e)}), 500
# ── /api/status ────────────────────────────────────────────────────────────────
@app.route('/api/status')
def status():
return jsonify({
'telegram_configured': bool(API_ID and API_HASH),
'telegram_session': bool(SESSION),
'regions': list(CHANNEL_REGISTRY.keys()),
'channels_by_region': {k: len(v['media']) + len(v['party']) for k, v in CHANNEL_REGISTRY.items()},
})
# ── Static serving ─────────────────────────────────────────────────────────────
@app.route('/')
def index():
return send_from_directory(os.path.dirname(__file__), 'deep-intelligence.html')
@app.route('/<path:path>')
def static_files(path):
return send_from_directory(os.path.dirname(__file__), path)
# ── Main ───────────────────────────────────────────────────────────────────────
if __name__ == '__main__':
print('\n╔══════════════════════════════════════════════════════╗')
print('║ CognivexAI · Deep Intelligence ║')
print('║ Media vs Ground Reality · Any Country, Any State ║')
print('╠══════════════════════════════════════════════════════╣')
print(f'║ Telegram session : {"✓ YES" if SESSION else "✗ NO"} ║')
print(f'║ Regions loaded : {len(CHANNEL_REGISTRY)} regions, {sum(len(v["media"])+len(v["party"]) for v in CHANNEL_REGISTRY.values())} channels total ║')
print('╠══════════════════════════════════════════════════════╣')
print('║ App → http://localhost:5001 ║')
print('╚══════════════════════════════════════════════════════╝\n')
app.run(port=5001, debug=False)