Improve Twitter efficiency and UI

This commit is contained in:
pluja 2020-08-26 22:13:51 +02:00
parent 4c0406cb24
commit 022bf2d5ff
3 changed files with 80 additions and 54 deletions

View File

@ -15,6 +15,7 @@ import random, string
import time, datetime
import feedparser
import requests
import urllib
import json
import re
@ -67,7 +68,7 @@ def savePost(url):
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.body = html.body.find_all('div', attrs={'class':'main-tweet'})[0].find_all('div', attrs={'class':'tweet-content'})[0].text.encode('latin1').decode('unicode_escape').encode('latin1').decode('utf8')
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
@ -76,7 +77,7 @@ def savePost(url):
db.session.commit()
except:
flash("Post could not be saved. Either it was already saved or there was an error.")
return redirect(url_for('index'))
return redirect(request.referrer)
@app.route('/saved')
@login_required
@ -166,20 +167,19 @@ def search():
@app.route('/user/<username>')
@login_required
def user(username):
isTwitter = isTwitterUser(username)
if not isTwitter:
form = EmptyForm()
avatarPath = "img/avatars/{}.png".format(str(random.randint(1,12)))
user = getTwitterUserInfo(username)
if not user:
flash("This user is not on Twitter.")
return redirect( url_for('error', errno="404"))
return redirect(request.referrer)
posts = []
posts.extend(getPosts(username))
form = EmptyForm()
user = User.query.filter_by(username=username).first()
if not posts:
profilePic = avatarPath
else:
profilePic = posts[0].userProfilePic
return render_template('user.html', user=user, posts=posts, profilePic = profilePic, form=form)
user['profilePic'] = avatarPath
return render_template('user.html', posts=posts, user=user, form=form)
#########################
#### Youtube Logic ######
@ -469,11 +469,32 @@ def getTimeDiff(t):
return timeString
def isTwitterUser(username):
request = requests.get('{instance}{user}/rss'.format(instance=nitterInstance, user=username))
if request.status_code == 404:
response = requests.get('{instance}{user}/rss'.format(instance=nitterInstance, user=username))
if response.status_code == 404:
return False
return True
def getTwitterUserInfo(username):
response = urllib.request.urlopen('{instance}{user}'.format(instance=nitterInstance, user=username)).read()
#rssFeed = feedparser.parse(response.content)
html = BeautifulSoup(str(response), "lxml")
if html.body.find('div', attrs={'class':'error-panel'}):
return False
else:
html = html.body.find('div', attrs={'class':'profile-card'})
user = {
"profileFullName":html.find('a', attrs={'class':'profile-card-fullname'}).string,
"profileUsername":html.find('a', attrs={'class':'profile-card-username'}).string,
"profileBio":html.find('div', attrs={'class':'profile-bio'}).get_text().encode('latin1').decode('unicode_escape').encode('latin1').decode('utf8'),
"tweets":html.find_all('span', attrs={'class':'profile-stat-num'})[0].string,
"following":html.find_all('span', attrs={'class':'profile-stat-num'})[1].string,
"followers":html.find_all('span', attrs={'class':'profile-stat-num'})[2].string,
"likes":html.find_all('span', attrs={'class':'profile-stat-num'})[3].string,
"profilePic":"{instance}{pic}".format(instance=nitterInstance, pic=html.find('a', attrs={'class':'profile-card-avatar'})['href'][1:])
}
return user
def getFeed(urls):
avatarPath = "img/avatars/{}.png".format(str(random.randint(1,12)))
feedPosts = []
@ -484,12 +505,16 @@ def getFeed(urls):
rssFeed=feedparser.parse(resp.content)
if rssFeed.entries != []:
for post in rssFeed.entries:
time = datetime.datetime.now() - datetime.datetime(*post.published_parsed[:6])
if time.days >= 15:
continue
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:

View File

@ -6,17 +6,17 @@
<div class="center aligned author">
<img class="ui avatar image" src="{{channel.avatar}}">
</div>
<div style="margin: .1em" class="center aligned header"><a href="">{{channel.name}}</a></div>
<div class="center aligned description">
<div class="statistic">
<div class="value">
<i class="users icon"></i>{{channel.subCount}}
</div>
<div class="label">
Followers
</div>
</div>
</div>
<div class="center aligned header"><a href="">{{channel.name}}</a></div>
<div class="center aligned description">
<div class="statistic">
<div class="value">
<i class="users icon"></i>{{channel.subCount}}
</div>
<div class="label">
Followers
</div>
</div>
</div>
</div>
<div class="center aligned extra content">
{% if not current_user.is_following_yt(channel.id) %}

View File

@ -4,51 +4,52 @@
<div class="blue ui centered card">
<div class="content">
<div class="center aligned author">
<img class="ui avatar image" src="{{ posts[0].userProfilePic }}"> {{ twitterAt }}
<img class="ui avatar image" src="{{user.profilePic}}">
</div>
<div class="center aligned header"><a href="https://nitter.net/{{ user.profileUsername.replace('@','') }}">
{%if user.profileFullName%}
{{user.profileFullName}}
{%else%}
{{user.profileUsername}}
{%endif%}
</a></div>
<div class="center aligned description">
<div class="statistic">
<div class="value">
<i class="users icon"></i>{{user.followers}}
</div>
<div class="label">
Followers
</div>
</div>
</div>
<div style="margin: .1em" class="center aligned header"><a href="https://nitter.net/{{ user.username }}">{{ posts[0].twitterName }}</a></div>
<div class="center aligned description">
<a>
<i class="users icon"></i>
{{ user.followers.count() }}
</a>
</div>
</div>
<div class="extra content">
<div class="ui one column centered grid">
{% if user == current_user %}
<p><a>This is your profile</a></p>
{% elif not current_user.is_following_tw(user.username) %}
<div class="center aligned extra content">
{% if not current_user.is_following_tw(user.profileUsername.replace('@','')) %}
<p>
<form action="{{ url_for('follow', username=user.username) }}" method="post">
<form action="{{ url_for('follow', username=user.profileUsername.replace('@','')) }}" method="post">
{{ form.hidden_tag() }}
{{ form.submit(value='Follow') }}
</form>
</p>
{% else %}
<p>
<form action="{{ url_for('unfollow', username=user.username) }}" method="post">
<form action="{{ url_for('unfollow', username=user.profileUsername.replace('@','')) }}" method="post">
{{ form.hidden_tag() }}
{{ form.submit(value='Unfollow') }}
</form>
</p>
{% endif %}
</div>
{% endif %}
</div>
</div>
<div class="ui one column grid" id="card-container">
{% if not posts %}
{% include '_empty_feed.html' %}
{% else %}
{% for post in posts %}
{% if post.isRT %}
{% include '_post.html' %}
{% else %}
{% include '_post_nort.html' %}
{% endif %}
{% endfor %}
{% endif %}
<div class="text container" id="card-container">
{% if not posts %}
{% include '_empty_feed.html' %}
{% else %}
{% for post in posts %}
{% include '_twitter_post.html' %}
{% endfor %}
{% endif %}
</div>
{% endblock %}