Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libturpial/api/models/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(self):
self.location = ''
self.url = ''
self.bio = ''
self.entities = {}
self.following = None
self.followed_by = None
self.follow_request = False
Expand Down
67 changes: 36 additions & 31 deletions libturpial/lib/protocols/twitter/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def get_following(self, only_id=False):
return following

def get_profile(self, user):
rtn = self.http.get('/users/show', {'screen_name': user})
rtn = self.http.get('/users/show', {'screen_name': user, 'include_entities': True})
profile = self.json_to_profile(rtn)
rtn = self.http.get('/statuses/user_timeline',
{'screen_name': user, 'count': 10,
Expand Down Expand Up @@ -461,6 +461,8 @@ def json_to_profile(self, response):
profile.location = response['location']
profile.url = response['url']
profile.bio = response['description']
if 'url' in response['entities']:
profile.entities = self.get_entities(response['entities']['url'])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you just need to do:

profile.entities = self.get_entities(response)

profile.following = response['following']
profile.followers_count = response['followers_count']
profile.friends_count = response['friends_count']
Expand Down Expand Up @@ -548,7 +550,7 @@ def json_to_status(self, response, column_id='', type_=Status.NORMAL):
status.repeated_by = repeated_by
status.datetime = self.get_str_time(post['created_at'])
status.timestamp = self.get_int_time(post['created_at'])
status.entities = self.get_entities(post)
status.entities = self.get_entities(post['entities'])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be changed

status.type_ = type_
status.account_id = self.account_id
status.is_own = (username.lower() == self.uname.lower())
Expand Down Expand Up @@ -608,44 +610,47 @@ def json_to_trend_location(self, response):
location.placetype_name = response['placeType']['name']
return location

def get_entities(self, tweet):
if 'entities' in tweet:
def get_entities(self, raw_entities):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change nothing in this method

if raw_entities:
entities = {
'urls': [],
'hashtags': [],
'mentions': [],
'groups': [],
}
for mention in tweet['entities']['user_mentions']:
text = '@' + mention['screen_name']
entities['mentions'].append(Entity(self.account_id,
mention['screen_name'], text,
text))

for url in tweet['entities']['urls']:
try:
expanded_url = url['expanded_url']
except KeyError:
expanded_url = url['url']

try:
display_url = ''.join(['http://', url['display_url']])
except KeyError:
display_url = url['url']

entities['urls'].append(Entity(self.account_id, expanded_url,
display_url, url['url']))

if 'media' in tweet['entities']:
for url in tweet['entities']['media']:
if 'user_mentions' in raw_entities:
for mention in raw_entities['user_mentions']:
text = '@' + mention['screen_name']
entities['mentions'].append(Entity(self.account_id,
mention['screen_name'], text,
text))

if 'urls' in raw_entities:
for url in raw_entities['urls']:
try:
expanded_url = url['expanded_url']
except KeyError:
expanded_url = url['url']

try:
display_url = ''.join(['http://', url['display_url']])
except KeyError:
display_url = url['url']

entities['urls'].append(Entity(self.account_id, expanded_url,
display_url, url['url']))

if 'media' in raw_entities:
for url in raw_entities['media']:
display_url = ''.join(['http://', url['display_url']])
entities['urls'].append(Entity(self.account_id,
url['media_url'], display_url,
url['url']))

for ht in tweet['entities']['hashtags']:
text = ''.join(['#', ht['text']])
url = "%s%s" % (self.hashtags_url, ht['text'])
entities['hashtags'].append(Entity(self.account_id, url, text,
text))
if 'hashtags' in raw_entities:
for ht in raw_entities['hashtags']:
text = ''.join(['#', ht['text']])
url = "%s%s" % (self.hashtags_url, ht['text'])
entities['hashtags'].append(Entity(self.account_id, url, text,
text))
return entities