def get_all_categories(api_url): """Given an api_url, retrieves all categories :arg api_url: URL for the api. :returns: list of dicts each belonging to a category :raises steve.restapi.Http5xxException: if there's a server error Example:: from steve.util import get_all_categories cats = get_all_categories('http://pyvideo.org/api/v1/') print [cat['title'] for cat in cats] # Prints something like: # [u'PyCon 2012', u'PyCon 2011', etc.] """ api = restapi.API(api_url) # Build a dict of cat title -> cat data. all_categories = restapi.get_content(api.category.get(limit=0)) return all_categories['objects']
def get_all_categories(api_url): """Given an api_url, retrieves all categories :arg api_url: URL for the api. :returns: list of dicts each belonging to a category :raises steve.restapi.Http5xxException: if there's a server error Example:: from steve.util import get_all_categories cats = get_all_categories('http://pyvideo.org/api/v1/') print [cat['title'] for cat in cats] # Prints something like: # [u'PyCon 2012', u'PyCon 2011', etc.] """ api = restapi.API(api_url) # Build a dict of cat title -> cat data resp = restapi.get_content(api.category.get(limit=0)) return resp['results']
def get_all_categories(api_url): """Given an api_url, retrieves all categories :arg api_url: URL for the api. :returns: list of dicts each belonging to a category :raises steve.restapi.Http5xxException: if there's a server error Example:: from steve.util import get_all_categories cats = get_all_categories('http://pyvideo.org/api/v1/') print [cat['title'] for cat in cats] # Prints something like: # [u'PyCon 2012', u'PyCon 2011', etc.] """ api = restapi.API(api_url) # Build a dict of cat title -> cat data resp = restapi.get_content(api.category.get()) cats = resp['results'] # If there are more than 50 categories, then the results are paged. So we # hit each "next' page until we have all the results. while resp['next'] is not None: parts = urlparse.urlparse(resp['next']) qs = urlparse.parse_qs(parts.query) resp = restapi.get_content(api.category.get(page=qs['page'])) cats.extend(resp['results']) return cats
def get_video(api_url, auth_token, video_id): """Gets information for specified video :arg api_url: URL for the api :arg auth_token: auth token :arg video_id: The id for the video :returns: video data :raises steve.richardapi.DoesNotExist: if the video doesn't exist """ api = restapi.API(api_url) return restapi.get_content(api.video(video_id).get(auth_token=auth_token))
def up_richard(self, ep): host = pw.richard[ep.show.client.richard_id] endpoint = "http://{hostname}/api/v1".format(hostname=host["host"]) api = API(endpoint) vid = ep.public_url.split("/video/")[1].split("/")[0] response = api.video(vid).get(username=host["user"], api_key=host["api_key"]) video_data = get_content(response) video_data["state"] = 1 try: update_video(endpoint, host["user"], host["api_key"], vid, video_data) except MissingRequiredData, e: # this shouldn't happen, prolly debugging something. import code code.interact(local=locals())
def update_pyvideo(self, vid, new_data): """ updates a pyvideo record :arg vid: video id for pyvideo :arg new_data: dict of fields to update :returns: a dict from the updated video """ try: # fetch current record response = self.api.video(vid).get(username=self.host['user'], api_key=self.host['api_key']) video_data = get_content(response) if self.options.verbose: pprint.pprint( video_data ) # update dict with new information video_data.update(new_data) if self.options.verbose: pprint.pprint( video_data ) # update in pyvideo return update_video(self.pyvideo_endpoint, self.host['user'], self.host['api_key'], vid, video_data) except MissingRequiredData as e: print 'Missing required fields', e.errors raise e
def up_richard(self, ep): host = pw.richard[ep.show.client.richard_id] endpoint = 'http://{hostname}/api/v1'.format(hostname=host['host']) api = API(endpoint) # vid = ep.public_url.split('/video/')[1].split('/')[0] vid = get_video_id(ep.public_url) response = api.video(vid).get( username=host['user'], api_key=host['api_key']) video_data = get_content(response) video_data['state'] = 1 try: update_video(endpoint, host['user'], host['api_key'], vid, video_data) except MissingRequiredData, e: # this shouldn't happen, prolly debugging something. import code code.interact(local=locals())
def update_video(api_url, auth_token, video_id, video_data): """Updates an existing video on the site This updates an existing video on the site using HTTP PUT. It returns the final video data. .. Warning:: This stomps on the data that's currently there. If you have the video_id wrong, then this will overwrite the current data. Be very careful about updating existing video data. Best to get it, make sure the id is correct (check the title? the slug?), and then update it. :arg api_url: URL for the api :arg auth_token: auth token :arg video_id: The id for the video :arg video_data: Python dict holding all the data for this video :returns: the updated video data :raises steve.restapi.Http4xxException: if the video doesn't exist on the server :raises steve.restapi.Http5xxException: if there's a server error :raises steve.richardapi.MissingRequiredData: if the video_data is missing keys that are required Example:: import datetime from steve.util import STATE_LIVE, update_video, MissingRequiredData try: video = update_video( 'http://pyvideo.org/api/v1/', auth_token='ou812authkey', video_id=1101, video_data={ 'id': 1101, 'category': 'Test Category', 'state': STATE_LIVE, 'title': 'Test video title', 'speakers': ['Jimmy Discotheque'], 'language': 'English', 'added': datetime.datetime.now().isoformat() }) # Prints the video data. print video except MissingRequiredData as exc: # Prints the errors print exc .. Note:: Check the richard project in the video app at ``models.py`` for up-to-date list of fields and their types. https://github.com/pyvideo/richard/blob/master/richard/videos/models.py """ # If you do a create_video, then update that data and do an # update_video, the data has a few fields in it that shouldn't be # there. We nix those here. video_data.pop('resource_uri', None) video_data.pop('added', None) errors = verify_video_data(video_data) if errors: raise MissingRequiredData('video data has errors: {0}'.format( repr(errors))) api = restapi.API(api_url) # Try to get the video on the site. This will kick up a 404 # if it doesn't exist. api.video(video_id).get(auth_token=auth_token) # Everything is probably fine, so try to update the data. return restapi.get_content( api.video(video_id).put(data=video_data, auth_token=auth_token))
def create_video(api_url, auth_token, video_data): """Creates a video on the site This creates a video on the site using HTTP POST. It returns the video data it posted which also contains the id. .. Note:: This doesn't yet check to see if the video already exists. :arg api_url: URL for the api :arg auth_token: auth token :arg video_data: Python dict holding the values to create this video :returns: the video data :raises steve.restapi.Http5xxException: if there's a server error :raises steve.richardapi.MissingRequiredData: if the video_data is missing keys that are required Example:: import datetime from steve.util import STATE_LIVE, create_video, MissingRequiredData try: video = create_video( 'http://pyvideo.org/api/v1/', auth_token='ou812authkey', video_data={ 'category': 'Test Category', 'state': STATE_LIVE, 'title': 'Test video title', 'speakers': ['Jimmy Discotheque'], 'language': 'English', 'added': datetime.datetime.now().isoformat() }) # Prints the video data. print video except MissingRequiredData as exc: # Prints the errors print exc .. Note:: Check the richard project in the video app at ``models.py`` for up-to-date list of fields and their types. https://github.com/pyvideo/richard/blob/master/richard/videos/models.py """ errors = verify_video_data(video_data) if errors: raise MissingRequiredData('video data has errors: {0}'.format( repr(errors))) # TODO: Check to see if the video exists already. Probably # want to use (category, title) as a key. api = restapi.API(api_url) return restapi.get_content( api.video.post(data=video_data, auth_token=auth_token))
def create_category_if_missing(api_url, username, auth_key, category_data): """Creates a category on the site if it doesn't already exist This checks to see if the category is already on the site by checking titles and slugs. If the category does not exist, it creates it and returns the new category data. If the category does exist, then it just returns the category. :arg api_url: URL for the api :arg username: username :arg auth_key: auth key for that username for that API URL :arg category_data: Python dict holding the values to create this category :returns: the category data :raises steve.restapi.Http5xxException: if there's a server error :raises steve.richardapi.MissingRequiredData: if the category_data is missing keys that are required Example:: from steve.util import create_category_if_not_exists cat = create_category_if_not_exists( 'http://pyvideo.org/api/v1/', 'carl', 'ou812authkey', {'title': 'Test Category 2013'}) print cat # Prints something like: # {u'description': u'', u'videos': [], # u'title': u'Test Category 2013', u'url': u'', # u'whiteboard': u'', u'start_date': None, # u'id': u'114', u'slug': u'test-category-2013', # u'resource_uri': u'/api/v1/category/114/'} .. Note:: Check the richard project in the video app at ``models.py`` for up-to-date list of fields and their types. https://github.com/willkg/richard/blob/master/richard/videos/models.py """ if not 'title' in category_data or not category_data['title']: raise MissingRequiredData( 'category data has errors', errors=['missing "title"']) try: cat = get_category(api_url, category_data['title']) return cat except DoesNotExist: pass api = restapi.API(api_url) return restapi.get_content(api.category.post(category_data, username=username, api_key=auth_key))
def update_video(api_url, username, auth_key, video_id, video_data): """Updates an existing video on the site This updates an existing video on the site using HTTP PUT. It returns the final video data. .. Warning:: This stomps on the data that's currently there. If you have the video_id wrong, then this will overwrite the current data. Be very careful about updating existing video data. Best to get it, make sure the id is correct (check the title? the slug?), and then update it. :arg api_url: URL for the api :arg username: username :arg auth_key: auth key for that username for that API URL :arg video_id: The id for the video :arg video_data: Python dict holding all the data for this video :returns: the updated video data :raises steve.restapi.Http4xxException: if the video doesn't exist on the server :raises steve.restapi.Http5xxException: if there's a server error :raises steve.richardapi.MissingRequiredData: if the video_data is missing keys that are required---check the ``errors`` property of the exception for a list of errors Example:: import datetime from steve.util import update_video, MissingRequiredData try: video = update_video( 'http://pyvideo.org/api/v1/', 'carl', 'ou812authkey', 1101, { 'id': 1101, 'category': 'Test Category', 'state': 1, 'title': 'Test video title', 'speakers': ['Jimmy Discotheque'], 'language': 'English', 'added': datetime.datetime.now().isoformat() }) # Prints the video data. print video except MissingRequiredData as exc: # Prints the errors print exc.errors .. Note:: Check the richard project in the video app at ``models.py`` for up-to-date list of fields and their types. https://github.com/willkg/richard/blob/master/richard/videos/models.py """ # If you do a create_video, then update that data and do an # update_video, the data has 'resource_uri' in it. We want to nix # that if it's there. video_data.pop('resource_uri', None) errors = verify_video_data(video_data) if errors: raise MissingRequiredData( 'video data has errors', errors=errors) api = restapi.API(api_url) # Try to get the video on the site. This will kick up a 404 # if it doesn't exist. api.video(video_id).get(username=username, api_key=auth_key) # Everything is probably fine, so try to update the data. return restapi.get_content( api.video(video_id).put(data=video_data, username=username, api_key=auth_key))
def create_video(api_url, username, auth_key, video_data): """Creates a video on the site This creates a video on the site using HTTP POST. It returns the video data it posted which also contains the id. .. Note:: This doesn't yet check to see if the video already exists. :arg api_url: URL for the api :arg username: username :arg auth_key: auth key for that username for that API URL :arg video_data: Python dict holding the values to create this video :returns: the video data :raises steve.restapi.Http5xxException: if there's a server error :raises steve.richardapi.MissingRequiredData: if the video_data is missing keys that are required---check the ``errors`` property of the exception for a list of errors Example:: import datetime from steve.util import create_video, MissingRequiredData try: video = create_video( 'http://pyvideo.org/api/v1/', 'carl', 'ou812authkey', { 'category': 'Test Category', 'state': 1, 'title': 'Test video title', 'speakers': ['Jimmy Discotheque'], 'language': 'English', 'added': datetime.datetime.now().isoformat() }) # Prints the video data. print video except MissingRequiredData as exc: # Prints the errors print exc.errors .. Note:: Check the richard project in the video app at ``models.py`` for up-to-date list of fields and their types. https://github.com/willkg/richard/blob/master/richard/videos/models.py """ errors = verify_video_data(video_data) if errors: raise MissingRequiredData( 'video data has errors', errors=errors) # TODO: Check to see if the video exists already. Probably # want to use (category, title) as a key. api = restapi.API(api_url) return restapi.get_content( api.video.post(data=video_data, username=username, api_key=auth_key))
def create_category_if_missing(api_url, username, auth_key, category_data): """Creates a category on the site if it doesn't already exist This checks to see if the category is already on the site by checking titles and slugs. If the category does not exist, it creates it and returns the new category data. If the category does exist, then it just returns the category. :arg api_url: URL for the api :arg username: username :arg auth_key: auth key for that username for that API URL :arg category_data: Python dict holding the values to create this category :returns: the category data :raises steve.restapi.Http5xxException: if there's a server error :raises steve.richardapi.MissingRequiredData: if the category_data is missing keys that are required Example:: from steve.util import create_category_if_not_exists cat = create_category_if_not_exists( 'http://pyvideo.org/api/v1/', 'carl', 'ou812authkey', {'title': 'Test Category 2013'}) print cat # Prints something like: # {u'description': u'', u'videos': [], # u'title': u'Test Category 2013', u'url': u'', # u'whiteboard': u'', u'start_date': None, # u'id': u'114', u'slug': u'test-category-2013', # u'resource_uri': u'/api/v1/category/114/'} .. Note:: Check the richard project in the video app at ``models.py`` for up-to-date list of fields and their types. https://github.com/willkg/richard/blob/master/richard/videos/models.py """ if not 'title' in category_data or not category_data['title']: raise MissingRequiredData('category data has errors', errors=['missing "title"']) try: cat = get_category(api_url, category_data['title']) return cat except DoesNotExist: pass api = restapi.API(api_url) return restapi.get_content( api.category.post(category_data, username=username, api_key=auth_key))