Efficiency improvements + Pinned posts.

This commit is contained in:
Wire 2020-07-14 14:24:43 +02:00
parent adf57e8ca9
commit 6892d41c52
8 changed files with 73 additions and 85 deletions

View File

@ -56,14 +56,16 @@ def load_user(id):
class twitterPost():
date = 0
username = 'Error'
twitterName = "Error Name"
isPinned = False
op = 'Error'
isRT = True
urlToPost = ""
validPost = True
content = "El gato siguió a la liebre. Esto es un texto de ejemplo."
profilePic = "url"
twitterName = "Error Name"
timeStamp = "error"
userProfilePic = "1.png"
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)

View File

@ -13,7 +13,6 @@ import requests
nitterInstance = "https://nitter.net/"
nitterInstanceII = "https://nitter.mastodont.cat"
print("[ATTENTION]: New version. You will need to install BeautifulSoup: 'pip install bs4'")
@app.route('/')
@app.route('/index')
@ -24,39 +23,9 @@ def index():
posts = []
avatarPath = "img/avatars/1.png"
form = EmptyForm()
for fwd in following:
avatarPath = "img/avatars/{}.png".format(str(random.randint(1,12)))
#Gather profile info.
rssFeed = feedparser.parse('{instance}{user}/rss'.format(instance=nitterInstance, user=fwd.username))
twitterAt = rssFeed.feed.title.split("/")[1].replace(" ", "")
twitterName = rssFeed.feed.title.split("/")[0]
#Gather posts
if rssFeed.entries != []:
for post in rssFeed.entries:
newPost = twitterPost()
newPost.username = rssFeed.feed.title.split("/")[0]
newPost.date = getTimeDiff(post.published_parsed)
newPost.timeStamp = datetime.datetime(*post.published_parsed[:6])
newPost.op = post.author
newPost.urlToPost = post.link
newPost.content = Markup(post.description)
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)
posts.sort(key=lambda x: x.timeStamp, reverse=True)
return render_template('index.html', title='Home', posts=posts, avatar=avatarPath, followedCount=followed, twitterAt=twitterAt, twitterName=twitterName, form=form)
[posts.extend(getPosts(fwd.username)) for fwd in following]
posts.sort(key=lambda x: x.timeStamp, reverse=True)
return render_template('index.html', title='Home', posts=posts, avatar=avatarPath, followedCount=followed, form=form)
@app.route('/login', methods=['GET', 'POST'])
def login():
@ -188,7 +157,7 @@ def search():
user = form.username.data
if isTwitterUser(user):
r = requests.get("{instance}search?f=users&q={usern}".format(instance=nitterInstance, usern=user))
html = BeautifulSoup(str(r.content), features="lxml")
html = BeautifulSoup(str(r.content), "lxml")
results = html.body.find_all('a', attrs={'class':'tweet-link'})
parsedResults = [s['href'].replace("/", "") for s in results]
@ -221,42 +190,11 @@ def user(username):
elif not isTwitter and user is None:
return redirect(url_for('notfound'))
#Gather profile info.
rssFeed = feedparser.parse('{instance}{user}/rss'.format(instance=nitterInstance,user=username))
try:
profilePicture = rssFeed.channel.image.url
except:
profilePicture = ""
twitterAt = rssFeed.feed.title.split("/")[1].replace(" ", "")
twitterName = rssFeed.feed.title.split("/")[0]
#Gather posts
posts = []
for post in rssFeed.entries:
newPost = twitterPost()
newPost.username = rssFeed.feed.title.split("/")[0]
newPost.date = getTimeDiff(post.published_parsed)
newPost.timeStamp = datetime.datetime(*post.published_parsed[:6])
newPost.op = post.author
newPost.urlToPost = post.link
newPost.content = Markup(post.description)
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
#validPost = True
posts.append(newPost)
posts.extend(getPosts(username))
form = EmptyForm()
user = User.query.filter_by(username=username).first()
return render_template('user.html', user=user, posts=posts, form=form, profilePic=profilePicture, twitterAt=twitterAt, twitterName=twitterName)
return render_template('user.html', user=user, posts=posts, form=form)
def getTimeDiff(t):
tweetTime = datetime.datetime(*t[:6])
@ -273,7 +211,43 @@ def getTimeDiff(t):
def isTwitterUser(username):
request = requests.get('https://nitter.net/{}'.format(username), timeout=1)
print("User {name} is {boo} twitter.".format(name=username, boo=request.status_code == 404))
if request.status_code == 404:
return False
return True
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 = ""
newPost.urlToPost = 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)
return posts

