Numerize twitter followers

This commit is contained in:
pluja 2020-08-26 23:13:54 +02:00
parent 71b3bb6f0c
commit 1c5c09715f
2 changed files with 10 additions and 29 deletions

View File

@ -9,6 +9,7 @@ from werkzeug.utils import secure_filename
from youtube_search import YoutubeSearch from youtube_search import YoutubeSearch
from werkzeug.urls import url_parse from werkzeug.urls import url_parse
from youtube_dl import YoutubeDL from youtube_dl import YoutubeDL
from numerize import numerize
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from app import app, db from app import app, db
import random, string import random, string
@ -233,11 +234,12 @@ def ytsearch():
}) })
for c in chnns: for c in chnns:
print(c)
channels.append({ channels.append({
'username':c['name'], 'username':c['name'],
'channelId':c['id'], 'channelId':c['id'],
'thumbnail':'https:{}'.format(c['thumbnails'][0]), 'thumbnail':'https:{}'.format(c['thumbnails'][0]),
'subCount':letterify(c['suscriberCountText']) 'subCount':c['suscriberCountText'].split(" ")[0]
}) })
return render_template('ytsearch.html', form=form, btform=button_form, channels=channels, videos=videos) return render_template('ytsearch.html', form=form, btform=button_form, channels=channels, videos=videos)
@ -285,7 +287,6 @@ def unfollowYoutubeChannel(channelId):
db.session.commit() db.session.commit()
flash("{} unfollowed!".format(channel.channelName)) flash("{} unfollowed!".format(channel.channelName))
except: except:
print("Exception")
flash("There was an error unfollowing the user. Try again.") flash("There was an error unfollowing the user. Try again.")
return redirect(request.referrer) return redirect(request.referrer)
@ -404,7 +405,6 @@ def exportData():
@login_required @login_required
def importdata(): def importdata():
if request.method == 'POST': if request.method == 'POST':
print("Post request recieved")
# check if the post request has the file part # check if the post request has the file part
if 'file' not in request.files: if 'file' not in request.files:
flash('No file part') flash('No file part')
@ -456,8 +456,7 @@ def error(errno):
return render_template('{}.html'.format(str(errno))) return render_template('{}.html'.format(str(errno)))
def getTimeDiff(t): def getTimeDiff(t):
tweetTime = datetime.datetime(*t[:6]) diff = datetime.datetime.now() - datetime.datetime(*t[:6])
diff = datetime.datetime.now() - tweetTime
if diff.days == 0: if diff.days == 0:
if diff.seconds > 3599: if diff.seconds > 3599:
@ -484,12 +483,12 @@ def getTwitterUserInfo(username):
else: else:
html = html.body.find('div', attrs={'class':'profile-card'}) html = html.body.find('div', attrs={'class':'profile-card'})
user = { user = {
"profileFullName":html.find('a', attrs={'class':'profile-card-fullname'}).string, "profileFullName":html.find('a', attrs={'class':'profile-card-fullname'}).string.encode('latin1').decode('unicode_escape').encode('latin1').decode('utf8'),
"profileUsername":html.find('a', attrs={'class':'profile-card-username'}).string, "profileUsername":html.find('a', attrs={'class':'profile-card-username'}).string.encode('latin1').decode('unicode_escape').encode('latin1').decode('utf8'),
"profileBio":html.find('div', attrs={'class':'profile-bio'}).get_text().encode('latin1').decode('unicode_escape').encode('latin1').decode('utf8'), "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, "tweets":html.find_all('span', attrs={'class':'profile-stat-num'})[0].string,
"following":html.find_all('span', attrs={'class':'profile-stat-num'})[1].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, "followers":numerize.numerize(int(html.find_all('span', attrs={'class':'profile-stat-num'})[2].string.replace(",",""))),
"likes":html.find_all('span', attrs={'class':'profile-stat-num'})[3].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:]) "profilePic":"{instance}{pic}".format(instance=nitterInstance, pic=html.find('a', attrs={'class':'profile-card-avatar'})['href'][1:])
} }
@ -602,22 +601,3 @@ def getYoutubePosts(ids):
video.description = re.sub(r'^https?:\/\/.*[\r\n]*', '', video.description[0:120]+"...", flags=re.MULTILINE) video.description = re.sub(r'^https?:\/\/.*[\r\n]*', '', video.description[0:120]+"...", flags=re.MULTILINE)
videos.append(video) videos.append(video)
return videos return videos
def letterify(number):
order = len(str(number))
if order == 4:
subCount = "{k}.{c}k".format(k=str(number)[0:1], c=str(number)[1:2])
elif order == 5:
subCount = "{k}.{c}k".format(k=str(number)[0:2], c=str(number)[2:3])
elif order == 6:
subCount = "{k}.{c}k".format(k=str(number)[0:3], c=str(number)[3:4])
elif order == 7:
subCount = "~{M}M".format(M=str(number)[0:1])
elif order == 8:
subCount = "~{M}M".format(M=str(number)[0:2])
elif order >= 8:
subCount = "{M}M".format(M=str(number)[0:3])
else:
subCount = str(number)
return subCount

View File

@ -27,6 +27,7 @@ lxml==4.5.2
Mako==1.1.3 Mako==1.1.3
MarkupSafe==1.1.1 MarkupSafe==1.1.1
multidict==4.7.6 multidict==4.7.6
numerize==0.12
numpy==1.19.0 numpy==1.19.0
packaging==20.4 packaging==20.4
PyMySQL==0.9.3 PyMySQL==0.9.3