subscriptions: Support new subscriptions.csv format

According to
https://github.com/iv-org/invidious/issues/2319
Google Takeout changed the format from json to csv

Signed-off-by: Jesús <heckyel@hyperbola.info>
This commit is contained in:
James Taylor 2021-08-31 14:46:18 -07:00 committed by Jesús
parent 2ab5b96178
commit 5bf4c284a5
No known key found for this signature in database
GPG Key ID: F6EE7BC59A315766
2 changed files with 22 additions and 2 deletions

View File

@ -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:
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
print('WARNING: Unknown row format:', row)
else:
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)

View File

@ -20,7 +20,7 @@
<form class="subscriptions-import-form" enctype="multipart/form-data" action="/youtube.com/import_subscriptions" method="POST">
<h2>Import subscriptions</h2>
<div class="subscriptions-import-options">
<input type="file" id="subscriptions-import" accept="application/json, application/xml, text/x-opml" name="subscriptions_file">
<input type="file" id="subscriptions-import" accept="application/json, application/xml, text/x-opml, text/csv" name="subscriptions_file">
<input type="submit" value="Import">
</div>
</form>