View File

@ -6,10 +6,10 @@
<img class="ui avatar image" src="{{ url_for('static',filename='img/avatars/')}}{{range(1, 12) | random}}.png">
</div>
</div>
<div class="header" id="author"><a href="{{post.op.replace('@','')}}">{{ post.op }}</a></div>
<div class="header" id="author"><a href="user/{{post.op.replace('@','')}}">{{ post.op }}</a></div>
<div class="meta">
<span class="category" id="time"><i class="clock icon"></i> {{post.date}} </span>
<span class="category"><i class="retweet icon"></i> {{twitterAt}} retwitted</span>
<span class="category"><i class="retweet icon"></i> {{post.username}} retwitted</span>
</div>
<div class="description">
<p>{{post.content}}</p>

View File

@ -6,9 +6,12 @@
<img class="ui avatar image" src="{{ post.profilePic }}">
</div>
</div>
<div class="header" id="author"><a href="{{post.op.replace('@','')}}">{{ post.op }}</a></div>
<div class="header" id="author"><a href="user/{{post.op.replace('@','')}}">{{ post.op }}</a></div>
<div class="meta">
<span class="category" id="time"><i class="clock icon"></i> {{post.date}} </span>
{% if post.isPinned %}
<span class="category" id="time"><i class="map pin icon"></i> Pinned </span>
{%endif%}
</div>
<div class="description">
<p>{{post.content}}</p>

View File

@ -28,7 +28,7 @@
{% with messages = get_flashed_messages() %}
{% if messages %}
<div class="ui message">
<div class="ui info message">
<div class="header">
Information
</div>

View File

@ -9,12 +9,15 @@
</div>
<hr>
<div class="ui one column grid" id="card-container">
{% for post in posts %}
{% if post.isRT %}
{% include '_post.html' %}
{% else %}
{% include '_post_nort.html' %}
{% endif %}
{% endfor %}
</div>
{% if posts %}
{% for post in posts %}
{% if post.isRT %}
{% include '_post.html' %}
{% else %}
{% include '_post_nort.html' %}
{% endif %}
{% endfor %}
{% else %}
<div>No posts</div>
{% endif %}
{% endblock %}

View File

@ -4,9 +4,9 @@
<div class="blue ui centered card">
<div class="content">
<div class="center aligned author">
<img class="ui avatar image" src="{{ profilePic }}"> {{ twitterAt }}
<img class="ui avatar image" src="{{ posts[0].userProfilePic }}"> {{ twitterAt }}
</div>
<div style="margin: .1em" class="center aligned header"><a href="https://nitter.net/{{ twitterAt.replace('@','') }}/">{{ twitterName }}</a></div>
<div style="margin: .1em" class="center aligned header"><a href="https://nitter.net/{{ posts[0].op.replace('@','') }}/">{{ posts[0].twitterName }}</a></div>
<div class="center aligned description">
<a>
<i class="users icon"></i>

View File

@ -1,9 +1,11 @@
alembic==1.4.2
attrs==19.3.0
beautifulsoup4==4.9.1
bs4==0.0.1
certifi==2020.6.20
chardet==3.0.4
click==7.1.2
defusedxml==0.6.0
dnspython==1.16.0
email-validator==1.1.1
feedparser==5.2.1
@ -12,11 +14,15 @@ Flask-Login==0.5.0
Flask-Migrate==2.5.3
Flask-SQLAlchemy==2.4.3
Flask-WTF==0.14.3
future==0.18.2
idna==2.10
itsdangerous==1.1.0
Jinja2==2.11.2
llvmlite==0.33.0
lxml==4.5.2
Mako==1.1.3
MarkupSafe==1.1.1
numpy==1.19.0
python-dateutil==2.8.1
python-dotenv==0.14.0
python-editor==1.0.4