def api_metadata_download(collection_id): all_media = media_with_tag(user_mediacloud_key(), collection_id) metadata_items = [] for media_source in all_media: for tag in media_source['media_source_tags']: if is_metadata_tag_set(tag['tag_sets_id']): found = False for dictItem in metadata_items: if dictItem['metadataId'] == tag['tag_sets_id']: temp = dictItem['tagged'] dictItem.update({'tagged': temp + 1}) found = True if not found: metadata_items.append({ 'metadataCoverage': tag['tag_set'], 'metadataId': tag['tag_sets_id'], 'tagged': 1 }) for i in metadata_items: temp = len(all_media) - i['tagged'] i.update({'notTagged': temp}) props = ['metadataCoverage', 'tagged', 'notTagged'] filename = "metadataCoverageForCollection" + collection_id + ".csv" return csv.stream_response(metadata_items, props, filename)
def api_collection_sources_csv(collection_id): user_mc = user_admin_mediacloud_client() # info = user_mc.tag(int(collection_id)) all_media = media_with_tag(user_mediacloud_key(), collection_id) for src in all_media: for tag in src['media_source_tags']: if is_metadata_tag_set(tag['tag_sets_id']): format_metadata_fields(src, tag['tag_sets_id'], tag['tag']) file_prefix = "Collection_Sourcelist_Template_for_" + collection_id + "_" what_type_download = COLLECTIONS_TEMPLATE_PROPS_EDIT return csv.download_media_csv(all_media, file_prefix, what_type_download)
def api_collection_sources_csv(collection_id): user_mc = user_mediacloud_client() collection = user_mc.tag( collection_id) # not cached because props can change often all_media = media_with_tag(user_mediacloud_key(), collection_id) for src in all_media: for tag in src['media_source_tags']: if is_metadata_tag_set(tag['tag_sets_id']): format_metadata_fields(src, tag) file_prefix = "Collection {} ({}) - sources ".format( collection_id, collection['tag']) properties_to_include = COLLECTIONS_TEMPLATE_PROPS_EDIT return csv.download_media_csv(all_media, file_prefix, properties_to_include)
def source_update(media_id): user_mc = user_admin_mediacloud_client() # update the basic info name = request.form['name'] url = request.form['url'] editor_notes = request.form['editor_notes'] if 'editor_notes' in request.form else None # this is optional public_notes = request.form['public_notes'] if 'public_notes' in request.form else None # this is optional monitored = request.form['monitored'] if 'monitored' in request.form else None result = user_mc.mediaUpdate(media_id, {'url': url, 'name': name, 'editor_notes': editor_notes, 'is_monitored': monitored, 'public_notes': public_notes}) # now we need to update the collections separately, because they are tags on the media source source = user_mc.media(media_id) existing_tag_ids = [t['tags_id'] for t in source['media_source_tags'] if (t['tag_sets_id'] in VALID_COLLECTION_TAG_SETS_IDS)] tag_ids_to_add = tag_ids_from_collections_param() tag_ids_to_remove = list(set(existing_tag_ids) - set(tag_ids_to_add)) tags_to_add = [MediaTag(media_id, tags_id=cid, action=TAG_ACTION_ADD) for cid in tag_ids_to_add if cid not in existing_tag_ids] tags_to_remove = [MediaTag(media_id, tags_id=cid, action=TAG_ACTION_REMOVE) for cid in tag_ids_to_remove] tags = tags_to_add + tags_to_remove if len(tags) > 0: # don't make extraneous calls user_mc.tagMedia(tags=tags) # now update the metadata too valid_metadata = [ {'form_key': 'publicationCountry', 'tag_sets_id': TAG_SETS_ID_PUBLICATION_COUNTRY}, {'form_key': 'publicationState', 'tag_sets_id': TAG_SETS_ID_PUBLICATION_STATE}, {'form_key': 'primaryLanguage', 'tag_sets_id': TAG_SETS_ID_PRIMARY_LANGUAGE}, {'form_key': 'countryOfFocus', 'tag_sets_id': TAG_SETS_ID_COUNTRY_OF_FOCUS}, {'form_key': 'mediaType', 'tag_sets_id': TAG_SETS_ID_MEDIA_TYPE} ] for metadata_item in valid_metadata: metadata_tag_id = request.form[metadata_item['form_key']] if metadata_item['form_key'] in request.form else None # this is optional existing_tag_ids = [t for t in source['media_source_tags'] if is_metadata_tag_set(t['tag_sets_id'])] # form field check if metadata_tag_id in [None, '', 'null', 'undefined']: # we want to remove it if there was one there if len(existing_tag_ids) > 0: for remove_if_empty in existing_tag_ids: if metadata_item['tag_sets_id'] == remove_if_empty['tag_sets_id']: tag = MediaTag(media_id, tags_id=remove_if_empty['tags_id'], action=TAG_ACTION_REMOVE) user_mc.tagMedia([tag]) elif metadata_tag_id not in existing_tag_ids: # need to add it and clear out the other tag = MediaTag(media_id, tags_id=metadata_tag_id, action=TAG_ACTION_ADD) user_mc.tagMedia([tag], clear_others=True) # result the success of the media update call - would be better to catch errors in any of these calls... return jsonify(result)
def _stream_media_list_csv(user_mc_key, filename, topics_id, **kwargs): # Helper method to stream a list of media back to the client as a csv. Any args you pass in will be # simply be passed on to a call to topicMediaList. add_metadata = False # off for now because this is SUPER slow all_media = [] more_media = True params = kwargs params[ 'limit'] = 1000 # an arbitrary value to let us page through with big pages try: cols_to_export = TOPICS_TEMPLATE_PROPS if not add_metadata: cols_to_export = cols_to_export[:-4] # remove the metadata cols while more_media: page = apicache.topic_media_list(user_mediacloud_key(), topics_id, **params) media_list = page['media'] user_mc = user_admin_mediacloud_client() if add_metadata: for media_item in media_list: media_info = user_mc.media(media_item['media_id']) for eachItem in media_info['media_source_tags']: if is_metadata_tag_set(eachItem['tag_sets_id']): format_metadata_fields(media_item, eachItem) all_media = all_media + media_list if 'next' in page['link_ids']: params['link_id'] = page['link_ids']['next'] more_media = True else: more_media = False return csv.download_media_csv(all_media, filename, cols_to_export) except Exception as exception: return json.dumps({'error': str(exception)}, separators=(',', ':')), 400
def source_update(media_id): user_mc = user_admin_mediacloud_client() # update the basic info name = request.form['name'] url = request.form['url'] editor_notes = request.form[ 'editor_notes'] if 'editor_notes' in request.form else None # this is optional public_notes = request.form[ 'public_notes'] if 'public_notes' in request.form else None # this is optional monitored = request.form[ 'monitored'] if 'monitored' in request.form else None result = user_mc.mediaUpdate( media_id, { 'url': url, 'name': name, 'editor_notes': editor_notes, 'is_monitored': monitored, 'public_notes': public_notes }) # now we need to update the collections separately, because they are tags on the media source source = user_mc.media(media_id) existing_tag_ids = [ t['tags_id'] for t in source['media_source_tags'] if (t['tag_sets_id'] in VALID_COLLECTION_TAG_SETS_IDS) ] tag_ids_to_add = tag_ids_from_collections_param() tag_ids_to_remove = list(set(existing_tag_ids) - set(tag_ids_to_add)) tags_to_add = [ MediaTag(media_id, tags_id=cid, action=TAG_ACTION_ADD) for cid in tag_ids_to_add if cid not in existing_tag_ids ] tags_to_remove = [ MediaTag(media_id, tags_id=cid, action=TAG_ACTION_REMOVE) for cid in tag_ids_to_remove ] tags = tags_to_add + tags_to_remove if len(tags) > 0: # don't make extraneous calls user_mc.tagMedia(tags=tags) # now update the metadata too valid_metadata = [{ 'form_key': 'publicationCountry', 'tag_sets_id': TAG_SETS_ID_PUBLICATION_COUNTRY }, { 'form_key': 'publicationState', 'tag_sets_id': TAG_SETS_ID_PUBLICATION_STATE }, { 'form_key': 'primaryLanguage', 'tag_sets_id': TAG_SETS_ID_PRIMARY_LANGUAGE }, { 'form_key': 'countryOfFocus', 'tag_sets_id': TAG_SETS_ID_COUNTRY_OF_FOCUS }, { 'form_key': 'mediaType', 'tag_sets_id': TAG_SETS_ID_MEDIA_TYPE }] for metadata_item in valid_metadata: metadata_tag_id = request.form[ metadata_item['form_key']] if metadata_item[ 'form_key'] in request.form else None # this is optional existing_tag_ids = [ t for t in source['media_source_tags'] if is_metadata_tag_set(t['tag_sets_id']) ] # form field check if metadata_tag_id in [None, '', 'null', 'undefined']: # we want to remove it if there was one there if len(existing_tag_ids) > 0: for remove_if_empty in existing_tag_ids: if metadata_item['tag_sets_id'] == remove_if_empty[ 'tag_sets_id']: tag = MediaTag(media_id, tags_id=remove_if_empty['tags_id'], action=TAG_ACTION_REMOVE) user_mc.tagMedia([tag]) elif metadata_tag_id not in existing_tag_ids: # need to add it and clear out the other tag = MediaTag(media_id, tags_id=metadata_tag_id, action=TAG_ACTION_ADD) user_mc.tagMedia([tag], clear_others=True) # result the success of the media update call - would be better to catch errors in any of these calls... return jsonify(result)