diff --git a/newsaggregator/core/urls.py b/newsaggregator/core/urls.py index 24fc00b..2097b64 100644 --- a/newsaggregator/core/urls.py +++ b/newsaggregator/core/urls.py @@ -1,6 +1,8 @@ from django.urls import path from core import views # from .views import base_view +from .views import fetch_article_content + from .views import news_list app_name='core' @@ -25,5 +27,6 @@ path('top-rated/', views.top_rated_articles, name='top_rated_articles'), + path('fetch_article_content/', fetch_article_content, name='fetch_article_content'), ] \ No newline at end of file diff --git a/newsaggregator/core/views.py b/newsaggregator/core/views.py index 0e38f88..d13e749 100644 --- a/newsaggregator/core/views.py +++ b/newsaggregator/core/views.py @@ -241,3 +241,20 @@ def top_rated_articles(request): page = request + +import requests +from django.http import JsonResponse + +def fetch_article_content(request): + url = request.GET.get('url') + if not url: + return JsonResponse({'error': 'URL parameter is missing'}, status=400) + + try: + response = requests.get(url) + response.raise_for_status() + content = response.text + except requests.RequestException as e: + return JsonResponse({'error': str(e)}, status=500) + + return JsonResponse({'content': content}) diff --git a/newsaggregator/db.sqlite3 b/newsaggregator/db.sqlite3 index ca7939d..d789410 100644 Binary files a/newsaggregator/db.sqlite3 and b/newsaggregator/db.sqlite3 differ diff --git a/newsaggregator/static/assets/js/speech.js b/newsaggregator/static/assets/js/speech.js new file mode 100644 index 0000000..26935ee --- /dev/null +++ b/newsaggregator/static/assets/js/speech.js @@ -0,0 +1,60 @@ +let synth = window.speechSynthesis; +let currentUtterance = null; + +function startReadAloud(url) { + if (synth.speaking) { + synth.cancel(); + } + + console.log("Fetching article from URllL:", url); + + fetch(`/fetch_article_content/?url=${encodeURIComponent(url)}`) + .then(response => { + console.log("Response status:", response.status); + if (!response.ok) { + throw new Error("Network response was not ok " + response.statusText); + } + return response.json(); + }) + .then(data => { + if (data.error) { + throw new Error(data.error); + } + console.log("Fetched data successfully"); + const parser = new DOMParser(); + const doc = parser.parseFromString(data.content, 'text/html'); + const articleElement = doc.querySelector('.sc-77igqf-0.fnnahv'); // Updated selector to match your article content + if (!articleElement) { + throw new Error("Article content not found"); + } + const articleContent = articleElement.innerText; + + console.log("Article content extracted:", articleContent); + currentUtterance = new SpeechSynthesisUtterance(articleContent); + synth.speak(currentUtterance); + }) + .catch(error => { + console.error('Error fetching article content:', error); + }); +} + +function pauseReadAloud(button) { + console.log("eeee") + if (synth.speaking && !synth.paused) { + synth.pause(); + button.innerText = "Resume"; + } else if (synth.paused) { + synth.resume(); + button.innerText = "Pause"; + } +} + +function stopReadAloud() { + if (synth.speaking) { + synth.cancel(); + const pauseButton = document.getElementById('pause-resume-btn'); + if (pauseButton) { + pauseButton.innerText = "Pause"; + } + } +} \ No newline at end of file diff --git a/newsaggregator/templates/components/news.html b/newsaggregator/templates/components/news.html index 91b63d0..d9e06b4 100644 --- a/newsaggregator/templates/components/news.html +++ b/newsaggregator/templates/components/news.html @@ -26,26 +26,32 @@
diff --git a/newsaggregator/templates/core/bookmarks.html b/newsaggregator/templates/core/bookmarks.html index a799430..8e01c33 100644 --- a/newsaggregator/templates/core/bookmarks.html +++ b/newsaggregator/templates/core/bookmarks.html @@ -1,4 +1,4 @@ -{% extends 'core/base.html' %} +{% extends 'partials/base.html' %} {% load static %} {% block index %} diff --git a/newsaggregator/templates/core/index.html b/newsaggregator/templates/core/index.html index 8e844a1..8d72c23 100644 --- a/newsaggregator/templates/core/index.html +++ b/newsaggregator/templates/core/index.html @@ -1,5 +1,5 @@ -{% extends 'core/base.html' %} +{% extends 'partials/base.html' %} {% load static %} diff --git a/newsaggregator/templates/partials/base.html b/newsaggregator/templates/partials/base.html index 4718b23..db9e67f 100644 --- a/newsaggregator/templates/partials/base.html +++ b/newsaggregator/templates/partials/base.html @@ -109,6 +109,7 @@ } +