This commit is contained in:
Luciano Gervasoni
2025-08-14 14:04:33 +02:00
parent 3d09c1acff
commit 015f92a06b
2 changed files with 51 additions and 44 deletions

View File

@@ -1,6 +1,12 @@
import os
from django.http import JsonResponse, HttpResponse
from django.db import connection
import requests
import os
from django.utils import timezone
from django.utils.timezone import now, timedelta
from .models import Urls, Source, Search, UrlContent, UrlsSourceSearch, UrlsDuplicate
from django.db.models import Q, Count
####################################################################################################
"""
@@ -71,4 +77,46 @@ def log_db(request):
""").fetchall()
return HttpResponse( "\n".join([str(e) for e in r]) )
####################################################################################################
####################################################################################################
def notify_status(request):
last_hours = 24
start_date = timezone.now() - timedelta(hours=last_hours)
# Count the number of URLs grouped by status within the date range
urls_data_status = Urls.objects.filter(ts_fetch__gte=start_date) \
.values('status') \
.annotate(count=Count('id')) \
.order_by('status')
# Count the number of URLs grouped by source
urls_data_source = UrlsSourceSearch.objects \
.filter(id_url__ts_fetch__gte=start_date) \
.values('id_source__source') \
.annotate(count=Count('id_url')) \
.order_by('id_source__source')
# Count the number of URLs grouped by search
urls_data_search = UrlsSourceSearch.objects \
.filter(id_url__ts_fetch__gte=start_date) \
.values('id_search__search') \
.annotate(count=Count('id_url')) \
.order_by('id_search__search')
bot_token = os.getenv("TELEGRAM_BOT_TOKEN", "")
chat_id = os.getenv("TELEGRAM_CHAT_ID", "")
message = "During the last {} hours:\n{}\n{}\n{}".format(last_hours, urls_data_status, urls_data_source, urls_data_search)
url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
params = {
"chat_id": chat_id,
"text": message
}
# POST
response = requests.post(url, params=params)
# print(response.json()) # Check the response
return HttpResponse( "\n".join([str(e) for e in message]) )