Files
Django_kube/django_code/apps/podcast_aggregator/utils.py
T
pi-ux-ce f9f831738d
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 3s
parse the podcasts and run them
2026-07-02 10:43:59 +01:00

64 lines
2.0 KiB
Python

import requests
import xmltodict
from urllib import request as urlrequest
from apps.telegram_communication.utils import telegramUtils
from .models import podcastData
def rss_to_dict(url):
response = requests.get(url)
data = xmltodict.parse(response.content)
print(data['rss'])
title = data['rss']['channel']['title']
#Get the keys if needed of debug
#print(data['rss'].keys())
#use first object as it's ordered in a chronological order
today_object = data['rss']['channel']['item']
if isinstance(today_object, list):
today_object = data['rss']['channel']['item'][0]
today_title = today_object['title']
today_podcast_url = today_object['enclosure']['@url']
return title,today_title,today_podcast_url
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
def pretty_filename(filename,extension=''):
filename = filename.replace(' ', '_')
pretty_filename = filename.replace('/', '_')
if extension != '':
extension = str('.'+extension)
return pretty_filename+extension
def podcasts():
telegram = telegramUtils()
#clear all expired messages before
telegram.clear_expired_messages()
podcasts = podcastData.objects.all()
for podcast in podcasts:
url = podcast.podcast_url
#url = "https://feeds.audiomeans.fr/feed/44f6a116-20a6-44c4-98c4-b3e1bdd46dce.xml"
#url_bbc = "https://podcast.voice.api.bbci.co.uk/rss/audio/p002vsmz?api_key=Wbek5zSqxz0Hk1blo5IBqbd9SCWIfNbT"
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]))
#main()