From 57c5d2029ce478522c3b73e9bacb72b923fe49d5 Mon Sep 17 00:00:00 2001 From: rashiranjan22 Date: Sat, 8 Jun 2024 00:32:54 +0530 Subject: [PATCH 1/2] corrected html paths --- newsaggregator/db.sqlite3 | Bin 241664 -> 241664 bytes newsaggregator/templates/core/bookmarks.html | 2 +- newsaggregator/templates/core/index.html | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/newsaggregator/db.sqlite3 b/newsaggregator/db.sqlite3 index ca7939de38aec020c0857bf1e97bb74eac403fd3..d78941098cee899187747041637d0df7ea713ec4 100644 GIT binary patch delta 335 zcmZp8z}N7AZ-O)<-$WT_Rz3#3aIcLi3;JbvSpG8bckzqx&Eu`)3E|e~;^KJ1zMtKb zO_}BIX2l8jSk#qySbdvA9kWb|^3yAGEK+l_N-Oj8jPp_~GLy41EDOxDO3EzE(kJuG z+!ExLT99Xz6X9kOoEQ|G8Wx_F8D&sn7@Csl;Z$W<<`P^{abjm%PY%{a7_y?sW3OMa`%cX z^$B%NOis&AbGGykGz<1HO)m>cjS4R@4$nvp3N&?fH_vk`%QcJ-D~~TX@wUwJDvUHV z%nT|nGxJM#D>Sz-F%5Cdu5@%MD|7Yob#t!IEQ%~}EDub~a!GVD3oTAf3@J{p2v75= zaCY&tEVnW&c1!j*vntGSGV$>Ub}z~*a&+@D$_~#-F{`Zf4h~O=a!#&@PcJKT^es27 zEHaAnH8L -{% extends 'core/base.html' %} +{% extends 'partials/base.html' %} {% load static %} From b2304e3b69c1c95858a7b3d6e5e27198fe6acca9 Mon Sep 17 00:00:00 2001 From: rashiranjan22 Date: Sat, 8 Jun 2024 00:40:46 +0530 Subject: [PATCH 2/2] added read aloud --- newsaggregator/core/urls.py | 3 + newsaggregator/core/views.py | 17 ++++ newsaggregator/static/assets/js/speech.js | 60 ++++++++++++++ newsaggregator/templates/components/news.html | 83 +++++++++---------- newsaggregator/templates/partials/base.html | 1 + 5 files changed, 121 insertions(+), 43 deletions(-) create mode 100644 newsaggregator/static/assets/js/speech.js 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/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/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 @@ } +