File size: 4,869 Bytes
b190b45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# News Page - News API Integration

## Overview

This news page has been updated to integrate with the [News API](https://newsapi.org/) to fetch real-time cryptocurrency news articles. The implementation includes comprehensive error handling, sentiment analysis, and a modern UI with image support.

## Features

### 1. **News API Integration**
- Fetches cryptocurrency news from News API
- Configurable search queries (default: cryptocurrency, Bitcoin, Ethereum)
- Automatic date filtering (last 7 days by default)
- Sorted by most recent articles

### 2. **Error Handling**
The system handles multiple error scenarios:
- **Invalid API Key**: Displays authentication error message
- **Rate Limiting**: Notifies when API rate limit is exceeded
- **No Internet**: Detects network connectivity issues
- **Server Errors**: Handles News API server issues
- **Fallback Data**: Automatically switches to demo data if API fails

### 3. **Article Display**
Each article shows:
- **Title**: Article headline
- **Description**: Article summary/content
- **URL**: Link to full article (opens in new tab)
- **Image**: Article thumbnail (if available)
- **Source**: News source name
- **Author**: Article author (if available)
- **Timestamp**: Relative time (e.g., "2h ago")
- **Sentiment Badge**: Positive/Negative/Neutral indicator

### 4. **Sentiment Analysis**
Automatic sentiment detection based on keywords:
- **Positive**: surge, rise, gain, bullish, growth, etc.
- **Negative**: fall, drop, crash, bearish, decline, etc.
- **Neutral**: Neither positive nor negative

### 5. **Filtering & Search**
- **Keyword Search**: Real-time search across titles and descriptions
- **Source Filter**: Filter by news source
- **Sentiment Filter**: Filter by sentiment (positive/negative/neutral)

## Configuration

Edit `news-config.js` to customize settings:

```javascript

export const NEWS_CONFIG = {

  // API Settings

  apiKey: 'YOUR_API_KEY_HERE',

  baseUrl: 'https://newsapi.org/v2',

  

  // Search Parameters

  defaultQuery: 'cryptocurrency OR bitcoin OR ethereum',

  language: 'en',

  pageSize: 100,

  daysBack: 7,

  

  // Refresh Settings

  autoRefreshInterval: 60000, // milliseconds

  

  // Display Settings

  showImages: true,

  showAuthor: true,

  showSentiment: true

};

```

## API Key Setup

1. Get your free API key from [newsapi.org](https://newsapi.org/register)
2. Update the `apiKey` in `news-config.js`
3. Free tier includes:
   - 100 requests per day
   - Articles from the last 30 days
   - All sources and languages

## File Structure

```

static/pages/news/

β”œβ”€β”€ index.html          # HTML structure

β”œβ”€β”€ news.js             # Main JavaScript logic

β”œβ”€β”€ news.css            # Styling

β”œβ”€β”€ news-config.js      # Configuration settings

└── README.md           # This file

```

## Key Functions

### `fetchFromNewsAPI()`
Fetches articles from News API with proper error handling.

### `formatNewsAPIArticles(articles)`
Transforms News API response to internal format.

### `analyzeSentiment(text)`
Performs keyword-based sentiment analysis.

### `handleAPIError(error)`
Displays user-friendly error messages.

### `renderNews()`
Renders articles to the DOM with images and formatting.

## Error Messages

| Error | User Message |
|-------|-------------|
| Invalid API key | API authentication failed. Please check your API key. |
| Rate limit exceeded | Too many requests. Please try again later. |
| Server error | News service is temporarily unavailable. |
| No internet | No internet connection. Please check your network. |

## Browser Compatibility

- Modern browsers (Chrome, Firefox, Safari, Edge)
- ES6+ features required
- Fetch API support required

## Demo Data

If the API is unavailable, the system automatically loads demo cryptocurrency news to ensure the page always displays content.

## Performance

- Auto-refresh: Every 60 seconds (configurable)
- Lazy loading for images
- Efficient client-side filtering
- Responsive grid layout

## Styling

The page uses a modern glass-morphism design with:
- Gradient accents
- Smooth animations
- Hover effects
- Responsive layout
- Dark theme optimized

## Future Enhancements

Potential improvements:
- Multi-language support
- Category filtering
- Bookmarking articles
- Share functionality
- Advanced sentiment analysis (ML-based)
- Custom RSS feed support
- Export to PDF/CSV

## Support

For issues or questions:
1. Check News API status: [status.newsapi.org](https://status.newsapi.org/)
2. Verify API key is valid
3. Check browser console for errors
4. Review configuration settings

## License

This implementation uses the News API service which has its own [Terms of Service](https://newsapi.org/terms).