From ae2a4e86a9e5dc9da2e6001217cfc92a7fcc6b51 Mon Sep 17 00:00:00 2001 From: pluja Date: Mon, 24 Aug 2020 15:31:56 +0200 Subject: [PATCH] Several upgrades --- app/models.py | 4 +- app/routes.py | 81 +++++++++++++++++++------------------ app/templates/settings.html | 19 +++++++-- 3 files changed, 59 insertions(+), 45 deletions(-) diff --git a/app/models.py b/app/models.py index fc135a7..13b0057 100644 --- a/app/models.py +++ b/app/models.py @@ -132,7 +132,7 @@ class ytPost(): class youtubeFollow(db.Model): __tablename__ = 'channel' id = db.Column(db.Integer, primary_key=True) - channelId = db.Column(db.String(30), nullable=False, unique=True) + channelId = db.Column(db.String(30), nullable=False) channelName = db.Column(db.String(30)) followers = db.relationship('User', secondary=channel_association, @@ -144,7 +144,7 @@ class youtubeFollow(db.Model): class twitterFollow(db.Model): __tablename__ = 'twitterAccount' id = db.Column(db.Integer, primary_key=True) - username = db.Column(db.String(30), unique=True) + username = db.Column(db.String(30), nullable=False) followers = db.relationship('User', secondary=twitter_association, back_populates="twitterFollowed") diff --git a/app/routes.py b/app/routes.py index 559faec..239bbcd 100644 --- a/app/routes.py +++ b/app/routes.py @@ -19,8 +19,8 @@ import json import re # Instances - Format must be instance.tld (No '/' and no 'https://') -nitterInstance = "https://nitter.net/" -nitterInstanceII = "https://nitter.mastodont.cat/" +nitterInstanceII = "https://nitter.net/" +nitterInstance = "https://nitter.mastodont.cat/" ytChannelRss = "https://www.youtube.com/feeds/videos.xml?channel_id=" invidiousInstance = "invidio.us" @@ -92,24 +92,23 @@ def deleteSaved(id): def follow(username): form = EmptyForm() if form.validate_on_submit(): - if twFollow(username): + if followTwitterAccount(username): flash("{} followed!".format(username)) - else: - flash("Something went wrong...") return redirect(request.referrer) -def twFollow(username): +def followTwitterAccount(username): if isTwitterUser(username): - try: - follow = twitterFollow() - follow.username = username - follow.followers.append(current_user) - db.session.add(follow) - db.session.commit() - return True - except: - flash("Couldn't follow {}. Maybe you are already following!".format(username)) - return False + if not current_user.is_following_tw(username): + try: + follow = twitterFollow() + follow.username = username + follow.followers.append(current_user) + db.session.add(follow) + db.session.commit() + return True + except: + flash("Twitter: Couldn't follow {}. Already followed?".format(username)) + return False else: flash("Something went wrong... try again") return False @@ -121,8 +120,6 @@ def unfollow(username): if form.validate_on_submit(): if twUnfollow(username): flash("{} unfollowed!".format(username)) - else: - flash("Something went wrong...") return redirect(request.referrer) def twUnfollow(username): @@ -130,7 +127,6 @@ def twUnfollow(username): user = twitterFollow.query.filter_by(username=username).first() db.session.delete(user) db.session.commit() - flash("{} unfollowed!".format(username)) except: flash("There was an error unfollowing the user. Try again.") return redirect(request.referrer) @@ -252,19 +248,21 @@ def ytfollow(channelId): return redirect(request.referrer) def followYoutubeChannel(channelId): - channel = youtubeFollow.query.filter_by(channelId=channelId).first() channelData = YoutubeSearch.channelInfo(channelId, False) try: - follow = youtubeFollow() - follow.channelId = channelId - follow.channelName = channelData[0]['name'] - follow.followers.append(current_user) - db.session.add(follow) - db.session.commit() - flash("{} followed!".format(channelData[0]['name'])) - return True + if not current_user.is_following_yt(channelId): + follow = youtubeFollow() + follow.channelId = channelId + follow.channelName = channelData[0]['name'] + follow.followers.append(current_user) + db.session.add(follow) + db.session.commit() + flash("{} followed!".format(channelData[0]['name'])) + return True + else: + return False except: - flash("Couldn't follow {}. Maybe you are already following!".format(channelData[0]['name'])) + flash("Youtube: Couldn't follow {}. Already followed?".format(channelData[0]['name'])) return False @app.route('/ytunfollow/', methods=['POST']) @@ -374,7 +372,7 @@ def export(): return redirect(url_for('error/405')) def exportData(): - twitterFollowing = current_user.following_list() + twitterFollowing = current_user.twitter_following_list() youtubeFollowing = current_user.youtube_following_list() data = {} data['twitter'] = [] @@ -413,15 +411,20 @@ def importdata(): flash('No selected file') return redirect(request.url) if file and allowed_file(file.filename): - filename = secure_filename(file.filename) - data = json.load(file) - for acc in data['twitter']: - if twFollow(acc['username']): - print("{} followed!".format(acc['username'])) - else: - print("Something went wrong!") + importAccounts(file) + return render_template('settings.html') + return redirect(request.referrer) +def importAccounts(file): + filename = secure_filename(file.filename) + data = json.load(file) + for acc in data['twitter']: + r = followTwitterAccount(acc['username']) + + for acc in data['youtube']: + r = followYoutubeChannel(acc['channelId']) + def allowed_file(filename): return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS @@ -461,7 +464,7 @@ def getTimeDiff(t): return timeString def isTwitterUser(username): - request = requests.get('https://nitter.net/{}/rss'.format(username), timeout=5) + request = requests.get('{instance}{user}/rss'.format(instance=nitterInstance, user=username)) if request.status_code == 404: return False return True @@ -470,7 +473,7 @@ def getFeed(urls): avatarPath = "img/avatars/{}.png".format(str(random.randint(1,12))) feedPosts = [] with FuturesSession() as session: - futures = [session.get('https://nitter.net/{}/rss'.format(u.username)) for u in urls] + futures = [session.get('{instance}{user}/rss'.format(instance=nitterInstance, user=u.username)) for u in urls] for future in as_completed(futures): resp = future.result() rssFeed=feedparser.parse(resp.content) diff --git a/app/templates/settings.html b/app/templates/settings.html index 4861c5a..70e02b3 100644 --- a/app/templates/settings.html +++ b/app/templates/settings.html @@ -2,7 +2,7 @@ {% block content %}
-
+

@@ -11,8 +11,19 @@

-
-
+ + +
+
+ +
+
+ Imports can take up to 2 minutes. +
+

But you can still use Parasitter.

+
+
+
@@ -30,7 +41,7 @@ -
Import data from JSON file
+
Import from JSON export.