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 time, datetime
import feedparser import feedparser
import requests import requests
import urllib
import json import json
import re import re
@ -67,7 +68,7 @@ def savePost(url):
newPost = Post() newPost = Post()
newPost.url = savedUrl 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.username = html.body.find('a','username').text.replace("@","")
newPost.timestamp = html.body.find_all('p', attrs={'class':'tweet-published'})[0].text newPost.timestamp = html.body.find_all('p', attrs={'class':'tweet-published'})[0].text
newPost.user_id = current_user.id newPost.user_id = current_user.id
@ -76,7 +77,7 @@ def savePost(url):
db.session.commit() db.session.commit()
except: except:
flash("Post could not be saved. Either it was already saved or there was an error.") 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') @app.route('/saved')
@login_required @login_required
@ -166,20 +167,19 @@ def search():
@app.route('/user/<username>') @app.route('/user/<username>')
@login_required @login_required
def user(username): def user(username):
isTwitter = isTwitterUser(username) form = EmptyForm()
if not isTwitter: avatarPath = "img/avatars/{}.png".format(str(random.randint(1,12)))
user = getTwitterUserInfo(username)
if not user:
flash("This user is not on Twitter.") flash("This user is not on Twitter.")
return redirect( url_for('error', errno="404")) return redirect(request.referrer)
posts = [] posts = []
posts.extend(getPosts(username)) posts.extend(getPosts(username))
form = EmptyForm()
user = User.query.filter_by(username=username).first()
if not posts: if not posts:
profilePic = avatarPath user['profilePic'] = avatarPath
else:
profilePic = posts[0].userProfilePic return render_template('user.html', posts=posts, user=user, form=form)
return render_template('user.html', user=user, posts=posts, profilePic = profilePic, form=form)
######################### #########################
#### Youtube Logic ###### #### Youtube Logic ######
@ -469,11 +469,32 @@ def getTimeDiff(t):
return timeString return timeString
def isTwitterUser(username): def isTwitterUser(username):
request = requests.get('{instance}{user}/rss'.format(instance=nitterInstance, user=username)) response = requests.get('{instance}{user}/rss'.format(instance=nitterInstance, user=username))
if request.status_code == 404: if response.status_code == 404:
return False return False
return True 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): def getFeed(urls):
avatarPath = "img/avatars/{}.png".format(str(random.randint(1,12))) avatarPath = "img/avatars/{}.png".format(str(random.randint(1,12)))
feedPosts = [] feedPosts = []
@ -484,12 +505,16 @@ def getFeed(urls):
rssFeed=feedparser.parse(resp.content) rssFeed=feedparser.parse(resp.content)
if rssFeed.entries != []: if rssFeed.entries != []:
for post in 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 = twitterPost()
newPost.username = rssFeed.feed.title.split("/")[1].replace(" ", "") newPost.username = rssFeed.feed.title.split("/")[1].replace(" ", "")
newPost.twitterName = rssFeed.feed.title.split("/")[0] newPost.twitterName = rssFeed.feed.title.split("/")[0]
newPost.date = getTimeDiff(post.published_parsed) newPost.date = getTimeDiff(post.published_parsed)
newPost.timeStamp = datetime.datetime(*post.published_parsed[:6]) newPost.timeStamp = datetime.datetime(*post.published_parsed[:6])
newPost.op = post.author newPost.op = post.author
try: try:
newPost.userProfilePic = rssFeed.channel.image.url newPost.userProfilePic = rssFeed.channel.image.url
except: except:

View File

@ -6,7 +6,7 @@
<div class="center aligned author"> <div class="center aligned author">
<img class="ui avatar image" src="{{channel.avatar}}"> <img class="ui avatar image" src="{{channel.avatar}}">
</div> </div>
<div style="margin: .1em" class="center aligned header"><a href="">{{channel.name}}</a></div> <div class="center aligned header"><a href="">{{channel.name}}</a></div>
<div class="center aligned description"> <div class="center aligned description">
<div class="statistic"> <div class="statistic">
<div class="value"> <div class="value">

View File

@ -4,30 +4,37 @@
<div class="blue ui centered card"> <div class="blue ui centered card">
<div class="content"> <div class="content">
<div class="center aligned author"> <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>
<div style="margin: .1em" class="center aligned header"><a href="https://nitter.net/{{ user.username }}">{{ posts[0].twitterName }}</a></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="center aligned description">
<a> <div class="statistic">
<i class="users icon"></i> <div class="value">
{{ user.followers.count() }} <i class="users icon"></i>{{user.followers}}
</a> </div>
<div class="label">
Followers
</div> </div>
</div> </div>
<div class="extra content"> </div>
<div class="ui one column centered grid"> </div>
{% if user == current_user %} <div class="center aligned extra content">
<p><a>This is your profile</a></p> {% if not current_user.is_following_tw(user.profileUsername.replace('@','')) %}
{% elif not current_user.is_following_tw(user.username) %}
<p> <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.hidden_tag() }}
{{ form.submit(value='Follow') }} {{ form.submit(value='Follow') }}
</form> </form>
</p> </p>
{% else %} {% else %}
<p> <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.hidden_tag() }}
{{ form.submit(value='Unfollow') }} {{ form.submit(value='Unfollow') }}
</form> </form>
@ -35,19 +42,13 @@
{% endif %} {% endif %}
</div> </div>
</div> </div>
</div>
<div class="ui one column grid" id="card-container">
<div class="text container" id="card-container">
{% if not posts %} {% if not posts %}
{% include '_empty_feed.html' %} {% include '_empty_feed.html' %}
{% else %} {% else %}
{% for post in posts %} {% for post in posts %}
{% if post.isRT %} {% include '_twitter_post.html' %}
{% include '_post.html' %}
{% else %}
{% include '_post_nort.html' %}
{% endif %}
{% endfor %} {% endfor %}
{% endif %} {% endif %}
</div> </div>