File size: 38,153 Bytes
eb34004
 
 
8a0f05d
eb34004
 
dd99def
 
 
 
 
 
eb34004
 
dd99def
eb34004
dd99def
eb34004
 
dd99def
 
 
 
 
 
eb34004
 
8a0f05d
 
eb34004
dd99def
eb34004
dd99def
 
 
 
 
eb34004
dd99def
 
 
 
eb34004
dd99def
 
eb34004
 
 
 
dd99def
 
eb34004
dd99def
 
eb34004
dd99def
eb34004
 
 
 
 
 
 
 
 
 
 
 
 
 
dd99def
 
eb34004
dd99def
 
eb34004
 
 
 
 
dd99def
eb34004
 
 
 
 
dd99def
eb34004
 
 
 
 
dd99def
 
 
 
eb34004
 
 
 
 
 
8a0f05d
 
eb34004
 
 
 
 
 
 
8a0f05d
 
 
 
 
 
 
 
 
dd99def
eb34004
dd99def
 
 
 
eb34004
dd99def
 
eb34004
 
dd99def
eb34004
dd99def
 
 
 
 
eb34004
8a0f05d
dd99def
 
eb34004
dd99def
 
eb34004
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd99def
 
eb34004
dd99def
 
eb34004
 
 
 
 
 
 
 
 
 
dd99def
eb34004
 
 
dd99def
eb34004
 
dd99def
eb34004
 
 
 
 
 
dd99def
eb34004
 
 
dd99def
eb34004
 
 
 
 
 
 
dd99def
eb34004
dd99def
eb34004
dd99def
 
 
 
 
eb34004
 
 
 
dd99def
 
 
eb34004
 
 
 
 
dd99def
 
 
 
 
 
 
 
 
 
 
 
 
 
eb34004
dd99def
 
 
 
 
 
 
 
 
eb34004
dd99def
eb34004
dd99def
 
 
 
eb34004
 
dd99def
 
 
eb34004
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd99def
 
eb34004
dd99def
 
eb34004
 
 
8a0f05d
eb34004
8a0f05d
 
 
 
 
 
 
 
eb34004
8a0f05d
 
 
 
 
 
 
 
eb34004
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd99def
eb34004
 
 
 
 
 
 
 
dd99def
eb34004
 
 
 
dd99def
 
 
 
eb34004
 
 
 
 
dd99def
 
 
eb34004
 
 
dd99def
eb34004
 
 
dd99def
eb34004
dd99def
 
 
 
 
 
 
 
eb34004
dd99def
eb34004
dd99def
 
eb34004
 
 
 
dd99def
 
 
eb34004
 
 
dd99def
eb34004
dd99def
eb34004
 
 
 
dd99def
eb34004
 
 
dd99def
 
eb34004
dd99def
 
35f6fa6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb34004
dd99def
eb34004
 
 
dd99def
eb34004
 
 
 
35f6fa6
eb34004
 
dd99def
8a0f05d
 
35f6fa6
eb34004
 
35f6fa6
 
 
 
 
 
eb34004
8a0f05d
 
eb34004
 
35f6fa6
eb34004
 
 
 
 
35f6fa6
eb34004
 
 
 
 
 
 
 
 
dd99def
eb34004
 
 
 
 
 
 
 
 
 
35f6fa6
8a0f05d
 
 
 
 
35f6fa6
8a0f05d
eb34004
35f6fa6
eb34004
35f6fa6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a0f05d
35f6fa6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a0f05d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35f6fa6
8a0f05d
 
 
 
 
 
 
 
 
 
 
 
 
dd99def
eb34004
 
 
dd99def
eb34004
dd99def
eb34004
35f6fa6
 
eb34004
dd99def
eb34004
dd99def
eb34004
 
dd99def
eb34004
 
dd99def
eb34004
 
dd99def
eb34004
 
 
 
dd99def
eb34004
 
 
 
 
 
 
 
 
 
 
dd99def
eb34004
dd99def
eb34004
 
 
 
dd99def
eb34004
 
 
 
 
 
 
 
dd99def
eb34004
 
 
 
 
 
 
 
 
dd99def
eb34004
 
 
 
 
 
 
 
 
 
 
 
 
dd99def
eb34004
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd99def
eb34004
 
 
 
 
 
 
 
 
 
 
 
dd99def
eb34004
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a0f05d
35f6fa6
 
eb34004
 
 
35f6fa6
8a0f05d
eb34004
 
8a0f05d
eb34004
 
 
 
 
 
 
8a0f05d
35f6fa6
8a0f05d
eb34004
 
 
35f6fa6
 
 
8a0f05d
 
 
 
 
35f6fa6
 
 
 
 
 
 
8a0f05d
 
 
 
 
 
 
 
 
 
 
 
35f6fa6
8a0f05d
35f6fa6
 
 
 
 
 
 
 
eb34004
 
 
 
 
 
 
35f6fa6
8a0f05d
 
