File size: 12,642 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 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 |
# News API Implementation Summary
# خلاصه پیادهسازی API اخبار
---
## English Summary
### What Was Done
The news page has been completely updated to integrate with the News API service, replacing the previous implementation with a robust, production-ready solution.
### Key Improvements
#### 1. **News API Integration**
- ✅ Integrated with [NewsAPI.org](https://newsapi.org/)
- ✅ Fetches real-time cryptocurrency news
- ✅ Configurable search parameters
- ✅ Automatic date filtering (last 7 days)
- ✅ Sorted by most recent articles
#### 2. **Comprehensive Error Handling**
- ✅ Invalid API key detection
- ✅ Rate limiting management
- ✅ Network connectivity checks
- ✅ Server error handling
- ✅ Automatic fallback to demo data
#### 3. **Enhanced UI/UX**
- ✅ Article images support
- ✅ Author information display
- ✅ Sentiment badges (Positive/Negative/Neutral)
- ✅ Improved card layout
- ✅ Responsive design
- ✅ Loading states
- ✅ Empty states
#### 4. **Smart Sentiment Analysis**
- ✅ Keyword-based sentiment detection
- ✅ Configurable sentiment keywords
- ✅ Visual sentiment indicators
- ✅ Sentiment-based filtering
#### 5. **Flexible Configuration**
- ✅ Centralized configuration file (`news-config.js`)
- ✅ Customizable API settings
- ✅ Adjustable refresh intervals
- ✅ Display preferences
### How Users Access the Services
#### **Method 1: Web Browser (Most Common)**
Simply open the news page in a web browser:
```
http://localhost:3000/static/pages/news/index.html
```
The page automatically:
- Loads latest cryptocurrency news
- Refreshes every 60 seconds
- Provides search and filter options
- Shows sentiment analysis
#### **Method 2: Direct API Calls**
Users can query the API directly using HTTP requests:
**Get All News:**
```bash
curl "http://localhost:3000/api/news?limit=50"
```
**Filter by Sentiment:**
```bash
curl "http://localhost:3000/api/news?sentiment=positive"
```
**Filter by Source:**
```bash
curl "http://localhost:3000/api/news?source=CoinDesk"
```
#### **Method 3: JavaScript Client**
```javascript
// In browser or Node.js
const client = new CryptoNewsClient('http://localhost:3000');
// Get all news
const articles = await client.getAllNews(50);
// Search for Bitcoin news
const bitcoinNews = await client.searchNews('bitcoin');
// Get positive sentiment news
const positiveNews = await client.getNewsBySentiment('positive');
// Get statistics
const stats = await client.getNewsStatistics();
```
#### **Method 4: Python Client**
```python
from api_client_examples import CryptoNewsClient
# Create client
client = CryptoNewsClient('http://localhost:3000')
# Get all news
articles = client.get_all_news(limit=50)
# Search for Ethereum news
ethereum_news = client.search_news('ethereum')
# Get statistics
stats = client.get_news_statistics()
```
### API Endpoints
| Endpoint | Method | Parameters | Description |
|----------|--------|------------|-------------|
| `/api/news` | GET | `limit`, `source`, `sentiment` | Get news articles |
| `/api/crypto/prices` | GET | `symbols` | Get crypto prices |
| `/api/crypto/market` | GET | - | Get market overview |
| `/api/crypto/history` | GET | `symbol`, `days` | Get historical data |
### Response Format
```json
{
"articles": [
{
"title": "Bitcoin Reaches New High",
"content": "Article description...",
"source": {
"title": "CryptoNews"
},
"published_at": "2025-11-30T10:00:00Z",
"url": "https://example.com/article",
"urlToImage": "https://example.com/image.jpg",
"author": "John Doe",
"sentiment": "positive",
"category": "crypto"
}
],
"total": 50,
"fallback": false
}
```
### Files Created/Modified
```
static/pages/news/
├── index.html (Modified)
├── news.js (Modified - Major Update)
├── news.css (Modified)
├── news-config.js (New)
├── README.md (New)
├── API-USAGE-GUIDE.md (New)
├── IMPLEMENTATION-SUMMARY.md (This file)
└── examples/
├── basic-usage.html (New)
├── api-client-examples.js (New)
└── api-client-examples.py (New)
```
### How to Use
#### For End Users:
1. Open `http://localhost:3000/static/pages/news/index.html`
2. Browse latest cryptocurrency news
3. Use search box to find specific topics
4. Filter by source or sentiment
5. Click "Read Full Article" to view complete news
#### For Developers:
1. **Import the client:**
```javascript
import { CryptoNewsClient } from './examples/api-client-examples.js';
```
2. **Make API calls:**
```javascript
const client = new CryptoNewsClient();
const news = await client.getAllNews();
```
3. **Customize configuration:**
Edit `news-config.js` to change settings
4. **View examples:**
- HTML: Open `examples/basic-usage.html`
- JavaScript: Run `node examples/api-client-examples.js`
- Python: Run `python examples/api-client-examples.py`
---
## خلاصه فارسی
### تغییرات انجام شده
صفحه اخبار به طور کامل بهروز شده و با سرویس News API یکپارچه شده است.
### بهبودهای کلیدی
#### ۱. **یکپارچهسازی با News API**
- ✅ اتصال به [NewsAPI.org](https://newsapi.org/)
- ✅ دریافت اخبار لحظهای ارزهای دیجیتال
- ✅ پارامترهای جستجوی قابل تنظیم
- ✅ فیلتر خودکار بر اساس تاریخ (۷ روز گذشته)
- ✅ مرتبسازی بر اساس جدیدترین مقالات
#### ۲. **مدیریت جامع خطاها**
- ✅ تشخیص کلید API نامعتبر
- ✅ مدیریت محدودیت درخواست
- ✅ بررسی اتصال به اینترنت
- ✅ مدیریت خطاهای سرور
- ✅ بازگشت خودکار به دادههای نمایشی
#### ۳. **بهبود رابط کاربری**
- ✅ نمایش تصاویر مقالات
- ✅ نمایش اطلاعات نویسنده
- ✅ نشانهای احساسی (مثبت/منفی/خنثی)
- ✅ طرح کارت بهبود یافته
- ✅ طراحی واکنشگرا
- ✅ حالتهای بارگذاری
- ✅ حالتهای خالی
#### ۴. **تحلیل هوشمند احساسات**
- ✅ تشخیص احساسات بر اساس کلمات کلیدی
- ✅ کلمات کلیدی احساسی قابل تنظیم
- ✅ نشانگرهای بصری احساسات
- ✅ فیلتر بر اساس احساسات
### چگونه کاربران از سرویسها استفاده میکنند
#### **روش ۱: مرورگر وب (متداولترین)**
به سادگی صفحه اخبار را در مرورگر باز کنید:
```
http://localhost:3000/static/pages/news/index.html
```
صفحه به طور خودکار:
- آخرین اخبار ارز دیجیتال را بارگذاری میکند
- هر ۶۰ ثانیه بهروز میشود
- گزینههای جستجو و فیلتر ارائه میدهد
- تحلیل احساسات نمایش میدهد
#### **روش ۲: فراخوانی مستقیم API**
کاربران میتوانند مستقیماً با درخواستهای HTTP به API دسترسی داشته باشند:
**دریافت تمام اخبار:**
```bash
curl "http://localhost:3000/api/news?limit=50"
```
**فیلتر بر اساس احساسات:**
```bash
curl "http://localhost:3000/api/news?sentiment=positive"
```
**فیلتر بر اساس منبع:**
```bash
curl "http://localhost:3000/api/news?source=CoinDesk"
```
#### **روش ۳: کلاینت جاوااسکریپت**
```javascript
// در مرورگر یا Node.js
const client = new CryptoNewsClient('http://localhost:3000');
// دریافت تمام اخبار
const articles = await client.getAllNews(50);
// جستجوی اخبار بیتکوین
const bitcoinNews = await client.searchNews('bitcoin');
// دریافت اخبار با احساسات مثبت
const positiveNews = await client.getNewsBySentiment('positive');
// دریافت آمار
const stats = await client.getNewsStatistics();
```
#### **روش ۴: کلاینت پایتون**
```python
from api_client_examples import CryptoNewsClient
# ساخت کلاینت
client = CryptoNewsClient('http://localhost:3000')
# دریافت تمام اخبار
articles = client.get_all_news(limit=50)
# جستجوی اخبار اتریوم
ethereum_news = client.search_news('ethereum')
# دریافت آمار
stats = client.get_news_statistics()
```
### نقاط پایانی API
| نقطه پایانی | متد | پارامترها | توضیحات |
|-------------|------|-----------|---------|
| `/api/news` | GET | `limit`, `source`, `sentiment` | دریافت مقالات خبری |
| `/api/crypto/prices` | GET | `symbols` | دریافت قیمتهای ارز دیجیتال |
| `/api/crypto/market` | GET | - | دریافت نمای کلی بازار |
| `/api/crypto/history` | GET | `symbol`, `days` | دریافت دادههای تاریخی |
### فرمت پاسخ
```json
{
"articles": [
{
"title": "بیتکوین به رکورد جدید رسید",
"content": "توضیحات مقاله...",
"source": {
"title": "اخبار کریپتو"
},
"published_at": "2025-11-30T10:00:00Z",
"url": "https://example.com/article",
"urlToImage": "https://example.com/image.jpg",
"author": "نام نویسنده",
"sentiment": "positive",
"category": "crypto"
}
],
"total": 50,
"fallback": false
}
```
### نحوه استفاده
#### برای کاربران نهایی:
1. `http://localhost:3000/static/pages/news/index.html` را باز کنید
2. آخرین اخبار ارز دیجیتال را مرور کنید
3. از جعبه جستجو برای یافتن موضوعات خاص استفاده کنید
4. بر اساس منبع یا احساسات فیلتر کنید
5. برای مشاهده خبر کامل روی "ادامه مطلب" کلیک کنید
#### برای توسعهدهندگان:
1. **وارد کردن کلاینت:**
```javascript
import { CryptoNewsClient } from './examples/api-client-examples.js';
```
2. **فراخوانی API:**
```javascript
const client = new CryptoNewsClient();
const news = await client.getAllNews();
```
3. **سفارشیسازی تنظیمات:**
فایل `news-config.js` را ویرایش کنید
4. **مشاهده مثالها:**
- HTML: فایل `examples/basic-usage.html` را باز کنید
- JavaScript: `node examples/api-client-examples.js` را اجرا کنید
- Python: `python examples/api-client-examples.py` را اجرا کنید
---
## Quick Start Guide
### For Users (کاربران):
```
1. Open browser → مرورگر را باز کنید
2. Go to: http://localhost:3000/static/pages/news/index.html
3. Browse news → اخبار را مرور کنید
4. Use filters → از فیلترها استفاده کنید
5. Click articles → روی مقالات کلیک کنید
```
### For Developers (توسعهدهندگان):
```javascript
// Quick start code
const client = new CryptoNewsClient();
const articles = await client.getAllNews();
console.log(articles);
```
```python
# Quick start code
from api_client_examples import CryptoNewsClient
client = CryptoNewsClient()
articles = client.get_all_news()
print(articles)
```
---
## Support & Documentation
- **README**: Detailed feature documentation
- **API-USAGE-GUIDE**: Complete API reference (English & فارسی)
- **Examples**: Working code samples in HTML, JS, Python
- **Configuration**: `news-config.js` for customization
## Notes
- Free API tier: 100 requests/day
- Auto-refresh: Every 60 seconds
- Fallback data: Available if API fails
- Languages: English & فارسی supported
- Responsive: Works on mobile & desktop
|