Skip to content

Commit

Permalink
catch error if no posts
Browse files Browse the repository at this point in the history
  • Loading branch information
hpiwowar committed Sep 17, 2014
1 parent 1bae12e commit cb238b8
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions totalimpact/providers/altmetric_com.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,24 +155,27 @@ def _extract_metrics_via_fetch(self, page, status_code=200, id=None):
}
metrics_dict = provider._extract_from_json(page, dict_of_keylists)

if metrics_dict['altmetric_com:posts'] and "twitter" in metrics_dict['altmetric_com:posts']:
twitter_posts = metrics_dict['altmetric_com:posts']["twitter"]
impressions = 0
tweeter_followers = []
for post in twitter_posts:
# print post["author"]
twitter_handle = post["author"]["id_on_source"]
try:
followers = post["author"]["followers"]
tweeter_followers.append([twitter_handle, followers])
except KeyError:
pass
impressions = sum([followers for (handle, followers) in tweeter_followers])
if tweeter_followers:
metrics_dict['altmetric_com:tweeter_followers'] = tweeter_followers
if impressions:
metrics_dict['altmetric_com:impressions'] = impressions
del metrics_dict['altmetric_com:posts']
try:
if metrics_dict['altmetric_com:posts'] and "twitter" in metrics_dict['altmetric_com:posts']:
twitter_posts = metrics_dict['altmetric_com:posts']["twitter"]
impressions = 0
tweeter_followers = []
for post in twitter_posts:
# print post["author"]
twitter_handle = post["author"]["id_on_source"]
try:
followers = post["author"]["followers"]
tweeter_followers.append([twitter_handle, followers])
except KeyError:
pass
impressions = sum([followers for (handle, followers) in tweeter_followers])
if tweeter_followers:
metrics_dict['altmetric_com:tweeter_followers'] = tweeter_followers
if impressions:
metrics_dict['altmetric_com:impressions'] = impressions
del metrics_dict['altmetric_com:posts']
except KeyError:
pass # no posts

return metrics_dict

Expand Down

0 comments on commit cb238b8

Please sign in to comment.