Commit 5c81ecd9 authored by Yunus Emre Çatalçam's avatar Yunus Emre Çatalçam Committed by GitHub

Lookup patch (#873)

* favorite and following functions are working again using the user_id that fetched with tweet search function, also removed some of the user agents that returns dynamic site which is an undesired situation

* lookup url updated

* bio, location, url parsing ok

* returning join date empty since its not coming with the new interface

* inf function updated to new interface; name, username and private status fully ok, user_id will be parsed if there is a mention of the account itself on the first page

* stat function ok for tweets, following and followers counts. Favorite and media counts do not exist on the profile page

* verified function updated to new page

* updated User function, media count, likes, background image link are empty since they are not included in the new one
Co-authored-by: default avataryunusemrecatalcam <yunus.catalcam@gmail.com>
parent ee08fccc
...@@ -6,6 +6,8 @@ from .storage import db ...@@ -6,6 +6,8 @@ from .storage import db
import logging as logme import logging as logme
import time
class Twint: class Twint:
def __init__(self, config): def __init__(self, config):
logme.debug(__name__+':Twint:__init__') logme.debug(__name__+':Twint:__init__')
...@@ -348,7 +350,7 @@ def Lookup(config): ...@@ -348,7 +350,7 @@ def Lookup(config):
logme.debug(__name__+':Twint:Lookup:user_id') logme.debug(__name__+':Twint:Lookup:user_id')
config.Username = get_event_loop().run_until_complete(get.Username(config.User_id)) config.Username = get_event_loop().run_until_complete(get.Username(config.User_id))
url = f"https://twitter.com/{config.Username}?lang=en" url = f"https://mobile.twitter.com/{config.Username}?prefetchTimestamp=" + str(int(time.time() * 1000))
get_event_loop().run_until_complete(get.User(url, config, db.Conn(config.Database))) get_event_loop().run_until_complete(get.User(url, config, db.Conn(config.Database)))
if config.Pandas_au: if config.Pandas_au:
......
...@@ -9,7 +9,7 @@ class user: ...@@ -9,7 +9,7 @@ class user:
def inf(ur, _type): def inf(ur, _type):
logme.debug(__name__+':inf') logme.debug(__name__+':inf')
try: try:
group = ur.find("div", "user-actions btn-group not-following ") group = ur.find("div", "profile")
if group == None: if group == None:
group = ur.find("div", "user-actions btn-group not-following") group = ur.find("div", "user-actions btn-group not-following")
if group == None: if group == None:
...@@ -18,14 +18,17 @@ def inf(ur, _type): ...@@ -18,14 +18,17 @@ def inf(ur, _type):
print("Error: " + str(e)) print("Error: " + str(e))
if _type == "id": if _type == "id":
ret = group["data-user-id"] screen_name = group.find("span", "screen-name").text
ret = ur.find("a", {"data-screenname": screen_name})
ret = ret.get('data-mentioned-user-id') if ret is not None else None
ret = "" if ret is None else ret
elif _type == "name": elif _type == "name":
ret = group["data-name"] ret = group.find("div", "fullname").text.split('\n')[0]
elif _type == "username": elif _type == "username":
ret = group["data-screen-name"] ret = group.find("span", "screen-name").text
elif _type == "private": elif _type == "private":
ret = group["data-protected"] ret = group.find("div","protected")
if ret == 'true': if ret:
ret = 1 ret = 1
else: else:
ret = 0 ret = 0
...@@ -36,27 +39,29 @@ def card(ur, _type): ...@@ -36,27 +39,29 @@ def card(ur, _type):
logme.debug(__name__+':card') logme.debug(__name__+':card')
if _type == "bio": if _type == "bio":
try: try:
ret = ur.find("p", "ProfileHeaderCard-bio u-dir").text.replace("\n", " ") ret = ur.find("div", "bio").text.replace("\n", " ").strip()
except: except:
ret = "" ret = ""
elif _type == "location": elif _type == "location":
try: try:
ret = ur.find("span", "ProfileHeaderCard-locationText u-dir").text ret = ur.find("div", "location").text
ret = ret[15:].replace("\n", " ")[:-10]
except: except:
ret = "" ret = ""
elif _type == "url": elif _type == "url":
try: try:
ret = ur.find("span", "ProfileHeaderCard-urlText u-dir").find("a")["title"] ret = ur.find("link")["href"]
except: except:
ret = "" ret = ""
return ret return ret
def join(ur): def join(ur):
logme.debug(__name__+':join') try:
jd = ur.find("span", "ProfileHeaderCard-joinDateText js-tooltip u-dir")["title"] logme.debug(__name__+':join')
return jd.split(" - ") jd = ur.find("span", "ProfileHeaderCard-joinDateText js-tooltip u-dir")["title"]
return jd.split(" - ")
except:
return ["", ""]
def convertToInt(x): def convertToInt(x):
logme.debug(__name__+':contertToInt') logme.debug(__name__+':contertToInt')
...@@ -84,10 +89,13 @@ def convertToInt(x): ...@@ -84,10 +89,13 @@ def convertToInt(x):
def stat(ur, _type): def stat(ur, _type):
logme.debug(__name__+':stat') logme.debug(__name__+':stat')
_class = f"ProfileNav-item ProfileNav-item--{_type}" stats = ur.find('table', 'profile-stats')
stat = ur.find("li", _class) stat_dict = {}
for stat in stats.find_all('td', 'stat'):
statnum, statlabel = stat.text.replace('\n', '').replace(',', '').split(' ')[:2]
stat_dict[statlabel.lower()] = int(statnum.replace(',', ''))
try : try :
return int(stat.find("span", "ProfileNav-value")["data-count"]) return stat_dict[_type]
except AttributeError: except AttributeError:
return 0 return 0
...@@ -102,8 +110,8 @@ def media(ur): ...@@ -102,8 +110,8 @@ def media(ur):
def verified(ur): def verified(ur):
logme.debug(__name__+':verified') logme.debug(__name__+':verified')
try: try:
is_verified = ur.find("span", "ProfileHeaderCard-badges").text is_verified = ur.find("img", {"alt": "Verified Account"})['alt']
if "Verified account" in is_verified: if "Verified Account" in is_verified:
is_verified = 1 is_verified = 1
else: else:
is_verified = 0 is_verified = 0
...@@ -125,13 +133,13 @@ def User(ur): ...@@ -125,13 +133,13 @@ def User(ur):
u.url = card(ur, "url") u.url = card(ur, "url")
u.join_date = join(ur)[1] u.join_date = join(ur)[1]
u.join_time = join(ur)[0] u.join_time = join(ur)[0]
u.tweets = stat(ur, "tweets is-active") u.tweets = stat(ur, "tweets")
u.following = stat(ur, "following") u.following = stat(ur, "following")
u.followers = stat(ur, "followers") u.followers = stat(ur, "followers")
u.likes = stat(ur, "favorites") u.likes = "" # stat(ur, "favorites")
u.media_count = media(ur) u.media_count = "" # media(ur)
u.is_private = inf(ur, "private") u.is_private = inf(ur, "private")
u.is_verified = verified(ur) u.is_verified = verified(ur)
u.avatar = ur.find("img", "ProfileAvatar-image")["src"] u.avatar = ur.find("img", {"alt": u.name})["src"]
u.background_image = ur.find('div',{'class':'ProfileCanopy-headerBg'}).find('img').get('src') #u.background_image = ur.find('div',{'class':'ProfileCanopy-headerBg'}).find('img').get('src')
return u return u
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment