So, I'm creating a youtube playlist calculator whicxh takes the playlist URl as input from the user, this web-app is wtritten in Flask framework. Code:
from flask import Flask, render_template, request
import re
import requests
from bs4 import BeautifulSoup
app = Flask(__name__)
def calculate_playlist_length(playlist_url):
html = requests.get(playlist_url).text
soup = BeautifulSoup(html, 'html.parser')
total_seconds = 0
for span in soup.select('span.ytd-thumbnail-overlay-time-status-renderer'):
match = re.search(r'(\d+):(\d+)', span.text.strip())
if match:
minutes, seconds = match.groups()
total_seconds += int(minutes) * 60 + int(seconds)
hours = total_seconds // 3600
minutes = (total_seconds % 3600) // 60
seconds = total_seconds % 60
return hours, minutes, seconds
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
playlist_url = request.form['playlist_url']
hours, minutes, seconds = calculate_playlist_length(playlist_url)
return render_template('result.html', hours=hours, minutes=minutes,
seconds=seconds)
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
But on running the app, which compiles succesfully and input a valid and public playlist. I get this output:
The total length of the playlist is 0:00:00.
How do I fix this? I have tried multiple URLs but the outuput is same. Also furthur down the line, I want to host this project on GCP and use YouTube APIs. So please guide me for that too.