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:
parent
2ab5b96178
commit
5bf4c284a5
@ -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)
|
||||
|
||||
|
@ -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>
|
||||
|
Loading…
x
Reference in New Issue
Block a user