diff --git a/django_code/apps/podcast_aggregator/utils.py b/django_code/apps/podcast_aggregator/utils.py index 26c41de..b6bb151 100644 --- a/django_code/apps/podcast_aggregator/utils.py +++ b/django_code/apps/podcast_aggregator/utils.py @@ -1,10 +1,14 @@ import requests import xmltodict from urllib import request as urlrequest +import tempfile +#Django import from apps.telegram_communication.utils import telegramUtils from .models import podcastData + + proxy_url = 'http://gluetun-svc.gluetun.svc:8888' proxy = urlrequest.ProxyHandler({ 'http': proxy_url, @@ -47,7 +51,9 @@ def download_news_mp3(url_mp3, filename): print("Downloading : " + filename +" -- " + url_mp3) try: + urlrequest.urlretrieve(url_mp3, filename) + except: print("Can't download " + filename +" -- " + url_mp3 + " == Cancelling") return 0 @@ -71,8 +77,13 @@ def podcasts(): today_podcast = rss_to_dict(url) print(today_podcast) - file= pretty_filename(today_podcast[1],'mp3') - download_news_mp3(today_podcast[2],file) - telegram.send_document(file,str(today_podcast[0])+" "+str(today_podcast[1])) + pretty_file_name = pretty_filename(today_podcast[1],'mp3') + + temp = tempfile.TemporaryDirectory() + temp_path = temp.name.replace("\\", "/") + file_path = temp_path+"/"+pretty_file_name + download_news_mp3(today_podcast[2],file_path) + print(file_path) + telegram.send_document(file_path,str(today_podcast[0])+" "+str(today_podcast[1])) #main() diff --git a/django_code/apps/telegram_communication/admin.py b/django_code/apps/telegram_communication/admin.py index 208c31d..be65569 100644 --- a/django_code/apps/telegram_communication/admin.py +++ b/django_code/apps/telegram_communication/admin.py @@ -1,8 +1,26 @@ from django.contrib import admin from import_export.admin import ImportExportModelAdmin from .models import TelegramData +from .utils import telegramUtils + +@admin.action(description="Clear all expired messages") +def messages_cleaning(modeladmin, request, queryset): + telegram = telegramUtils() + telegram.clear_expired_messages() # Register your models here. @admin.register(TelegramData) class JobAdmin(ImportExportModelAdmin): list_display = ('message_id','chat_id','description','creation_date','expiration_date') + actions = [messages_cleaning] + + def delete_queryset(self, request, queryset): + """Given a queryset, delete it from the database.""" + print("here") + telegram = telegramUtils() + list_of_message_id = list(queryset.all().values_list('message_id',flat=True)) + telegram.delete_message(list_of_message_id) + #no queryset.delete() because it is specified in delete_message function already + + +