| # 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 | |