Working extraction of title and url of video and videos in channels / playlists

This commit is contained in:
Maximilian Wagner
2023-07-31 14:01:34 +02:00
parent 9929b34bdb
commit 9f292655b9
7 changed files with 101 additions and 19 deletions

3
.idea/misc.xml generated
View File

@@ -1,4 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (yt-dls)" project-jdk-type="Python SDK" />
<component name="PythonCompatibilityInspectionAdvertiser">
<option name="version" value="3" />
</component>
</project>

3
app.py
View File

@@ -2,8 +2,7 @@ from flask import Flask, current_app, g
from flask_bootstrap import Bootstrap
from flask_wtf.csrf import CSRFProtect
from frontend import frontend, get_db
from nav import nav
from frontend import frontend, get_db, nav
from os import urandom

View File

@@ -5,4 +5,4 @@ from wtforms.validators import URL, DataRequired
class DownloadForm(FlaskForm):
url = URLField('Video, Channel or Playlist', validators=[DataRequired(), URL()], render_kw={'placeholder': 'YouTube Link'})
submit = SubmitField('submit')
submit = SubmitField('Go')

View File

@@ -1,21 +1,25 @@
from flask import Blueprint, render_template, send_from_directory, request, current_app, g
from flask_nav3.elements import Navbar, View
import sqlite3
import os
from __future__ import unicode_literals
import os
import sqlite3
import yt_dlp as ydl
from flask import Blueprint, render_template, send_from_directory, current_app, g
from flask_nav3 import Nav
from flask_nav3.elements import Navbar, View
import forms.download
from nav import nav
from forms.download import DownloadForm
frontend = Blueprint('frontend', __name__)
nav = Nav()
nav.register_element('frontend_top', Navbar(
View('yt-dls', '.index'),
View('downloader', '.downloader'),
View('updater', '.updater'),
View('library', '.library')
)
)
)
@frontend.route('/', methods=['GET'])
@@ -26,13 +30,52 @@ def index():
@frontend.route('/downloader', methods=['GET', 'POST'])
def downloader():
form = DownloadForm()
if form.validate_on_submit():
# put here the interaction with youtube-dl
# or forward to site that shows details of yt link
url = form.url.data
return f"Hello There {url}"
return render_template('downloader.html', form=form)
url = str(form.url.data)
# so error message does not get displayed when url is empty
# validate_on_submit requires len(str) > 0 in DownloadForm
ytLink = True
titles = []
urls = []
if form.validate_on_submit():
ytLink = True if 'youtube.com' in url else False
if ytLink:
query = ydl.YoutubeDL({'quiet': True}).extract_info(url=url, download=False)
# if downloading playlist
try:
for video in query['entries']:
ydl.YoutubeDL({'quiet': True}).extract_info('https://www.youtube.com/watch?v=' + video['id'], download=False)
titles.append(video['title'])
urls.append('https://www.youtube.com/watch?v=' + video['id'])
return render_template('downloader.html', form=form, ytLink=ytLink, titles=titles, urls=urls,
amount=len(titles))
except:
pass
# if downloading channel
try:
for tab in query['entries']:
for video in tab['entries']:
titles.append(video['title'])
urls.append('https://www.youtube.com/watch?v=' + video['id'])
return render_template('downloader.html', form=form, ytLink=ytLink, titles=titles, urls=urls,
amount=len(titles))
except:
pass
# if downloading video
try:
titles.append(query['title'])
urls.append('https://www.youtube.com/watch?v=' + query['id'])
return render_template('downloader.html', form=form, ytLink=ytLink, titles=titles, urls=urls,
amount=len(titles))
except:
pass
return render_template('downloader.html', form=form, ytLink=ytLink, titles=titles, urls=urls, amount=len(titles))
@frontend.route('/download/<path:file>', methods=['GET'])
@@ -79,3 +122,18 @@ def query_db(query, args=(), one=False):
res = cur.fetchall()
cur.close()
return (res[0] if res else None) if one else res
def db_add_collection(info):
return
def db_add_single(info):
return
async def download_all(info):
return
# todo: cache results from extract_info for /download and don't fetch again. update of existing downloads only over /update

3
nav.py
View File

@@ -1,3 +0,0 @@
from flask_nav3 import Nav
nav = Nav()

View File

@@ -4,3 +4,4 @@ flask_bootstrap
flask_nav3
flask_login
flask_wtf
yt_dlp

View File

@@ -11,5 +11,29 @@
{% if form.errors %}
{{ form.errors['url'][0][:-1] + ', try again.' }}
{% endif %}
{% if ytLink == False %}
Please enter a full, valid YouTube URL.
{% endif %}
{% if titles %}
<div class="container">
<table id="videos" class="table">
<thead>
<tr>
<th scope="col" class="text-center">Title</th>
<th scope="col" class="text-center">URL</th>
</tr>
</thead>
<tbody>
{% for i in range(amount) %}
<tr>
<td class="text-center">{{ titles[i] }}</td>
<td class="text-center"> <a href="{{ urls[i] }}" target="_blank">Link</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}
</div>
{%- endblock %}