From 5bf4c284a5da4e7cc3bc76af2093cdc0e04f9d4e Mon Sep 17 00:00:00 2001 From: James Taylor Date: Tue, 31 Aug 2021 14:46:18 -0700 Subject: subscriptions: Support new subscriptions.csv format MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit According to https://github.com/iv-org/invidious/issues/2319 Google Takeout changed the format from json to csv Signed-off-by: Jesús --- youtube/subscriptions.py | 22 +++++++++++++++++++++- youtube/templates/subscription_manager.html | 2 +- 2 files changed, 22 insertions(+), 2 deletions(-) (limited to 'youtube') diff --git a/youtube/subscriptions.py b/youtube/subscriptions.py index afc3df9..6ab12c7 100644 --- a/youtube/subscriptions.py +++ b/youtube/subscriptions.py @@ -15,6 +15,8 @@ import math import secrets import collections import calendar # bullshit! https://bugs.python.org/issue6280 +import csv +import re import flask from flask import request @@ -729,8 +731,26 @@ def import_subscriptions(): except (AssertionError, IndexError, defusedxml.ElementTree.ParseError) as e: return '400 Bad Request: Unable to read opml xml file, or the file is not the expected format', 400 + elif mime_type == 'text/csv': + content = file.read().decode('utf-8') + reader = csv.reader(content.splitlines()) + channels = [] + for row in reader: + if not row or row[0].lower().strip() == 'channel id': + continue + elif len(row) > 1 and re.fullmatch(r'UC[-_\w]{22}', + row[0].strip()): + channels.append( (row[0], row[-1]) ) + else: + print('WARNING: Unknown row format:', row) else: - return '400 Bad Request: Unsupported file format: ' + mime_type + '. Only subscription.json files (from Google Takeouts) and XML OPML files exported from YouTube\'s subscription manager page are supported', 400 + error = 'Unsupported file format: ' + mime_type + error += (' . Only subscription.json, subscriptions.csv files' + ' (from Google Takeouts)' + ' and XML OPML files exported from Youtube\'s' + ' subscription manager page are supported') + return (flask.render_template('error.html', error_message=error), + 400) _subscribe(channels) diff --git a/youtube/templates/subscription_manager.html b/youtube/templates/subscription_manager.html index 62a8bed..5e240b2 100644 --- a/youtube/templates/subscription_manager.html +++ b/youtube/templates/subscription_manager.html @@ -20,7 +20,7 @@

Import subscriptions

- +
-- cgit v1.2.3