Пример #1
0
def update_youtube_statuses():
    """
    Update the status of recently uploaded YouTube videos if complete
    """
    if not is_youtube_enabled():
        return
    videos_processing = VideoFile.objects.filter(
        Q(status=VideoFileStatus.UPLOADED)
        & Q(destination=DESTINATION_YOUTUBE))
    if videos_processing.count() == 0:
        return
    youtube = YouTubeApi()
    for video_file in videos_processing:
        try:
            with transaction.atomic():
                video_file.destination_status = youtube.video_status(
                    video_file.destination_id)
                if video_file.destination_status == YouTubeStatus.PROCESSED:
                    video_file.status = VideoFileStatus.COMPLETE
                video_file.save()
                drive_file = DriveFile.objects.filter(
                    video=video_file.video).first()
                if drive_file and drive_file.resource:
                    resource = drive_file.resource
                    set_dict_field(
                        resource.metadata,
                        settings.YT_FIELD_ID,
                        video_file.destination_id,
                    )
                    set_dict_field(
                        resource.metadata,
                        settings.YT_FIELD_THUMBNAIL,
                        YT_THUMBNAIL_IMG.format(
                            video_id=video_file.destination_id),
                    )
                    resource.save()
            mail_youtube_upload_success(video_file)
        except IndexError:
            # Video might be a dupe or deleted, mark it as failed and continue to next one.
            video_file.status = VideoFileStatus.FAILED
            video_file.save()
            log.exception(
                "Status of YouTube video not found: youtube_id %s",
                video_file.destination_id,
            )
            mail_youtube_upload_failure(video_file)
        except HttpError as error:
            if API_QUOTA_ERROR_MSG in error.content.decode("utf-8"):
                # Don't raise the error, task will try on next run until daily quota is reset
                break
            log.exception(
                "Error for youtube_id %s: %s",
                video_file.destination_id,
                error.content.decode("utf-8"),
            )
            mail_youtube_upload_failure(video_file)
Пример #2
0
def update_youtube_thumbnail(website_id: str, metadata: Dict, overwrite=False):
    """ Assign a youtube thumbnail url if appropriate to a website's metadata"""
    website = Website.objects.get(uuid=website_id)
    if is_ocw_site(website):
        youtube_id = get_dict_field(metadata, settings.YT_FIELD_ID)
        if youtube_id and (not get_dict_field(
                metadata, settings.YT_FIELD_THUMBNAIL) or overwrite):
            set_dict_field(
                metadata,
                settings.YT_FIELD_THUMBNAIL,
                YT_THUMBNAIL_IMG.format(video_id=youtube_id),
            )
Пример #3
0
    def make_field_change(website_content: WebsiteContent, field: str,
                          new_value: str):
        if field == "markdown":
            website_content.markdown = new_value
            return
        if field.startswith("metadata."):
            metadata_keypath = remove_prefix(field, "metadata.")
            set_dict_field(website_content.metadata, metadata_keypath,
                           new_value)
            return

        raise ValueError(f"Unexpected field value: {field}")
Пример #4
0
def update_content_from_s3_data(website, text_id, content_data, update_field):
    """
    Update the update_field of a single content file for an ocw course from hugo2ocw output

    Args:
        website (Website): The content's website
        text_id (str): Tthe content's text_id
        content_data (dict): Dictionary of content data from s3 bucket
        update_field (str): the field to update

    Returns:
        The WebsiteContent object if it existed, None otherwise.
    """
    is_metadata_field = False

    if update_field and update_field.startswith("metadata."):
        is_metadata_field = True
        update_field = update_field.replace("metadata.", "", 1)

    content_file = WebsiteContent.objects.filter(
        website=website, text_id=text_id
    ).first()

    if not content_file:
        return None

    if is_metadata_field:
        set_dict_field(
            content_file.metadata,
            update_field,
            get_dict_field(content_data.get("metadata", {}), update_field),
        )
        if update_field == "parent_uid" and content_data["parent"]:
            content_file.parent_id = content_data["parent"].id
    elif update_field is not None:
        setattr(
            content_file,
            update_field,
            content_data.get(update_field, ""),
        )
    content_file.save()
    return content_file
Пример #5
0
def update_transcripts_for_video(video_id: int):
    """Update transcripts for a video"""
    video = Video.objects.get(id=video_id)
    if threeplay_api.update_transcripts_for_video(video):
        first_transcript_download = False

        if video.status != VideoStatus.COMPLETE:
            video.status = VideoStatus.COMPLETE
            video.save()
            first_transcript_download = True

        website = video.website
        if is_ocw_site(website):
            search_fields = {}
            search_fields[get_dict_query_field(
                "metadata", settings.FIELD_RESOURCETYPE)] = RESOURCE_TYPE_VIDEO
            search_fields[get_dict_query_field(
                "metadata", settings.YT_FIELD_ID)] = video.youtube_id()

            for video_resource in website.websitecontent_set.filter(
                    **search_fields):
                metadata = video_resource.metadata
                set_dict_field(
                    metadata,
                    settings.YT_FIELD_TRANSCRIPT,
                    video.pdf_transcript_file.name,
                )
                set_dict_field(
                    metadata,
                    settings.YT_FIELD_CAPTIONS,
                    video.webvtt_transcript_file.name,
                )
                video_resource.save()

                if (first_transcript_download
                        and len(videos_missing_captions(website)) == 0):
                    mail_transcripts_complete_notification(website)
Пример #6
0
def test_set_dict_field():
    """The input dict should get updated with the expected keys/values"""
    input_dict = {
        "section_a": {
            "section_b": {
                "parameter_1": "a",
                "parameter_2": "b"
            }
        }
    }
    set_dict_field(input_dict, "section_a.section_b.parameter_3", "c")
    set_dict_field(input_dict, "section_a.new_param", "new_val_for_a")
    set_dict_field(input_dict, "section_a.section_b.parameter_2", "b_updated")
    set_dict_field(input_dict, "section_c.parameter_1", "new_section_val")
    set_dict_field(input_dict, "video_files.parameter_1", "value1")
    set_dict_field(input_dict, "video_files.parameter_2", "value2")
    assert input_dict == {
        "section_a": {
            "new_param": "new_val_for_a",
            "section_b": {
                "parameter_1": "a",
                "parameter_2": "b_updated",
                "parameter_3": "c",
            },
        },
        "section_c": {
            "parameter_1": "new_section_val"
        },
        "video_files": {
            "parameter_1": "value1",
            "parameter_2": "value2",
        },
    }