eb34004
dd99def
eb34004
dd99def
8a0f05d
 
 
eb34004
8a0f05d
 
eb34004
8a0f05d
 
 
 
 
 
35f6fa6
8a0f05d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb34004
8a0f05d
 
35f6fa6
8a0f05d
 
 
 
 
eb34004
8a0f05d
 
 
 
 
 
 
dd99def
 
eb34004
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
"""
Unified News Scraper & Sentiment Analysis Application
Combines scraping, processing, and visualization in one interface
Modified for Hugging Face Spaces - uses /tmp directory
"""

import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as px
import plotly.graph_objects as go
from matplotlib.ticker import MaxNLocator
import subprocess
import sys
import os
from pathlib import Path
import time
from datetime import datetime
import warnings
import json
import requests
import spacy

warnings.filterwarnings('ignore')

# Constants
INDIA_GEOJSON_URL = 'https://raw.githubusercontent.com/geohacker/india/master/state/india_state.geojson'
BASE_DIR = Path('/tmp/news_scraper')

# Page config
st.set_page_config(
    page_title="News Scraper & Analysis Platform",
    page_icon="πŸ“°",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Custom CSS
def load_css():
    st.markdown("""
    <style>
        .main-header {
            font-size: 2.8rem !important;
            font-weight: 700 !important;
            text-align: center !important;
            padding: 1.5rem 0 !important;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        .sub-header {
            font-size: 1.8rem !important;
            font-weight: 600 !important;
            padding: 1rem 0 !important;
            color: #2c3e50;
        }
        .feature-card {
            background: white;
            border-radius: 15px;
            padding: 25px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            margin: 10px 0;
            transition: transform 0.3s;
        }
        .feature-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 12px rgba(0, 0, 0, 0.15);
        }
        .metric-card {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 10px;
            padding: 20px;
            color: white;
            text-align: center;
        }
        .status-running {
            background-color: #fff3cd;
            border-left: 4px solid #ffc107;
            padding: 15px;
            border-radius: 5px;
        }
        .status-success {
            background-color: #d4edda;
            border-left: 4px solid #28a745;
            padding: 15px;
            border-radius: 5px;
        }
        .status-error {
            background-color: #f8d7da;
            border-left: 4px solid #dc3545;
            padding: 15px;
            border-radius: 5px;
        }
    </style>
    """, unsafe_allow_html=True)

# Initialize session state
def init_session_state():
    defaults = {
        'scraped_data': {},
        'scraping_active': False,
        'processing_status': {},
        'selected_dataset': None,
        'base_dir': BASE_DIR
    }
    for key, value in defaults.items():
        if key not in st.session_state:
            st.session_state[key] = value

# Setup directories
def setup_directories():
    """Create necessary directories in /tmp"""
    try:
        for dir_name in ['output', 'data', 'temp']:
            dir_path = BASE_DIR / dir_name
            dir_path.mkdir(parents=True, exist_ok=True)
        return True
    except Exception as e:
        st.error(f"❌ Directory setup error: {e}")
        return False

# Load India GeoJSON
@st.cache_data
def load_india_geojson():
    """Load India GeoJSON data for mapping"""
    try:
        response = requests.get(INDIA_GEOJSON_URL, timeout=10)
        return json.loads(response.text)
    except Exception as e:
        st.warning(f"Could not load India map: {e}")
        return None

# Load spaCy model
@st.cache_resource
def load_spacy_model():
    try:
        return spacy.load("en_core_web_sm")
    except OSError:
        st.info("Downloading spaCy model...")
        subprocess.call([sys.executable, "-m", "spacy", "download", "en_core_web_sm"])
        return spacy.load("en_core_web_sm")

# State mapping
def get_state_mapping():
    return {
        'andhra pradesh': 'Andhra Pradesh', 'arunachal pradesh': 'Arunachal Pradesh',
        'assam': 'Assam', 'bihar': 'Bihar', 'chhattisgarh': 'Chhattisgarh',
        'goa': 'Goa', 'gujarat': 'Gujarat', 'haryana': 'Haryana',
        'himachal pradesh': 'Himachal Pradesh', 'jharkhand': 'Jharkhand',
        'karnataka': 'Karnataka', 'kerala': 'Kerala', 'madhya pradesh': 'Madhya Pradesh',
        'maharashtra': 'Maharashtra', 'manipur': 'Manipur', 'meghalaya': 'Meghalaya',
        'mizoram': 'Mizoram', 'nagaland': 'Nagaland', 'odisha': 'Odisha',
        'punjab': 'Punjab', 'rajasthan': 'Rajasthan', 'sikkim': 'Sikkim',
        'tamil nadu': 'Tamil Nadu', 'telangana': 'Telangana', 'tripura': 'Tripura',
        'uttar pradesh': 'Uttar Pradesh', 'uttarakhand': 'Uttarakhand',
        'west bengal': 'West Bengal', 'delhi': 'Delhi', 'new delhi': 'Delhi',
        'jammu and kashmir': 'Jammu and Kashmir', 'j&k': 'Jammu and Kashmir',
        'ladakh': 'Ladakh', 'chandigarh': 'Chandigarh', 'puducherry': 'Puducherry',
        'mumbai': 'Maharashtra', 'kolkata': 'West Bengal', 'chennai': 'Tamil Nadu',
        'bangalore': 'Karnataka', 'bengaluru': 'Karnataka', 'hyderabad': 'Telangana',
        'ahmedabad': 'Gujarat', 'pune': 'Maharashtra', 'jaipur': 'Rajasthan',
    }

# Extract locations from text
@st.cache_data
def extract_locations_from_descriptions(df, description_column='desc'):
    """Extract state names from description using spaCy"""
    nlp = load_spacy_model()
    state_mapping = get_state_mapping()
    
    locations = []
    progress_bar = st.progress(0)
    
    for idx, (_, row) in enumerate(df.iterrows()):
        if idx % 100 == 0:
            progress_bar.progress(min(idx / len(df), 1.0))
        
        if pd.isna(row.get(description_column, None)):
            locations.append(None)
            continue
        
        description = str(row[description_column]).lower()
        doc = nlp(description)
        
        found_locations = []
        for ent in doc.ents:
            if ent.label_ in ["GPE", "LOC"]:
                loc_name = ent.text.lower()
                if loc_name in state_mapping:
                    found_locations.append(state_mapping[loc_name])
        
        for state_var, standard_name in state_mapping.items():
            if state_var in description and standard_name not in found_locations:
                found_locations.append(standard_name)
        
        locations.append(found_locations[0] if found_locations else None)
    
    progress_bar.progress(1.0)
    df = df.copy()
    df['extracted_location'] = locations
    
    return df

# Analyze sentiment by state
def analyze_sentiment_by_state(df, sentiment_column='sentiment_score'):
    """Analyze sentiment by state"""
    df_with_locations = df.dropna(subset=['extracted_location', sentiment_column])
    
    if len(df_with_locations) == 0:
        return None
    
    sentiment_by_state = df_with_locations.groupby('extracted_location')[sentiment_column].agg([
        ('avg_sentiment', 'mean'),
        ('count', 'count')
    ]).reset_index()
    
    return sentiment_by_state

# Create India sentiment map
def create_india_sentiment_map(sentiment_data, geojson_data, title):
    """Create choropleth map of India showing sentiment by state"""
    if sentiment_data is None or geojson_data is None:
        return None
    
    min_sentiment = sentiment_data['avg_sentiment'].min()
    max_sentiment = sentiment_data['avg_sentiment'].max()
    
    if min_sentiment < 0 and max_sentiment > 0:
        abs_max = max(abs(min_sentiment), abs(max_sentiment))
        color_range = [-abs_max, abs_max]
    else:
        color_range = [min_sentiment - 0.1, max_sentiment + 0.1]
    
    fig = px.choropleth_mapbox(
        sentiment_data,
        geojson=geojson_data,
        locations='extracted_location',
        featureidkey="properties.NAME_1",
        color='avg_sentiment',
        color_continuous_scale="RdBu",
        range_color=color_range,
        mapbox_style="carto-positron",
        zoom=3.5,
        center={"lat": 20.5937, "lon": 78.9629},
        opacity=0.7,
        hover_data=['count'],
        labels={
            'avg_sentiment': 'Avg Sentiment',
            'extracted_location': 'State',
            'count': 'Articles'
        }
    )
    
    fig.update_layout(
        title=dict(text=title, font=dict(size=20), x=0.5),
        height=600,
        margin={"r":0,"t":50,"l":0,"b":0}
    )
    
    return fig

# Top locations chart
def create_top_locations_chart(df, title):
    """Create bar chart of top mentioned locations"""
    if 'extracted_location' not in df.columns or df['extracted_location'].isna().all():
        return None
    
    location_counts = df['extracted_location'].value_counts().head(15).reset_index()
    location_counts.columns = ['Location', 'Count']
    
    fig = px.bar(
        location_counts,
        y='Location',
        x='Count',
        title=title,
        orientation='h',
        color='Count',
        color_continuous_scale='Viridis'
    )
    
    fig.update_layout(height=500, yaxis={'categoryorder':'total ascending'})
    return fig

# Discover datasets
@st.cache_data
def discover_datasets():
    """Discover datasets in /tmp directory"""
    datasets = {}
    
    search_paths = [
        BASE_DIR / 'data',
        BASE_DIR / 'output',
        BASE_DIR,
    ]
    
    for directory in search_paths:
        if directory.exists():
            try:
                for csv_file in directory.glob('*.csv'):
                    name = csv_file.stem.replace('_articles', '').replace('_', ' ').title()
                    if name not in datasets:
                        datasets[name] = str(csv_file)
            except Exception:
                continue
    
    return datasets

# Load data
@st.cache_data
def load_data(file_path):
    try:
        df = pd.read_csv(file_path)
        
        date_cols = [col for col in df.columns if 'date' in col.lower()]
        if date_cols:
            df['date'] = pd.to_datetime(df[date_cols[0]], errors='coerce')
        
        sentiment_cols = [col for col in df.columns if 'sentiment' in col.lower()]
        if sentiment_cols and 'sentiment_value' not in df.columns:
            df['sentiment_value'] = df[sentiment_cols[0]]
        
        if 'sentiment_score' not in df.columns and 'sentiment_value' in df.columns:
            sentiment_map = {'positive': 1.0, 'negative': -1.0, 'neutral': 0.0}
            df['sentiment_score'] = df['sentiment_value'].str.lower().map(sentiment_map).fillna(0)
        
        return df
    except Exception as e:
        st.error(f"Error loading data: {str(e)}")
        return None

# Plotting functions
def plot_sentiment_trends(df, title):
    if 'date' not in df.columns or 'sentiment_value' not in df.columns:
        return None
    
    plt.style.use('seaborn-v0_8-whitegrid')
    df['year'] = df['date'].dt.year
    
    valid_sentiments = {"positive", "negative", "neutral"}
    df['sentiment'] = df['sentiment_value'].apply(
        lambda x: x.lower() if isinstance(x, str) and x.lower() in valid_sentiments else "neutral"
    )
    
    sentiment_counts = df.groupby(['year', 'sentiment']).size().reset_index(name='count')
    year_totals = sentiment_counts.groupby('year')['count'].sum().reset_index(name='total')
    sentiment_counts = sentiment_counts.merge(year_totals, on='year')
    sentiment_counts['percentage'] = sentiment_counts['count'] / sentiment_counts['total'] * 100
    
    sentiment_pivot = sentiment_counts.pivot(
        index='year', columns='sentiment', values='percentage'
    ).fillna(0)
    
    for sentiment in ['negative', 'neutral', 'positive']:
        if sentiment not in sentiment_pivot.columns:
            sentiment_pivot[sentiment] = 0
    
    fig, ax = plt.subplots(figsize=(12, 6))
    
    colors = {
        'negative': '#e74c3c',
        'neutral': '#95a5a6',
        'positive': '#2ecc71'
    }
    
    for sentiment in ['negative', 'neutral', 'positive']:
        ax.plot(
            sentiment_pivot.index,
            sentiment_pivot[sentiment],
            marker='o',
            linewidth=2.5,
            label=sentiment.capitalize(),
            color=colors[sentiment],
            markersize=7
        )
    
    ax.set_ylabel('Percentage (%)', fontweight='bold')
    ax.set_xlabel('Year', fontweight='bold')
    ax.set_title(title, fontweight='bold', pad=15)
    ax.legend(loc='best', frameon=True)
    ax.grid(axis='y', linestyle='--', alpha=0.3)
    
    plt.tight_layout()
    return fig

def create_sentiment_pie(df, title):
    if 'sentiment_value' not in df.columns:
        return None
    
    sentiment_counts = df['sentiment_value'].str.lower().value_counts()
    
    fig = px.pie(
        values=sentiment_counts.values,
        names=[s.title() for s in sentiment_counts.index],
        title=title,
        color_discrete_map={
            'Positive': '#2ecc71',
            'Neutral': '#95a5a6',
            'Negative': '#e74c3c'
        }
    )
    fig.update_traces(textposition='inside', textinfo='percent+label')
    return fig

# Run scraper function
def run_scraper(source, topic, max_workers=4, max_articles=100):
    """
    Run the appropriate scraper based on source
    Returns dict with success status, file path, and article count
    """
    try:
        source_map = {
            'toi': ('scrapers.toi_scraper', 'TOIArticleScraper'),
            'ndtv': ('scrapers.ndtv_scraper', 'NDTVArticleScraper'),
            'wion': ('scrapers.wion_scraper', 'WIONArticleScraper'),
            'scroll': ('scrapers.scroll_scraper', 'ScrollArticleScraper')
        }
        
        if source not in source_map:
            return {'success': False, 'error': f'Unknown source: {source}'}
        
        module_name, class_name = source_map[source]
        
        try:
            module = __import__(module_name, fromlist=[class_name])
            ScrapeClass = getattr(module, class_name)
        except (ImportError, AttributeError) as e:
            return {
                'success': False, 
                'error': f'Scraper module not found. Please ensure scraper files are available.'
            }
        
        scraper = ScrapeClass(max_workers=max_workers)
        
        output_dir = BASE_DIR / 'output'
        output_dir.mkdir(parents=True, exist_ok=True)
        
        original_dir = os.getcwd()
        os.chdir(output_dir)
        
        try:
            if source == 'toi':
                topic_url = f"{scraper.base_url}/topic/{topic}/news"
                final_csv = scraper.scrape_topic(topic_url, topic)
            elif source == 'ndtv':
                final_csv = scraper.scrape_topic(topic)
            elif source in ['wion', 'scroll']:
                final_csv = scraper.scrape_topic(topic.lower(), topic)
            
            articles = getattr(scraper, 'articles', None) or getattr(scraper, 'scraped_articles', [])
            article_count = len(articles)
            
            if article_count > max_articles:
                df = pd.read_csv(final_csv)
                df = df.head(max_articles)
                df.to_csv(final_csv, index=False)
                article_count = max_articles
            
            return {
                'success': True,
                'file': str(output_dir / final_csv),
                'count': article_count
            }
            
        finally:
            os.chdir(original_dir)
            
    except Exception as e:
        return {
            'success': False,
            'error': str(e)
        }

# MAIN APP PAGES

def show_home_page():
    st.markdown('<h1 class="main-header">πŸ“° News Scraper & Analysis Platform</h1>', 
                unsafe_allow_html=True)
    
    st.markdown("""
    <div style="text-align: center; padding: 20px; background-color: #f8f9fa; 
                border-radius: 10px; margin: 20px 0;">
        <h3>Complete Pipeline for News Collection and Sentiment Analysis</h3>
        <p>Scrape or upload articles from major Indian news sources and analyze sentiment trends</p>
    </div>
    """, unsafe_allow_html=True)
    
    st.info(f"πŸ’Ύ **Storage Location:** `{BASE_DIR}` (temporary - cleared on restart)")
    
    col1, col2, col3, col4 = st.columns(4)
    
    with col1:
        st.markdown('<div class="feature-card">', unsafe_allow_html=True)
        st.markdown("### πŸ” Scrape")
        st.write("Collect articles from major Indian news sources")
        st.markdown('</div>', unsafe_allow_html=True)
    
    with col2:
        st.markdown('<div class="feature-card">', unsafe_allow_html=True)
        st.markdown("### πŸ“€ Upload")
        st.write("Upload pre-scraped CSV files for analysis")
        st.markdown('</div>', unsafe_allow_html=True)
    
    with col3:
        st.markdown('<div class="feature-card">', unsafe_allow_html=True)
        st.markdown("### πŸ“Š Analyze")
        st.write("Automatic sentiment classification and trend analysis")
        st.markdown('</div>', unsafe_allow_html=True)
    
    with col4:
        st.markdown('<div class="feature-card">', unsafe_allow_html=True)
        st.markdown("### πŸ“ˆ Visualize")
        st.write("Interactive charts and geographic sentiment mapping")
        st.markdown('</div>', unsafe_allow_html=True)
    
    datasets = discover_datasets()
    if datasets:
        st.markdown("---")
        st.markdown("### πŸ“Š Available Datasets")
        
        cols = st.columns(min(len(datasets), 4))
        for idx, (name, path) in enumerate(list(datasets.items())[:4]):
            with cols[idx]:
                df = load_data(path)
                if df is not None:
                    st.markdown('<div class="metric-card">', unsafe_allow_html=True)
                    st.metric(name, f"{len(df):,} articles")
                    st.markdown('</div>', unsafe_allow_html=True)

def show_scraper_page():
    st.markdown('<h2 class="sub-header">πŸ“€ Data Upload & Scraper</h2>', unsafe_allow_html=True)
    
    st.warning("""
    ⚠️ **Hugging Face Spaces Notice:** 
    - Data is stored in `/tmp` and will be cleared on app restart
    - Download your processed data regularly
    - Scraping may timeout on HF Spaces - consider running locally for large scrapes
    """)
    
    tab1, tab2 = st.tabs(["πŸ“€ Upload Data", "πŸ” Scrape Articles"])
    
    with tab1:
        st.markdown("### πŸ“€ Upload Your Data")
        uploaded_file = st.file_uploader(
            "Upload CSV file with scraped articles",
            type=['csv'],
            help="Upload a CSV file with columns: title, date, desc, sentiment_value, etc."
        )
        
        if uploaded_file:
            try:
                data_dir = BASE_DIR / 'data'
                data_dir.mkdir(parents=True, exist_ok=True)
                
                save_path = data_dir / uploaded_file.name
                with open(save_path, 'wb') as f:
                    f.write(uploaded_file.getbuffer())
                
                st.success(f"βœ… File uploaded successfully! Saved to `{save_path}`")
                st.info("Go to the Analysis page to view your data.")
                
                discover_datasets.clear()
                
                with st.expander("πŸ“‹ Preview Data"):
                    df = pd.read_csv(save_path)
                    st.dataframe(df.head(10), use_container_width=True)
                    st.caption(f"Total rows: {len(df):,}")
                
            except Exception as e:
                st.error(f"❌ Error saving file: {e}")
    
    with tab2:
        st.markdown("### πŸ” Scrape News Articles")
        
        st.info("""
        **Note:** This feature requires the scraper modules to be available. 
        On HF Spaces, scraping may timeout for large datasets. For production use, 
        consider running scrapers locally and uploading the results.
        """)
        
        col1, col2 = st.columns(2)
        
        with col1:
            news_source = st.selectbox(
                "News Source",
                ["TOI (Times of India)", "NDTV", "WION", "Scroll.in"],
                help="Select which news website to scrape from"
            )
        
        with col2:
            topic = st.text_input(
                "Topic to Search",
                placeholder="e.g., Climate Change, Politics, Education",
                help="Enter the topic you want to scrape articles about"
            )
        
        col3, col4 = st.columns(2)
        
        with col3:
            max_workers = st.slider(
                "Worker Threads",
                min_value=1,
                max_value=8,
                value=4,
                help="More workers = faster scraping, but may overload the server"
            )
        
        with col4:
            max_articles = st.number_input(
                "Max Articles",
                min_value=10,
                max_value=500,
                value=100,
                step=10,
                help="Maximum number of articles to scrape (set lower for HF Spaces)"
            )
        
        if st.button("πŸš€ Start Scraping", type="primary", disabled=not topic):
            if not topic:
                st.error("Please enter a topic to search for!")
            else:
                source_map = {
                    "TOI (Times of India)": "toi",
                    "NDTV": "ndtv",
                    "WION": "wion",
                    "Scroll.in": "scroll"
                }
                source_code = source_map[news_source]
                
                try:
                    with st.spinner(f"πŸ”„ Scraping {news_source} for '{topic}'... This may take a few minutes."):
                        result = run_scraper(source_code, topic, max_workers, max_articles)
                        
                        if result['success']:
                            st.success(f"βœ… Successfully scraped {result['count']} articles!")
                            st.info(f"πŸ“ Saved to: `{result['file']}`")
                            
                            discover_datasets.clear()
                            
                            with st.expander("πŸ“‹ Preview Scraped Data"):
                                df = pd.read_csv(result['file'])
                                st.dataframe(df.head(10), use_container_width=True)
                                st.caption(f"Total rows: {len(df):,}")
                            
                            st.balloons()
                        else:
                            st.error(f"❌ Scraping failed: {result['error']}")
                            
                except Exception as e:
                    st.error(f"❌ Error during scraping: {str(e)}")
                    st.info("Scraper modules may not be available on this platform. Consider uploading pre-scraped data instead.")
    
    st.markdown("---")
    
    datasets = discover_datasets()
    if datasets:
        st.markdown("### πŸ“ Available Datasets")
        for name, path in datasets.items():
            col_a, col_b, col_c = st.columns([3, 1, 1])
            with col_a:
                st.text(f"πŸ“„ {name}")
            with col_b:
                df = load_data(path)
                if df is not None:
                    st.text(f"{len(df):,} articles")
            with col_c:
                if Path(path).exists():
                    with open(path, 'rb') as f:
                        st.download_button(
                            "⬇️",
                            f,
                            file_name=f"{name}.csv",
                            mime='text/csv',
                            key=f"dl_{name}"
                        )
    else:
        st.info("No datasets found. Upload a CSV file or scrape articles to get started!")
    
    st.markdown("---")
    
    st.markdown("### πŸ’Ύ Storage Information")
    try:
        if BASE_DIR.exists():
            file_count = sum(1 for _ in BASE_DIR.rglob('*.csv'))
            st.metric("CSV Files", file_count)
            st.caption(f"Location: `{BASE_DIR}`")
        else:
            st.info("No data directory created yet. Upload a file to get started.")
    except Exception as e:
        st.error(f"Could not access storage: {e}")

def show_analysis_page():
    st.markdown('<h2 class="sub-header">πŸ“Š Sentiment Analysis Dashboard</h2>', 
                unsafe_allow_html=True)
    
    datasets = discover_datasets()
    
    if not datasets:
        st.warning("⚠️ No datasets available. Please upload a CSV file or scrape articles first!")
        st.info("πŸ‘‰ Go to the 'Upload & Scrape' page to get started.")
        return
    
    selected = st.selectbox("Select Dataset", options=list(datasets.keys()))
    
    if selected:
        df = load_data(datasets[selected])
        
        if df is not None:
            col1, col2, col3, col4 = st.columns(4)
            
            with col1:
                st.metric("πŸ“„ Total Articles", f"{len(df):,}")
            
            with col2:
                if 'date' in df.columns:
                    years = f"{df['date'].dt.year.min()}-{df['date'].dt.year.max()}"
                    st.metric("πŸ“… Years", years)
            
            with col3:
                if 'sentiment_value' in df.columns:
                    pos_pct = (df['sentiment_value'].str.lower() == 'positive').mean() * 100
                    st.metric("😊 Positive", f"{pos_pct:.1f}%")
            
            with col4:
                if 'sentiment_value' in df.columns:
                    neg_pct = (df['sentiment_value'].str.lower() == 'negative').mean() * 100
                    st.metric("😞 Negative", f"{neg_pct:.1f}%")
            
            st.markdown("---")
            
            tab1, tab2, tab3, tab4 = st.tabs(["πŸ“ˆ Trends", "πŸ₯§ Distribution", "πŸ—ΊοΈ Geographic", "πŸ“ Articles"])
            
            with tab1:
                fig = plot_sentiment_trends(df, f"{selected} - Sentiment Trends")
                if fig:
                    st.pyplot(fig)
                else:
                    st.info("Insufficient data for trend analysis")
            
            with tab2:
                col_a, col_b = st.columns([2, 1])
                with col_a:
                    pie_fig = create_sentiment_pie(df, "Sentiment Distribution")
                    if pie_fig:
                        st.plotly_chart(pie_fig, use_container_width=True)
                
                with col_b:
                    if 'sentiment_value' in df.columns:
                        st.markdown("### Breakdown")
                        counts = df['sentiment_value'].value_counts()
                        for sentiment, count in counts.items():
                            st.metric(sentiment.title(), f"{count:,}")
            
            with tab3:
                st.markdown("### πŸ—ΊοΈ Geographic Sentiment Analysis")
                
                if 'extracted_location' not in df.columns:
                    if 'desc' in df.columns or 'description' in df.columns:
                        if st.button("πŸ” Extract Locations from Articles"):
                            with st.spinner("Extracting locations... This may take a few minutes."):
                                desc_col = 'desc' if 'desc' in df.columns else 'description'
                                df = extract_locations_from_descriptions(df, desc_col)
                                df.to_csv(datasets[selected], index=False)
                                st.success("βœ… Locations extracted successfully!")
                                st.rerun()
                    else:
                        st.info("No description column found. Cannot extract locations.")
                else:
                    col_left, col_right = st.columns([3, 2])
                    
                    with col_left:
                        st.markdown("#### Sentiment by State")
                        
                        india_geojson = load_india_geojson()
                        
                        if india_geojson:
                            sentiment_by_state = analyze_sentiment_by_state(df)
                            
                            if sentiment_by_state is not None and not sentiment_by_state.empty:
                                map_fig = create_india_sentiment_map(
                                    sentiment_by_state, 
                                    india_geojson,
                                    f"{selected} - Sentiment by Indian States"
                                )
                                if map_fig:
                                    st.plotly_chart(map_fig, use_container_width=True)
                                
                                with st.expander("πŸ“Š State-wise Statistics"):
                                    sentiment_by_state_display = sentiment_by_state.sort_values('count', ascending=False)
                                    st.dataframe(
                                        sentiment_by_state_display,
                                        use_container_width=True,
                                        hide_index=True
                                    )
                            else:
                                st.warning("No location data with valid sentiment found.")
                        else:
                            st.error("Could not load India map data.")
                    
                    with col_right:
                        st.markdown("#### Top Mentioned Locations")
                        top_loc_fig = create_top_locations_chart(df, "Top 15 Locations")
                        if top_loc_fig:
                            st.plotly_chart(top_loc_fig, use_container_width=True)
                        
                        total_articles = len(df)
                        articles_with_location = df['extracted_location'].notna().sum()
                        coverage = (articles_with_location / total_articles) * 100
                        
                        st.metric("Location Coverage", f"{coverage:.1f}%")
                        st.caption(f"{articles_with_location:,} out of {total_articles:,} articles have location data")
            
            with tab4:
                col_a, col_b, col_c = st.columns(3)
                
                with col_a:
                    sentiment_filter = st.selectbox(
                        "Sentiment",
                        ["All"] + sorted(df['sentiment_value'].unique().tolist())
                    )
                
                with col_b:
                    if 'date' in df.columns:
                        years = sorted(df['date'].dt.year.dropna().unique())
                        year_filter = st.selectbox("Year", ["All"] + years)
                    else:
                        year_filter = "All"
                
                with col_c:
                    num_articles = st.slider("Display", 5, 50, 10)
                
                filtered_df = df.copy()
                if sentiment_filter != "All":
                    filtered_df = filtered_df[filtered_df['sentiment_value'] == sentiment_filter]
                if year_filter != "All" and 'date' in df.columns:
                    filtered_df = filtered_df[filtered_df['date'].dt.year == year_filter]
                
                st.write(f"Showing {min(num_articles, len(filtered_df))} of {len(filtered_df)} articles")
                
                for idx, row in filtered_df.head(num_articles).iterrows():
                    with st.expander(f"πŸ“° {row.get('title', 'Untitled')}"):
                        col_x, col_y = st.columns([3, 1])
                        
                        with col_x:
                            st.write(row.get('desc', row.get('description', 'No description')))
                            if 'link' in row:
                                st.markdown(f"[Read more β†’]({row['link']})")
                        
                        with col_y:
                            sentiment = row.get('sentiment_value', 'Unknown')
                            sentiment_emoji = {
                                'positive': '😊',
                                'negative': '😞',
                                'neutral': '😐'
                            }.get(sentiment.lower(), '❓')
                            
                            st.metric("Sentiment", f"{sentiment_emoji} {sentiment.title()}")
                            if 'date' in row:
                                st.caption(f"πŸ“… {row['date'].strftime('%d %b %Y')}")

def show_about_page():
    st.markdown('<h2 class="sub-header">ℹ️ About This Platform</h2>', 
                unsafe_allow_html=True)
    
    st.markdown("""
    ## 🎯 Overview
    
    This platform provides sentiment analysis and visualization for news articles,
    specifically designed for Indian news sources. It includes both upload functionality
    and integrated web scraping capabilities.
    
    ### ✨ Key Features
    
    - **Web Scraping**: Built-in scrapers for TOI, NDTV, WION, and Scroll.in
    - **Data Upload**: Upload pre-scraped CSV files for analysis
    - **Automatic Analysis**: Sentiment classification and scoring
    - **Interactive Visualizations**: Trends, distributions, and comparisons
    - **Geographic Analysis**: State-wise sentiment mapping for India
    - **Data Export**: Download processed datasets
    
    ### πŸ”§ Technical Stack
    
    - **Frontend**: Streamlit
    - **Data Processing**: Pandas, NumPy
    - **Visualization**: Plotly, Matplotlib
    - **NLP**: spaCy for location extraction
    - **Web Scraping**: BeautifulSoup4, Selenium (if available)
    - **Storage**: `/tmp` directory (HF Spaces compatible)
    
    ### πŸ“– How to Use
    
    1. **Scrape or Upload**: 
       - Use the built-in scraper to collect articles from news sources
       - Or upload your own CSV file with pre-scraped data
    2. **Analyze**: Go to Analysis page and select your dataset
    3. **Explore**: View trends, distributions, and geographic sentiment
    4. **Extract Locations**: Use the Geographic tab to extract state information
    5. **Export**: Download processed data for further use
    
    ### πŸ” Supported News Sources
    
    - **Times of India (TOI)**: India's largest English daily
    - **NDTV**: Leading Indian news channel
    - **WION**: International news perspective
    - **Scroll.in**: Independent digital news
    
    ### πŸ“‹ CSV File Format
    
    Your uploaded CSV should contain these columns:
    - `title`: Article headline
    - `date`: Publication date
    - `desc` or `description`: Article content/summary
    - `sentiment_value`: Sentiment label (positive/negative/neutral)
    - `link` (optional): URL to original article
    
    ### ⚠️ Hugging Face Spaces Limitations
    
    - Data stored in `/tmp` is temporary and cleared on restart
    - Scraping may timeout for large datasets (>500 articles)
    - Download your processed data regularly
    - For heavy scraping, consider running locally
    
    ### πŸ’‘ Tips for Best Results
    
    - Start with smaller scrapes (50-100 articles) to test
    - Use specific topics for better quality results
    - Lower worker threads (2-4) for stability on HF Spaces
    - Download results immediately after scraping
    
    ### 🀝 Support
    
    For issues or questions, please refer to the documentation or contact support.
    
    ---
    
    **Version**: 2.0.0 (HF Spaces Edition with Scraping)  
    **Last Updated**: October 2025  
    **Storage**: `/tmp/news_scraper`
    """)

# MAIN APP
def main():
    try:
        load_css()
        init_session_state()
        
        if not setup_directories():
            st.error("Failed to setup directories. Some features may not work.")
        
        with st.sidebar:
            st.markdown("### πŸ“° News Analysis")
            st.markdown("---")
            
            page = st.radio(
                "Navigation",
                ["🏠 Home", "πŸ“€ Upload & Scrape", "πŸ“Š Analysis", "ℹ️ About"],
                label_visibility="collapsed"
            )
            
            st.markdown("---")
            
            st.markdown("### πŸ’Ύ Storage")
            st.caption(f"Location: `/tmp/news_scraper`")
            st.caption("⚠️ Temporary storage")
            
            st.markdown("---")
            
            try:
                datasets = discover_datasets()
                if datasets:
                    st.markdown("### πŸ“Š Quick Stats")
                    total_articles = 0
                    for path in datasets.values():
                        try:
                            df = load_data(path)
                            if df is not None:
                                total_articles += len(df)
                        except:
                            continue
                    
                    if total_articles > 0:
                        st.metric("Total Articles", f"{total_articles:,}")
                        st.metric("Datasets", len(datasets))
                else:
                    st.info("No data yet. Upload a CSV to start!")
            except Exception:
                pass
        
        if page == "🏠 Home":
            show_home_page()
        elif page == "πŸ“€ Upload & Scrape":
            show_scraper_page()
        elif page == "πŸ“Š Analysis":
            show_analysis_page()
        else:
            show_about_page()
            
    except Exception as e:
        st.error(f"❌ Application error: {str(e)}")
        st.info("Try refreshing the page. If the problem persists, the app may need to restart.")
        
        with st.expander("πŸ” Error Details"):
            import traceback
            st.code(traceback.format_exc())

if __name__ == "__main__":
    main()