{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Script para grabar el programa de La Hora del Té\n", "
  • Autor: Luis Mejia\n", "
  • Propósito: Grabar los programas de los lideres y ayudar de backup por si las grabaciones del chef fallan.\n", "
  • Licencia: The MIT License" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import requests\n", "from requests.exceptions import RequestException\n", "import logging\n", "from datetime import datetime\n", "import time" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Iniciar el log de errores\n", "logging.basicConfig(filename=r'C:\\\\Users\\\\lmeji\\\\OneDrive\\\\LHT\\\\lht_errors.log', level=logging.ERROR)\n", "\n", "stream_url = r'http://ice42.securenetsystems.net/VOXFM'\n", "morning_start = '075500'\n", "morning_end = '101500'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Definir ventana de horas para grabar. Antes y despues de estas horas, el script no graba la radio.\n", "evening_start = '175500'\n", "evening_end = '201500'\n", "now = datetime.now().strftime('%H%M%S')\n", "record_stream = True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#dependiendo la hora graba el archivo en su respectiva carpeta, solo durante las horas del show\n", "if (now >= morning_start and now <= morning_end):\n", " output_directory = r'C:\\\\Users\\\\lmeji\\\\OneDrive\\\\LHT\\\\LHDD\\\\'\n", "elif (now >= evening_start and now <= evening_end):\n", " output_directory = r'C:\\\\Users\\\\lmeji\\\\OneDrive\\\\LHT\\\\LHT\\\\'\n", "else:\n", " print(\"No hay programa ahorita\")\n", " exit()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Loop en el stream y grabar en su propio archivo\n", "#Si hay problema de red trata de conectarse otra vez y grabar en un nuevo archivo \n", "while record_stream:\n", " try:\n", " if (now >= morning_start and now <= morning_end):\n", " output_file = output_directory + datetime.now().strftime('LHDD-%Y%m%d%H%M%S.mp3')\n", " elif (now >= evening_start and now <= evening_end):\n", " output_file = output_directory + datetime.now().strftime('LHT-%Y%m%d%H%M%S.mp3')\n", "\n", " response = requests.get(stream_url, stream=True)\n", " response.raise_for_status()\n", "\n", " print(f\"Conectado, grabando a los lideres en {output_file}.\")\n", "\n", " with open(output_file, 'wb') as f:\n", " try:\n", " for block in response.iter_content(1024):\n", " f.write(block)\n", " outside_stream_hours = datetime.now().strftime('%H%M%S')\n", " if outside_stream_hours >= morning_start and outside_stream_hours <= morning_end or outside_stream_hours >= evening_start and outside_stream_hours <= evening_end:\n", " record_stream = True\n", " else:\n", " exit() \n", " except RequestException as e:\n", " #Errores de red mientras está grabando\n", " error_time = datetime.now().strftime('%Y%m%d%H%M%S')\n", " print(f\"{error_time}: Network error occurred during streaming: {e}\")\n", " logging.error(f\"{error_time}: Network error occurred during streaming: {e}\")\n", " except Exception as e:\n", " error_time = datetime.now().strftime('%Y%m%d%H%M%S')\n", " print(f\"{error_time}: An error occurred during streaming: {e}\")\n", " logging.error(f\"{error_time}: An error occurred during streaming: {e}\")\n", "\n", " except RequestException as e:\n", " #Errores tratando de conectarse a la radio (Por ejemplo, cuando el stream link está down.)\n", " error_time = datetime.now().strftime('%Y%m%d%H%M%S')\n", " print(f\"{error_time}: Network error occurred when trying to connect to stream: {e}\")\n", " logging.error(f\"{error_time}: Network error occurred when trying to connect to stream: {e}\")\n", "\n", " except Exception as e:\n", " error_time = datetime.now().strftime('%Y%m%d%H%M%S')\n", " print(f\"{error_time}: An error occurred when trying to connect to stream: {e}\")\n", " logging.error(f\"{error_time}: An error occurred when trying to connect to stream: {e}\")\n", "\n", " finally:\n", " #Espera 2 segundos antes de tratar the conectarse otra vez.\n", " time.sleep(2)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.6" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }