This repository has been archived on 2022-06-28. You can view files and clone it, but cannot push or open issues or pull requests.
Yotter/app/routes.py

418 lines
16 KiB
Python
Raw Normal View History

2020-07-27 18:30:01 +05:30
from flask import render_template, flash, redirect, url_for, request, send_from_directory
from app.forms import LoginForm, RegistrationForm, EmptyForm, SearchForm, ChannelForm
from app.models import User, twitterPost, invidiousPost, Post, invidiousFollow
2020-07-13 03:13:36 +05:30
from flask_login import login_user, logout_user, current_user, login_required
2020-07-14 21:15:29 +05:30
from requests_futures.sessions import FuturesSession
from concurrent.futures import as_completed
2020-07-13 03:13:36 +05:30
from werkzeug.urls import url_parse
2020-07-13 20:06:45 +05:30
from bs4 import BeautifulSoup
2020-07-13 03:13:36 +05:30
from flask import Markup
from app import app, db
import time, datetime
import random, string
import feedparser
2020-07-13 20:06:45 +05:30
import requests
2020-07-27 18:30:01 +05:30
import json
import re
2020-07-13 20:06:45 +05:30
nitterInstance = "https://nitter.net/"
nitterInstanceII = "https://nitter.mastodont.cat"
2020-07-13 03:13:36 +05:30
@app.route('/')
@app.route('/index')
@login_required
def index():
2020-07-27 18:30:01 +05:30
#start_time = time.time()
2020-07-13 03:13:36 +05:30
following = current_user.following_list()
followed = current_user.followed.count()
posts = []
avatarPath = "img/avatars/1.png"
2020-07-13 20:06:45 +05:30
form = EmptyForm()
2020-07-14 21:15:29 +05:30
posts.extend(getFeed(following))
posts.sort(key=lambda x: x.timeStamp, reverse=True)
2020-07-14 21:15:29 +05:30
if not posts:
profilePic = avatarPath
else:
profilePic = posts[0].userProfilePic
2020-07-27 18:30:01 +05:30
#print("--- {} seconds fetching feed---".format(time.time() - start_time))
2020-07-14 21:15:29 +05:30
return render_template('index.html', title='Home', posts=posts, avatar=avatarPath, profilePic = profilePic, followedCount=followed, form=form)
2020-07-13 03:13:36 +05:30
2020-07-27 18:30:01 +05:30
2020-07-13 03:13:36 +05:30
@app.route('/login', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('index'))
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user is None or not user.check_password(form.password.data):
flash('Invalid username or password')
return redirect(url_for('login'))
login_user(user, remember=form.remember_me.data)
next_page = request.args.get('next')
if not next_page or url_parse(next_page).netloc != '':
next_page = url_for('index')
return redirect(next_page)
return render_template('login.html', title='Sign In', form=form)
@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('index'))
2020-07-27 18:30:01 +05:30
@app.route('/settings')
@login_required
def settings():
return render_template('settings.html')
@app.route('/export')
@login_required
#Export data into a JSON file. Later you can import the data.
def export():
a = exportData()
if a:
2020-07-27 21:13:07 +05:30
return send_from_directory('.', 'data_export.json', as_attachment=True)
2020-07-27 18:30:01 +05:30
else:
2020-07-27 21:15:14 +05:30
return redirect(url_for('error/405'))
2020-07-27 18:30:01 +05:30
def exportData():
twitterFollowing = current_user.following_list()
youtubeFollowing = current_user.youtube_following_list()
data = {}
data['twitter'] = []
data['youtube'] = []
for f in twitterFollowing:
data['twitter'].append({
'username': f.username
})
for f in youtubeFollowing:
data['youtube'].append({
'channelId': f.channelId
})
try:
with open('app/data_export.json', 'w') as outfile:
json.dump(data, outfile)
return True
except:
return False
2020-07-13 03:13:36 +05:30
@app.route('/register', methods=['GET', 'POST'])
def register():
if current_user.is_authenticated:
return redirect(url_for('index'))
2020-07-13 20:06:45 +05:30
2020-07-13 03:13:36 +05:30
form = RegistrationForm()
if form.validate_on_submit():
2020-07-13 20:06:45 +05:30
if isTwitterUser(form.username.data):
flash('This is username is taken! Choose a different one.')
else:
2020-07-13 03:13:36 +05:30
user = User(username=form.username.data, email=form.email.data)
user.set_password(form.password.data)
db.session.add(user)
db.session.commit()
flash('Congratulations, you are now a registered user!')
return redirect(url_for('login'))
return render_template('register.html', title='Register', form=form)
2020-07-27 18:30:01 +05:30
@app.route('/invidious', methods=['GET', 'POST'])
@login_required
def invidious():
form = ChannelForm()
if form.validate_on_submit():
channelId = form.channelId.data
if requests.get('https://invidio.us/feed/channel/{}'.format(channelId)).status_code == 200:
follow = invidiousFollow()
follow.channelId = channelId
follow.followers.append(current_user)
try:
db.session.add(follow)
db.session.commit()
flash("Added to list!")
except:
flash("Something went wrong. Try again!")
return redirect(url_for('invidious'))
else:
flash("Enter a valid Channel ID. Eg: UCJWCJCWOxBYSi5DhCieLOLQ")
return redirect(url_for('invidious'))
ids = current_user.youtube_following_list()
videos = getInvidiousPosts(ids)
if videos:
videos.sort(key=lambda x: x.date, reverse=True)
return render_template('invidious.html', videos=videos, form=form)
2020-07-13 20:06:45 +05:30
@app.route('/savePost/<url>', methods=['POST'])
@login_required
def savePost(url):
savedUrl = url.replace('~', '/')
r = requests.get(savedUrl)
html = BeautifulSoup(str(r.content), "lxml")
post = html.body.find_all('div', attrs={'class':'tweet-content'})
newPost = Post()
newPost.url = savedUrl
newPost.body = html.body.find_all('div', attrs={'class':'main-tweet'})[0].find_all('div', attrs={'class':'tweet-content'})[0].text
newPost.username = html.body.find('a','username').text.replace("@","")
newPost.timestamp = html.body.find_all('p', attrs={'class':'tweet-published'})[0].text
newPost.user_id = current_user.id
try:
db.session.add(newPost)
db.session.commit()
except:
flash("Post could not be saved. Either it was already saved or there was an error.")
2020-07-13 20:06:45 +05:30
return redirect(url_for('index'))
@app.route('/saved')
@login_required
def saved():
savedPosts = current_user.saved_posts().all()
return render_template('saved.html', title='Saved', savedPosts=savedPosts)
@app.route('/deleteSaved/<id>', methods=['POST'])
@login_required
def deleteSaved(id):
savedPost = Post.query.filter_by(id=id).first()
db.session.delete(savedPost)
db.session.commit()
return redirect(url_for('saved'))
2020-07-13 03:13:36 +05:30
@app.route('/follow/<username>', methods=['POST'])
@login_required
def follow(username):
form = EmptyForm()
if form.validate_on_submit():
user = User.query.filter_by(username=username).first()
2020-07-13 20:06:45 +05:30
isTwitter = isTwitterUser(username)
2020-07-13 03:13:36 +05:30
if user is None and isTwitter:
2020-07-27 18:30:01 +05:30
x = ''.join(randomrandom.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(16))
2020-07-13 03:13:36 +05:30
newUser = User(username=username, email="{}@person.is".format(x))
db.session.add(newUser)
db.session.commit()
flash('You are now following {}!'.format(username))
#flash('User {} not found.'.format(username))
return redirect(url_for('index'))
if user == current_user:
flash('You cannot follow yourself!')
return redirect(url_for('user', username=username))
current_user.follow(user)
db.session.commit()
flash('You are following {}!'.format(username))
return redirect(url_for('user', username=username))
else:
return redirect(url_for('index'))
@app.route('/unfollow/<username>', methods=['POST'])
@login_required
def unfollow(username):
form = EmptyForm()
if form.validate_on_submit():
user = User.query.filter_by(username=username).first()
if user is None:
flash('User {} not found.'.format(username))
return redirect(url_for('index'))
if user == current_user:
flash('You cannot unfollow yourself!')
return redirect(url_for('user', username=username))
current_user.unfollow(user)
db.session.commit()
2020-07-13 05:40:51 +05:30
flash('You are no longer following {}.'.format(username))
2020-07-13 03:13:36 +05:30
return redirect(url_for('user', username=username))
else:
return redirect(url_for('index'))
2020-07-13 05:40:51 +05:30
@app.route('/unfollowList/<username>', methods=['POST'])
@login_required
def unfollowList(username):
form = EmptyForm()
if form.validate_on_submit():
user = User.query.filter_by(username=username).first()
if user is None:
flash('User {} not found.'.format(username))
return redirect(url_for('index'))
if user == current_user:
flash('You cannot unfollow yourself!')
return redirect(url_for('user', username=username))
current_user.unfollow(user)
db.session.commit()
flash('You are no longer following {}!'.format(username))
return redirect(url_for('following'))
else:
return redirect(url_for('index'))
@app.route('/following')
@login_required
def following():
form = EmptyForm()
following = current_user.following_list()
followed = current_user.followed.count()
return render_template('following.html', accounts = following, count = followed, form = form)
@app.route('/search', methods=['GET', 'POST'])
@login_required
def search():
form = SearchForm()
2020-07-13 20:06:45 +05:30
parsedResults = []
2020-07-13 05:40:51 +05:30
if form.validate_on_submit():
user = form.username.data
2020-07-14 22:30:50 +05:30
r = requests.get("{instance}search?f=users&q={usern}".format(instance=nitterInstance, usern=user.replace(" ", "+")))
html = BeautifulSoup(str(r.content), "lxml")
results = html.body.find_all('a', attrs={'class':'tweet-link'})
if results:
2020-07-13 20:06:45 +05:30
parsedResults = [s['href'].replace("/", "") for s in results]
return render_template('search.html', form = form, results = parsedResults)
2020-07-13 05:40:51 +05:30
else:
2020-07-14 22:30:50 +05:30
flash("User {} not found...".format(user))
2020-07-13 20:06:45 +05:30
return render_template('search.html', form = form, results = parsedResults)
2020-07-13 05:40:51 +05:30
else:
return render_template('search.html', form = form)
@app.route('/error/<errno>')
def error(errno):
return render_template('{}.html'.format(str(errno)))
2020-07-13 05:40:51 +05:30
2020-07-13 03:13:36 +05:30
@app.route('/user/<username>')
@login_required
def user(username):
user = User.query.filter_by(username=username).first()
2020-07-13 20:06:45 +05:30
isTwitter = isTwitterUser(username)
if isTwitter and user is None:
2020-07-13 03:13:36 +05:30
x = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(16))
newUser = User(username=username, email="{}@person.is".format(x))
db.session.add(newUser)
db.session.commit()
2020-07-13 05:40:51 +05:30
elif not isTwitter and user is None:
return redirect( url_for('error', errno="404"))
2020-07-13 05:40:51 +05:30
2020-07-13 03:13:36 +05:30
posts = []
posts.extend(getPosts(username))
2020-07-13 03:13:36 +05:30
form = EmptyForm()
2020-07-13 05:40:51 +05:30
user = User.query.filter_by(username=username).first()
2020-07-14 21:15:29 +05:30
if not posts:
profilePic = avatarPath
else:
profilePic = posts[0].userProfilePic
return render_template('user.html', user=user, posts=posts, profilePic = profilePic, form=form)
2020-07-13 03:13:36 +05:30
def getTimeDiff(t):
tweetTime = datetime.datetime(*t[:6])
2020-07-13 20:06:45 +05:30
diff = datetime.datetime.now() - tweetTime
2020-07-13 03:13:36 +05:30
if diff.days == 0:
2020-07-13 20:06:45 +05:30
if diff.seconds > 3599:
timeString = "{}h".format(int((diff.seconds/60)/60))
2020-07-13 03:13:36 +05:30
else:
2020-07-13 20:06:45 +05:30
timeString = "{}m".format(int(diff.seconds/60))
2020-07-13 03:13:36 +05:30
else:
timeString = "{}d".format(diff.days)
2020-07-13 05:40:51 +05:30
return timeString
def isTwitterUser(username):
2020-07-14 21:15:29 +05:30
request = requests.get('https://nitter.net/{}/rss'.format(username), timeout=5)
2020-07-13 20:06:45 +05:30
if request.status_code == 404:
2020-07-13 05:40:51 +05:30
return False
return True
2020-07-14 21:15:29 +05:30
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]
for future in as_completed(futures):
resp = future.result()
rssFeed=feedparser.parse(resp.content)
if rssFeed.entries != []:
for post in rssFeed.entries:
newPost = twitterPost()
newPost.username = rssFeed.feed.title.split("/")[1].replace(" ", "")
newPost.twitterName = rssFeed.feed.title.split("/")[0]
newPost.date = getTimeDiff(post.published_parsed)
newPost.timeStamp = datetime.datetime(*post.published_parsed[:6])
newPost.op = post.author
try:
newPost.userProfilePic = rssFeed.channel.image.url
except:
newPost.profilePicture = ""
2020-07-19 14:35:50 +05:30
newPost.url = post.link
2020-07-14 21:15:29 +05:30
newPost.content = Markup(post.description)
if "Pinned" in post.title.split(":")[0]:
newPost.isPinned = True
if "RT by" in post.title:
newPost.isRT = True
newPost.profilePic = ""
else:
newPost.isRT = False
try:
newPost.profilePic = rssFeed.channel.image.url
except:
newPost.profilePic = avatarPath
feedPosts.append(newPost)
return feedPosts
def getPosts(account):
avatarPath = "img/avatars/{}.png".format(str(random.randint(1,12)))
posts = []
#Gather profile info.
rssFeed = feedparser.parse('{instance}{user}/rss'.format(instance=nitterInstance, user=account))
#Gather posts
if rssFeed.entries != []:
for post in rssFeed.entries:
newPost = twitterPost()
newPost.username = rssFeed.feed.title.split("/")[1].replace(" ", "")
newPost.twitterName = rssFeed.feed.title.split("/")[0]
newPost.date = getTimeDiff(post.published_parsed)
newPost.timeStamp = datetime.datetime(*post.published_parsed[:6])
newPost.op = post.author
try:
newPost.userProfilePic = rssFeed.channel.image.url
except:
newPost.profilePicture = ""
2020-07-19 14:35:50 +05:30
newPost.url = post.link
newPost.content = Markup(post.description)
if "Pinned" in post.title.split(":")[0]:
newPost.isPinned = True
if "RT by" in post.title:
newPost.isRT = True
newPost.profilePic = ""
else:
newPost.isRT = False
try:
newPost.profilePic = rssFeed.channel.image.url
except:
newPost.profilePic = avatarPath
posts.append(newPost)
2020-07-27 18:30:01 +05:30
return posts
def getInvidiousPosts(ids):
videos = []
with FuturesSession() as session:
futures = [session.get('https://invidio.us/feed/channel/{}'.format(id.channelId)) for id in ids]
for future in as_completed(futures):
resp = future.result()
rssFeed=feedparser.parse(resp.content)
for vid in rssFeed.entries:
video = invidiousPost()
video.date = vid.published_parsed
video.timeStamp = getTimeDiff(vid.published_parsed)
video.channelName = vid.author_detail.name
video.channelUrl = vid.author_detail.href
video.videoUrl = vid.link
video.videoTitle = vid.title
video.videoThumb = vid.media_thumbnail[0]['url']
video.description = vid.summary.split('<p style="word-break:break-word;white-space:pre-wrap">')[1]
video.description = re.sub(r'^https?:\/\/.*[\r\n]*', '', video.description[0:120]+"...", flags=re.MULTILINE)
videos.append(video)
return videos