From 89d818fe347376d6ff9b0473a167e601c866a18a Mon Sep 17 00:00:00 2001 From: Bram Kelchtermans Date: Thu, 6 Jun 2024 20:10:43 +0200 Subject: [PATCH] youtube-rss --- Dockers/YoutubeRSS/Dockerfile | 20 +++++++++ Dockers/YoutubeRSS/README.MD | 24 +++++++++++ Dockers/YoutubeRSS/app.py | 66 +++++++++++++++++++++++++++++ Dockers/YoutubeRSS/requirements.txt | 5 +++ 4 files changed, 115 insertions(+) create mode 100644 Dockers/YoutubeRSS/Dockerfile create mode 100644 Dockers/YoutubeRSS/README.MD create mode 100644 Dockers/YoutubeRSS/app.py create mode 100644 Dockers/YoutubeRSS/requirements.txt diff --git a/Dockers/YoutubeRSS/Dockerfile b/Dockers/YoutubeRSS/Dockerfile new file mode 100644 index 0000000..ac437d1 --- /dev/null +++ b/Dockers/YoutubeRSS/Dockerfile @@ -0,0 +1,20 @@ +# Use an official Python runtime as a parent image +FROM python:3.8-slim + +# Set the working directory to /app +WORKDIR /app + +# Copy the current directory contents into the container at /app +COPY . /app + +# Install any needed packages specified in requirements.txt +RUN pip install --no-cache-dir -r requirements.txt + +# Make port 5000 available to the world outside this container +EXPOSE 5000 + +# Define environment variable +ENV NAME World + +# Run app.py when the container launches +CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"] diff --git a/Dockers/YoutubeRSS/README.MD b/Dockers/YoutubeRSS/README.MD new file mode 100644 index 0000000..0e3a2df --- /dev/null +++ b/Dockers/YoutubeRSS/README.MD @@ -0,0 +1,24 @@ +# Docker Hub Repository Overview + +**Repository Name:** +[Your Docker Hub Repository Name] + +**Description:** +This Docker image provides a Flask application that acts as a proxy to convert YouTube channel RSS feeds to a more accessible format. It uses a simple Flask API to expose an endpoint that takes a YouTube channel name as a parameter and returns the channel's RSS feed. + +**Usage:** + +- **Pull the Docker Image:** + ```bash + docker pull [username/repository-name:tag] + ``` + +- **Run the Docker Container:** + ```bash + docker run -p 5000:5000 -e youtube-proxy=[your_proxy_url] bramkel/youtuberss/latest + ``` + - Replace [your_proxy_url] with the URL of your preferred YouTube proxy (default is 'https://www.youtube.com'). + - The application will be accessible at http://localhost:5000/[channelName]. + +- **API Endpoint:** + - /[channelName]: Returns the converted RSS feed for the specified YouTube channel. \ No newline at end of file diff --git a/Dockers/YoutubeRSS/app.py b/Dockers/YoutubeRSS/app.py new file mode 100644 index 0000000..6f81467 --- /dev/null +++ b/Dockers/YoutubeRSS/app.py @@ -0,0 +1,66 @@ +from flask import Flask, redirect, url_for, request, Response +import requests +from bs4 import BeautifulSoup +import os +from urllib.parse import urlparse +import xmltodict +import json +import dicttoxml + +app = Flask(__name__) + +def get_youtube_rss_url(channel_url): + # Send a GET request to the YouTube channel page + response = requests.get(channel_url) + + # Check if the request was successful (status code 200) + if response.status_code == 200: + # Parse the HTML content of the page + soup = BeautifulSoup(response.text, 'html.parser') + + # Find the RSS feed URL in the HTML source + rss_link = soup.find('link', {'type': 'application/rss+xml'}) + + if rss_link: + return rss_link.get('href') + else: + print("RSS feed not found on the page.") + return None + else: + print(f"Failed to retrieve the page. Status code: {response.status_code}") + return None + +def prepend_title(xml): + xml_dict = xmltodict.parse(xml) + channel_title = xml_dict["feed"]["title"] + for entry in xml_dict["feed"]["entry"]: + entry["title"] = channel_title + ' - ' + entry["title"] + + xml_data = xmltodict.unparse(xml_dict, pretty=True) + return xml_data + +def read_rss(rss_url): + resp = requests.get(rss_url) + + if resp.status_code == 200: + proxy_url = os.getenv('youtube-proxy', 'https://www.youtube.com') + + parsed_url = urlparse(proxy_url) + scheme = parsed_url.scheme + netloc = parsed_url.netloc + replace_url = scheme + "://" + netloc + "/watch?" + rss_xml = prepend_title(resp.text) + + return rss_xml.replace("https://www.youtube.com/watch?", replace_url) + else: + return None + +@app.route('/') +def doMagic(channelName): + link = 'https://www.youtube.com/@' + channelName + rss_url = get_youtube_rss_url(link) + rss_xml = read_rss(rss_url) + return Response(rss_xml, content_type='application/xml') + +if __name__ == '__main__': + app.run(debug = True) \ No newline at end of file diff --git a/Dockers/YoutubeRSS/requirements.txt b/Dockers/YoutubeRSS/requirements.txt new file mode 100644 index 0000000..ac79e4a --- /dev/null +++ b/Dockers/YoutubeRSS/requirements.txt @@ -0,0 +1,5 @@ +flask +requests +beautifulsoup4 +xmltodict +dicttoxml \ No newline at end of file