Skip to content

Commit

Permalink
[youtube] Fix exception handling
Browse files Browse the repository at this point in the history
Since we moved to JSON, we need to catch KeyError, not AtrributeError
  • Loading branch information
Elad Alfassa committed Jun 9, 2013
1 parent 6980112 commit b0684fa
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ def ytget(willie, trigger, uri):
vid_id = video_entry['id']['$t'].split(':')
vid_id = vid_id[len(vid_id)-1] #last item is the actual ID
vid_info['link'] = 'http://youtu.be/' + vid_id
except AttributeError as e:
except KeyError as e:
vid_info['link'] = 'N/A'

try:
vid_info['title'] = video_entry['title']['$t']
except AttributeError as e:
except KeyError as e:
vid_info['title'] = 'N/A'

#get youtube channel
try:
vid_info['uploader'] = video_entry['author'][0]['name']['$t']
except AttributeError as e:
except KeyError as e:
vid_info['uploader'] = 'N/A'

#get upload time in format: yyyy-MM-ddThh:mm:ss.sssZ
Expand All @@ -66,7 +66,7 @@ def ytget(willie, trigger, uri):
vid_info['uploaded'] = '%s/%s/%s, %s:%s' % (upraw[8:10], upraw[5:7],
upraw[0:4], upraw[11:13],
upraw[14:16])
except AttributeError as e:
except KeyError as e:
vid_info['uploaded'] = 'N/A'

#get duration in seconds
Expand All @@ -90,33 +90,33 @@ def ytget(willie, trigger, uri):
vid_info['length'] = vid_info['length'] + ' '
if seconds:
vid_info['length'] = vid_info['length'] + str(seconds) + 'secs'
except AttributeError as e:
except KeyError as e:
vid_info['length'] = 'N/A'

#get views
try:
views = video_entry['yt$statistics']['viewCount']
vid_info['views'] = str('{0:20,d}'.format(int(views))).lstrip(' ')
except AttributeError as e:
except KeyError as e:
vid_info['views'] = 'N/A'

#get comment count
try:
comments = video_entry['gd$comments']['gd$feedLink']['countHint']
vid_info['comments'] = str('{0:20,d}'.format(int(comments))).lstrip(' ')
except AttributeError as e:
except KeyError as e:
vid_info['comments'] = 'N/A'

#get likes & dislikes
try:
likes = video_entry['yt$rating']['numLikes']
vid_info['likes'] = str('{0:20,d}'.format(int(likes))).lstrip(' ')
except AttributeError as e:
except KeyError as e:
vid_info['likes'] = 'N/A'
try:
dislikes = video_entry['yt$rating']['numDislikes']
vid_info['dislikes'] = str('{0:20,d}'.format(int(dislikes))).lstrip(' ')
except AttributeError as e:
except KeyError as e:
vid_info['dislikes'] = 'N/A'
return vid_info

Expand Down

0 comments on commit b0684fa

Please sign in to comment.