Merge pull request #92 from 0ihgk1uVBLlTBzVo7F9B/dev-indep

Simplify import process.
This commit is contained in:
PLUJA 2020-10-05 09:11:06 +02:00 committed by GitHub
commit d27990e56a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 51 deletions

View File

@ -47,7 +47,6 @@ YOUTUBERSS = "https://www.youtube.com/feeds/videos.xml?channel_id="
########################## ##########################
#### Global variables #### #### Global variables ####
########################## ##########################
ALLOWED_EXTENSIONS = {'json', 'db'}
######################### #########################
#### Twitter Logic ###### #### Twitter Logic ######
@ -296,28 +295,34 @@ def ytsearch():
def ytfollow(channelId): def ytfollow(channelId):
form = EmptyForm() form = EmptyForm()
if form.validate_on_submit(): if form.validate_on_submit():
r = followYoutubeChannel(channelId) r = followYoutubeChannel(channelId)
return redirect(request.referrer) return redirect(request.referrer)
def followYoutubeChannel(channelId): def followYoutubeChannel(channelId):
channelData = YoutubeSearch.channelInfo(channelId, False)
try: try:
if not current_user.is_following_yt(channelId): channelData = YoutubeSearch.channelInfo(channelId, False)
follow = youtubeFollow() try:
follow.channelId = channelId if not current_user.is_following_yt(channelId):
follow.channelName = channelData[0]['name'] follow = youtubeFollow()
follow.followers.append(current_user) follow.channelId = channelId
db.session.add(follow) follow.channelName = channelData[0]['name']
db.session.commit() follow.followers.append(current_user)
flash("{} followed!".format(channelData[0]['name'])) db.session.add(follow)
return True db.session.commit()
else: flash("{} followed!".format(channelData[0]['name']))
return True
else:
return False
except Exception as e:
print(e)
flash("Youtube: Couldn't follow {}. Already followed?".format(channelData[0]['name']))
return False return False
except Exception as e: except KeyError as ke:
print(e) print("KeyError: {}:'{}' could not be found".format(ke, channelId))
flash("Youtube: Couldn't follow {}. Already followed?".format(channelData[0]['name'])) flash("Youtube: ChannelId '{}' is not valid".format(channelId))
return False return False
@app.route('/ytunfollow/<channelId>', methods=['POST']) @app.route('/ytunfollow/<channelId>', methods=['POST'])
@login_required @login_required
def ytunfollow(channelId): def ytunfollow(channelId):
@ -571,16 +576,12 @@ def importdata():
if file.filename == '': if file.filename == '':
flash('No selected file') flash('No selected file')
return redirect(request.referrer) return redirect(request.referrer)
if file and allowed_file(file.filename) or 'subscription_manager' in file.filename: else:
option = request.form['import_format'] option = request.form['import_format']
if option == 'yotter': if option == 'yotter':
importYotterSubscriptions(file) importYotterSubscriptions(file)
elif option == 'newpipe':
importNewPipeSubscriptions(file)
elif option == 'youtube': elif option == 'youtube':
importYoutubeSubscriptions(file) importYoutubeSubscriptions(file)
elif option == 'freetube':
importFreeTubeSubscriptions(file)
return redirect(request.referrer) return redirect(request.referrer)
return redirect(request.referrer) return redirect(request.referrer)
@ -594,37 +595,25 @@ def deleteme():
logout_user() logout_user()
return redirect(url_for('index')) return redirect(url_for('index'))
def importYoutubeSubscriptions(file):
filename = secure_filename(file.filename)
try:
data = re.findall('(UC[a-zA-Z0-9_-]{22})|(?<=user/)[a-zA-Z0-9_-]+', file.read().decode('utf-8'))
for acc in data:
r = followYoutubeChannel(acc)
except Exception as e:
print(e)
flash("File is not valid.")
def importYotterSubscriptions(file): def importYotterSubscriptions(file):
filename = secure_filename(file.filename) filename = secure_filename(file.filename)
data = json.load(file) data = json.load(file)
for acc in data['twitter']: for acc in data['twitter']:
r = followTwitterAccount(acc['username']) r = followTwitterAccount(acc['username'])
for acc in data['youtube']: for acc in data['youtube']:
r = followYoutubeChannel(acc['channelId']) r = followYoutubeChannel(acc['channelId'])
def importNewPipeSubscriptions(file):
filename = secure_filename(file.filename)
data = json.load(file)
for acc in data['subscriptions']:
r = followYoutubeChannel(re.search('(UC[a-zA-Z0-9_-]{22})|(?<=user\/)[a-zA-Z0-9_-]+', acc['url']).group())
def importYoutubeSubscriptions(file):
filename = secure_filename(file.filename)
itemlist = minidom.parse(file).getElementsByTagName('outline')
for item in itemlist[1:]:
r = followYoutubeChannel(re.search('UC[a-zA-Z0-9_-]{22}', item.attributes['xmlUrl'].value).group())
def importFreeTubeSubscriptions(file):
filename = secure_filename(file.filename)
data = re.findall('UC[a-zA-Z0-9_-]{22}', file.read().decode('utf-8'))
for acc in data:
r = followYoutubeChannel(acc)
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/register', methods=['GET', 'POST']) @app.route('/register', methods=['GET', 'POST'])
def register(): def register():
form = RegistrationForm() form = RegistrationForm()

View File

@ -31,7 +31,7 @@
<div class="ui blue segment"> <div class="ui blue segment">
<i class="large upload middle aligned icon"></i> <i class="large upload middle aligned icon"></i>
<div class="content"> <div class="content">
<div class="description"><h5 class="ui header">Import suscription data</h5></div> <div class="description"><h5 class="ui header">Import subscription data</h5></div>
<form action = "{{ url_for('importdata') }}" method = "POST" enctype = "multipart/form-data"> <form action = "{{ url_for('importdata') }}" method = "POST" enctype = "multipart/form-data">
<input type = "file" name = "file"/> <input type = "file" name = "file"/>
<input type = "submit"/> <input type = "submit"/>
@ -39,15 +39,9 @@
<label class="radio-inline"> <label class="radio-inline">
<input type="radio" name="import_format" id="yotter" value="yotter" checked> Yotter <input type="radio" name="import_format" id="yotter" value="yotter" checked> Yotter
</label> </label>
<label class="radio-inline"> <label class="radio-inline" data-tooltip="Includes FreeTube, Invidious and NewPipe.">
<input type="radio" name="import_format" id="newpipe" value="newpipe"> NewPipe
</label>
<label class="radio-inline">
<input type="radio" name="import_format" id="youtube" value="youtube"> Youtube <input type="radio" name="import_format" id="youtube" value="youtube"> Youtube
</label> </label>
<label class="radio-inline">
<input type="radio" name="import_format" id="freetube" value="freetube"> FreeTube
</label>
</form> </form>
</div> </div>
</div> </div>