Permalink
Browse files

added way to ensure all required fields supplied on API calls

  • Loading branch information...
1 parent 1ac1e37 commit c738699e81f2763399eda719854f854eb72296de @thepeopleseason committed Nov 14, 2011
Showing with 60 additions and 6 deletions.
  1. +1 −1 pybrightcove/__init__.py
  2. +21 −0 pybrightcove/playlist.py
  3. +38 −5 pybrightcove/video.py
View
@@ -22,7 +22,7 @@
pybrightcove is a simple wrapper around the Brightcove Media API
"""
-VERSION = (1, 3, 7, "cmg", 6) # following PEP 386
+VERSION = (1, 3, 7, "cmg", 7) # following PEP 386
DEV_N = 1
View
@@ -33,6 +33,7 @@
pybrightcove.enums.PlaylistTypeEnum.PLAYS_TOTAL,
pybrightcove.enums.PlaylistTypeEnum.PLAYS_TRAILING_WEEK)
+
class Playlist(object):
"""
The Playlist object is a collection of Videos.
@@ -167,12 +168,29 @@ def delete(self, cascade=False):
self.id = None
@staticmethod
+ def ensure_essential_fields(**kwargs):
+ """
+ Ensures API calls have the appropriate fields for Playlist
+ creation, because the BC API fails to provide essentials on a
+ custom call.
+ """
+ # see Playlist._load for field details
+ essentials = ['id', 'referenceId', 'name', 'shortDescription',
+ 'thumbnailURL', 'videoIds', 'playlistType']
+
+ if kwargs.get('playlist_fields'):
+ kwargs['playlist_fields'].extend(essentials)
+ kwargs['playlist_fields'] = list(set(kwargs['playlist_fields']))
+ return kwargs
+
+ @staticmethod
def find_all(_connection=None, page_size=100, page_number=0,
sort_by=DEFAULT_SORT_BY, sort_order=DEFAULT_SORT_ORDER,
**kwargs):
"""
List all playlists.
"""
+ kwargs = Playlist.ensure_essential_fields(**kwargs)
return pybrightcove.connection.ItemResultSet(
"find_all_playlists",
Playlist, _connection, page_size, page_number, sort_by, sort_order,
@@ -186,6 +204,7 @@ def find_by_ids(ids, _connection=None, page_size=100, page_number=0,
List playlists by specific IDs.
"""
ids = ','.join([str(i) for i in ids])
+ kwargs = Playlist.ensure_essential_fields(**kwargs)
return pybrightcove.connection.ItemResultSet('find_playlists_by_ids',
Playlist, _connection, page_size, page_number, sort_by, sort_order,
playlist_ids=ids, **kwargs)
@@ -198,6 +217,7 @@ def find_by_reference_ids(reference_ids, _connection=None, page_size=100,
List playlists by specific reference_ids.
"""
reference_ids = ','.join([str(i) for i in reference_ids])
+ kwargs = Playlist.ensure_essential_fields(**kwargs)
return pybrightcove.connection.ItemResultSet(
"find_playlists_by_reference_ids", Playlist, _connection, page_size,
page_number, sort_by, sort_order, reference_ids=reference_ids,
@@ -210,6 +230,7 @@ def find_for_player_id(player_id, _connection=None, page_size=100,
"""
List playlists for a for given player id.
"""
+ kwargs = Playlist.ensure_essential_fields(**kwargs)
return pybrightcove.connection.ItemResultSet(
"find_playlists_for_player_id", Playlist, _connection, page_size,
page_number, sort_by, sort_order, player_id=player_id, **kwargs)
View
@@ -41,6 +41,7 @@ def is_ftp_connection(con):
"""
return isinstance(con, connection.FTPConnection)
+
def _convert_tstamp(val):
"""
Safe convert string timestamp into a ``datetime`` object. Returns None if
@@ -468,7 +469,7 @@ def add_asset(self, filename, asset_type, display_name,
"""
m = hashlib.md5()
fp = file(filename, 'rb')
- bits = fp.read(262144) ## 256KB
+ bits = fp.read(262144) # 256KB
while bits:
m.update(bits)
bits = fp.read(262144)
@@ -532,7 +533,29 @@ def delete(self, cascade=False, delete_shares=False):
if self.id:
self.connection.post('delete_video', video_id=self.id,
cascade=cascade, delete_shares=delete_shares)
- self.id = None ## Prevent more activity on this video id
+ self.id = None # Prevent more activity on this video id
+
+ @staticmethod
+ def ensure_essential_fields(**kwargs):
+ """
+ Ensures API calls have the appropriate fields for Video
+ creation, because the BC API fails to provide essentials on a
+ custom call.
+ """
+
+ # see Video._load for all key fields
+ essentials = ["creationDate", "economics", "id",
+ "lastModifiedDate", "length", "linkText",
+ "linkURL", "longDescription", "name",
+ "playsTotal", "playsTrailingWeek",
+ "publishedDate", "referenceId",
+ "shortDescription"]
+
+ if kwargs.get('video_fields'):
+ kwargs['video_fields'].extend(essentials)
+ kwargs['video_fields'] = list(set(kwargs['video_fields']))
+
+ return kwargs
def get_upload_status(self):
"""
@@ -553,7 +576,7 @@ def share(self, accounts):
return self.connection.post('share_video', video_id=self.id,
auto_accept=True, sharee_account_ids=accounts)
else:
- raise VideoStatusNotComplete
+ raise exceptions.VideoStatusNotComplete
def set_image(self, image, filename=None, resize=False):
"""
@@ -571,6 +594,7 @@ def find_related(self, _connection=None, page_size=100, page_number=0,
List all videos that are related to this one.
"""
if self.id:
+ kwargs = Video.ensure_essential_fields(**kwargs)
return connection.ItemResultSet('find_related_videos',
Video, _connection, page_size, page_number, None, None,
video_id=self.id, **kwargs)
@@ -629,14 +653,15 @@ def find_modified(since, filter_list=None, _connection=None, page_size=25,
if filter_list is not None:
filters = filter_list
if isinstance(since, datetime):
- fdate = int(since.strftime("%s")) / 60 ## Minutes since UNIX time
+ fdate = int(since.strftime("%s")) / 60 # Minutes since UNIX time
elif since == 0:
fdate = '0' # This should be a string, otherwise kwargs
# thinks it's not existent
else:
msg = 'The parameter "since" must be a datetime object.'
raise exceptions.PyBrightcoveError(msg)
+ kwargs = Video.ensure_essential_fields(**kwargs)
return connection.ItemResultSet('find_modified_videos',
Video, _connection, page_size, page_number, sort_by, sort_order,
from_date=fdate, filter=filters, **kwargs)
@@ -650,6 +675,8 @@ def find_all(_connection=None, page_size=100, page_number=0,
"""
List all videos.
"""
+
+ kwargs = Video.ensure_essential_fields(**kwargs)
return connection.ItemResultSet('find_all_videos', Video,
_connection, page_size, page_number, sort_by, sort_order, **kwargs)
@@ -679,6 +706,8 @@ def find_by_tags(and_tags=None, or_tags=None, _connection=None,
atags = ','.join([str(t) for t in and_tags])
if or_tags:
otags = ','.join([str(t) for t in or_tags])
+
+ kwargs = Video.ensure_essential_fields(**kwargs)
return connection.ItemResultSet('find_videos_by_tags',
Video, _connection, page_size, page_number, sort_by, sort_order,
and_tags=atags, or_tags=otags, **kwargs)
@@ -691,6 +720,7 @@ def find_by_text(text, _connection=None, page_size=100, page_number=0,
"""
List videos that match the ``text`` in title or description.
"""
+ kwargs = Video.ensure_essential_fields(**kwargs)
return connection.ItemResultSet('find_videos_by_text',
Video, _connection, page_size, page_number, sort_by, sort_order,
text=text, **kwargs)
@@ -703,6 +733,7 @@ def find_by_campaign(campaign_id, _connection=None, page_size=100,
"""
List all videos for a given campaign.
"""
+ kwargs = Video.ensure_essential_fields(**kwargs)
return connection.ItemResultSet(
'find_videos_by_campaign_id', Video, _connection, page_size,
page_number, sort_by, sort_order, campaign_id=campaign_id,
@@ -716,6 +747,7 @@ def find_by_user(user_id, _connection=None, page_size=100, page_number=0,
"""
List all videos uploaded by a certain user.
"""
+ kwargs = Video.ensure_essential_fields(**kwargs)
return connection.ItemResultSet('find_videos_by_user_id',
Video, _connection, page_size, page_number, sort_by, sort_order,
user_id=user_id, **kwargs)
@@ -735,6 +767,7 @@ def find_by_reference_ids(reference_ids, _connection=None, page_size=100,
apicall = 'find_videos_by_reference_ids'
if kwargs.get('unfiltered'):
apicall = 'find_videos_by_reference_ids_unfiltered'
+ kwargs = Video.ensure_essential_fields(**kwargs)
return connection.ItemResultSet(
apicall, Video, _connection, page_size,
page_number, sort_by, sort_order, reference_ids=ids, **kwargs)
@@ -753,7 +786,7 @@ def find_by_ids(ids, _connection=None, page_size=100, page_number=0,
apicall = 'find_videos_by_ids'
if kwargs.get('unfiltered'):
apicall = 'find_videos_by_ids_unfiltered'
+ kwargs = Video.ensure_essential_fields(**kwargs)
return connection.ItemResultSet(apicall,
Video, _connection, page_size, page_number, sort_by, sort_order,
video_ids=ids, **kwargs)
-

1 comment on commit c738699

Nice. That ought to make the code I wrote for the brightcove task considerably less ugly. +1

Please sign in to comment.