aboutsummaryrefslogtreecommitdiffstats
path: root/youtube
diff options
context:
space:
mode:
authorJames Taylor <user234683@users.noreply.github.com>2021-04-14 19:28:30 -0700
committerJesús <heckyel@hyperbola.info>2021-05-06 14:24:00 -0500
commit0ee1aabc74c4ca200dc3bd56ccaa40dbc3ecab59 (patch)
tree93ef9588fc3fbb12875fb7fe676f4ba5b66456a0 /youtube
parentccb3392e1d8def39125a8f83f69c11488f39d118 (diff)
downloadyt-local-0ee1aabc74c4ca200dc3bd56ccaa40dbc3ecab59.tar.lz
yt-local-0ee1aabc74c4ca200dc3bd56ccaa40dbc3ecab59.tar.xz
yt-local-0ee1aabc74c4ca200dc3bd56ccaa40dbc3ecab59.zip
Add local playlist export option
Temporary workaround for #61 Signed-off-by: Jesús <heckyel@hyperbola.info>
Diffstat (limited to 'youtube')
-rw-r--r--youtube/local_playlist.py68
-rw-r--r--youtube/templates/local_playlist.html14
2 files changed, 70 insertions, 12 deletions
diff --git a/youtube/local_playlist.py b/youtube/local_playlist.py
index 776e992..40ff035 100644
--- a/youtube/local_playlist.py
+++ b/youtube/local_playlist.py
@@ -39,32 +39,54 @@ def add_to_playlist(name, video_info_list):
gevent.spawn(util.download_thumbnails, os.path.join(thumbnails_directory, name), missing_thumbnails)
-def get_local_playlist_videos(name, offset=0, amount=50):
+def add_extra_info_to_videos(videos, playlist_name):
+ '''Adds extra information necessary for rendering the video item HTML
+ Downloads missing thumbnails'''
try:
- thumbnails = set(os.listdir(os.path.join(thumbnails_directory, name)))
+ thumbnails = set(os.listdir(os.path.join(thumbnails_directory,
+ playlist_name)))
except FileNotFoundError:
thumbnails = set()
missing_thumbnails = []
+ for video in videos:
+ video['type'] = 'video'
+ util.add_extra_html_info(video)
+ if video['id'] + '.jpg' in thumbnails:
+ video['thumbnail'] = (
+ '/https://youtube.com/data/playlist_thumbnails/'
+ + playlist_name
+ + '/' + video['id'] + '.jpg')
+ else:
+ video['thumbnail'] = util.get_thumbnail_url(video['id'])
+ missing_thumbnails.append(video['id'])
+
+ gevent.spawn(util.download_thumbnails,
+ os.path.join(thumbnails_directory, playlist_name),
+ missing_thumbnails)
+
+
+def read_playlist(name):
+ '''Returns a list of videos for the given playlist name'''
+ playlist_path = os.path.join(playlists_directory, name + '.txt')
+ with open(playlist_path, 'r', encoding='utf-8') as f:
+ data = f.read()
+
videos = []
- with open(os.path.join(playlists_directory, name + ".txt"), 'r', encoding='utf-8') as file:
- data = file.read()
videos_json = data.splitlines()
for video_json in videos_json:
try:
info = json.loads(video_json)
- if info['id'] + ".jpg" in thumbnails:
- info['thumbnail'] = "/https://youtube.com/data/playlist_thumbnails/" + name + "/" + info['id'] + ".jpg"
- else:
- info['thumbnail'] = util.get_thumbnail_url(info['id'])
- missing_thumbnails.append(info['id'])
- info['type'] = 'video'
- util.add_extra_html_info(info)
videos.append(info)
except json.decoder.JSONDecodeError:
if not video_json.strip() == '':
print('Corrupt playlist video entry: ' + video_json)
- gevent.spawn(util.download_thumbnails, os.path.join(thumbnails_directory, name), missing_thumbnails)
+ return videos
+
+
+def get_local_playlist_videos(name, offset=0, amount=50):
+ videos = read_playlist(name)
+ add_extra_info_to_videos(videos, name)
return videos[offset:offset+amount], len(videos)
@@ -131,6 +153,28 @@ def path_edit_playlist(playlist_name):
number_of_videos_remaining = remove_from_playlist(playlist_name, videos_to_remove)
redirect_page_number = min(int(request.values.get('page', 1)), math.ceil(number_of_videos_remaining/50))
return flask.redirect(util.URL_ORIGIN + request.path + '?page=' + str(redirect_page_number))
+ elif request.values['action'] == 'export':
+ videos = read_playlist(playlist_name)
+ fmt = request.values['export_format']
+ if fmt in ('ids', 'urls'):
+ prefix = ''
+ if fmt == 'urls':
+ prefix = 'https://www.youtube.com/watch?v='
+ id_list = '\n'.join(prefix + v['id'] for v in videos)
+ id_list += '\n'
+ resp = flask.Response(id_list, mimetype='text/plain')
+ cd = 'attachment; filename="%s.txt"' % playlist_name
+ resp.headers['Content-Disposition'] = cd
+ return resp
+ elif fmt == 'json':
+ json_data = json.dumps({'videos': videos}, indent=2,
+ sort_keys=True)
+ resp = flask.Response(json_data, mimetype='text/json')
+ cd = 'attachment; filename="%s.json"' % playlist_name
+ resp.headers['Content-Disposition'] = cd
+ return resp
+ else:
+ flask.abort(400)
else:
flask.abort(400)
diff --git a/youtube/templates/local_playlist.html b/youtube/templates/local_playlist.html
index 765ac66..64abcf4 100644
--- a/youtube/templates/local_playlist.html
+++ b/youtube/templates/local_playlist.html
@@ -9,6 +9,20 @@
{% block main %}
<div class="playlist-metadata">
<h2 class="play-title">{{ playlist_name }}</h2>
+
+ <div id="export-options">
+ <form id="playlist-export" method="post">
+ <select id="export-type" name="export_format">
+ <option value="json">JSON</option>
+ <option value="ids">Video id list (txt)</option>
+ <option value="urls">Video url list (txt)</option>
+ </select>
+ <button type="submit" id="playlist-export-button" name="action" value="export">Export</button>
+ </form>
+ </div>
+ </div>
+
+ <div class="playlist-metadata" id="video-remove-container">
<input type="hidden" name="playlist_page" value="{{ playlist_name }}" form="playlist-edit">
<button class="play-action" type="submit" id="playlist-remove-button" name="action" value="remove" form="playlist-edit" formaction="">Remove from playlist</button>
</div>