youtube-rss

This commit is contained in:
Bram Kelchtermans
2024-06-06 20:10:43 +02:00
parent f3ec017fa3
commit 89d818fe34
4 changed files with 115 additions and 0 deletions
+66
View File
@@ -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('/<channelName>')
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)