Example #1
0
    def generate_mmm_template(self):
        """
        create mmm json template

        Args:
            None

        Return:
            None
        """

        relocate_path = 'video/orig/{}'.format(self.fullCameraLocation)
        file_parts = self.video_name.split('.')
        ext = file_parts.pop()
        date_time_original_timestamp = int(pytz.timezone('Asia/Taipei').localize(self.video_org_datetime).timestamp()) # 由 date_time_original 轉換而來
        base_file_name = '{}_{}.{}'.format('.'.join(file_parts), date_time_original_timestamp, ext.lower())
        relative_url = '{}/{}'.format(relocate_path, base_file_name)

        return {
            '_id': to_md5_hexdigest(relative_url),
            'projectId': self.projectId,
            'fullCameraLocationMd5': to_md5_hexdigest(self.fullCameraLocation),
            '$set': {
                'youtube_url': self.youtube_url,
                'modifiedBy': self.userId,
                'type': 'MovingImage',
                'date_time_original': self.video_org_datetime.strftime("%Y-%m-%d %H:%M:%S"), # 格式以metadata 中擷取出來的為準
                'date_time_original_timestamp': date_time_original_timestamp, # 由date_time_original 轉換而來
                'length_of_video': self.video_length, # 暫時以 metadata 中擷取出來的為準
                'youtube_playlist_id': self.youtube_playlist_id, 
                'device_metadata': self.device_metadata, # 與相機相關但非 EXIF 的 Metadata 整組直接以 json 先塞在這
                'exif': self.exif, # EXIF 整組先以 json 塞在這
                'make': self.make, # 相機製造商(如果有此項資訊的話)
                'model': self.model, # 相機型號(如果有此項資訊的話)
                'modify_date': self.video_mod_datetime.strftime("%Y-%m-%d %H:%M:%S"), # device_metadata 中的檔案編修時間
                'width': self.video_width,
                'height': self.video_height
            },
            '$setOnInsert': {
                'url': relative_url,
                'url_md5': to_md5_hexdigest(relative_url),
                'date_time_corrected_timestamp': date_time_original_timestamp, # 這邊此值等於 date_time_original_timestamp
                'corrected_date_time': self.video_org_datetime.strftime("%Y-%m-%d %H:%M:%S"),
                'projectId': self.projectId,
                'projectTitle': self.projectTitle,
                'site': self.site,
                'subSite': self.subSite,
                'cameraLocation': self.cameraLocation,
                'fullCameraLocationMd5': to_md5_hexdigest(self.fullCameraLocation),
                'uploaded_file_name': self.video_name,
                'timezone': '+8',
                'year': self.video_org_datetime.strftime("%Y"),
                'month': self.video_org_datetime.strftime("%m"),
                'day': self.video_org_datetime.strftime("%d"),
                'hour': self.video_org_datetime.strftime("%H")
            },
            '$upsert': True
        }
Example #2
0
def check_if_video_exist(file_name, date_time_original, projectId, site,
                         subSite, cameraLocation):
    """
    check if video exists in TaiBIF

    Args:
        file_name: 
            string => the status of this action
        original_datetime: 
            string =>  video original datetime 
        projectId: 
            string =>  from object tag - projectId
        site: 
            string =>  from object tag - site
        subSite: 
            string =>  from object tag - sub_subsite
        cameraLocation: 
            string =>  from object tag - cameraLocation

    Return:
        bool, string => True if video exists , url 
    """

    is_video_exist = False
    youtube_url = ''
    youtube_playlist_id = ''

    # check if this video has been uploaded or not
    # if the video was already uploaded, then dismiss the job
    location_path = CommenHelpers.generate_location_path(
        projectId, site, subSite, cameraLocation)
    location_path_md5 = CommenHelpers.to_md5_hexdigest(location_path)

    result = query_multimedia_metadata(
        file_name,
        int(
            pytz.timezone('Asia/Taipei').localize(
                date_time_original).timestamp()), location_path_md5)

    if 'results' in result and result['results'] is not None and len(
            result['results']) > 0 and 'youtube_url' in result['results'][
                0] and result['results'][0]['youtube_url'] is not None:
        is_video_exist = True
        youtube_url = result['results'][0]['youtube_url']
        youtube_playlist_id = result['results'][0]['youtube_playlist_id']

    return is_video_exist, youtube_url, youtube_playlist_id
Example #3
0
    def generate_mma_template(self):
        """
        create mma json template

        Args:
            None

        Return:
            None
        """

        relocate_path = 'video/orig/{}'.format(self.fullCameraLocation)
        file_parts = self.video_name.split('.')
        ext = file_parts.pop()
        date_time_original_timestamp = int(pytz.timezone('Asia/Taipei').localize(self.video_org_datetime).timestamp()) # 由 date_time_original 轉換而來
        base_file_name = '{}_{}.{}'.format('.'.join(file_parts), date_time_original_timestamp, ext.lower())
        relative_url = '{}/{}'.format(relocate_path, base_file_name)

        return {
            '_id': to_md5_hexdigest(relative_url),
            'projectId': self.projectId,
            'fullCameraLocationMd5': to_md5_hexdigest(self.fullCameraLocation),
            '$set': {
                'youtube_url': self.youtube_url,
                'modifiedBy': self.userId,
                'type': 'MovingImage',
                'date_time_original': self.video_org_datetime.strftime("%Y-%m-%d %H:%M:%S"),  # 格式以 metadata 中擷取出來的為準
                'date_time_original_timestamp': date_time_original_timestamp,
                'length_of_video': self.video_length,  # 暫時以 metadata 中擷取出來的為準
                'youtube_playlist_id': self.youtube_playlist_id
            },
            '$setOnInsert': {
                'url': relative_url,
                'url_md5': to_md5_hexdigest(relative_url),
                'date_time_corrected_timestamp': date_time_original_timestamp, # 這邊此值等於 date_time_original_timestamp
                'corrected_date_time': self.video_org_datetime.strftime("%Y-%m-%d %H:%M:%S"),
                'projectId': self.projectId,
                'projectTitle': self.projectTitle,
                'site': self.site,
                'subSite': self.subSite,
                'cameraLocation': self.cameraLocation,
                'fullCameraLocationMd5': to_md5_hexdigest(self.fullCameraLocation),
                'uploaded_file_name': self.video_name,
                'timezone': '+8',
                'year': self.video_org_datetime.strftime("%Y"),
                'month': self.video_org_datetime.strftime("%m"),
                'day': self.video_org_datetime.strftime("%d"),
                'hour': self.video_org_datetime.strftime("%H"),
                'tokens': [
                    {
                        'data': [
                            {
                                'key': 'species',
                                'label': '物種',
                                'value': '尚未辨識'
                            }
                        ],
                        'species_shortcut': '尚未辨識'
                    }
                ]
            },
            '$addToSet': {
                'related_upload_sessions': self.upload_session_id
            },
            '$upsert': True
        }
Example #4
0
def lambda_handler(event, context):
    print('event: {}'.format(event))

    event_key = urllib.parse.unquote(
        event['Records'][0]['s3']['object']['key'])
    session_id, file_name = S3Helpers.split_file_name(event_key)
    tags = S3Helpers.obtain_object_tags_from_s3(event_key)
    set_default_value(tags)

    print('session_id: {}'.format(session_id))
    print('file_name: {}'.format(file_name))
    print('tags: {}'.format(tags))

    parser = argparse.ArgumentParser()

    # path of the file location
    parser.add_argument(
        '--file', default=CommenHelpers.get_full_download_path(file_name))

    # video title on YouTube
    parser.add_argument('--title', default=file_name)

    # description for the video
    parser.add_argument('--description', default=file_name)

    # default 27 - Education, see more - https://developers.google.com/youtube/v3/docs/videoCategories/list
    parser.add_argument('--category', default='27')

    # keywords for the video
    parser.add_argument('--keywords', default=tags)

    # set if this video is public or private. options: 'public', 'private', 'unlisted'
    parser.add_argument('--privacyStatus', default='public')
    args = parser.parse_args()

    # download file to /tmp
    S3Helpers.download_file_to_tmp(SYS_PARAMS.SRC_BUCKET, file_name, event_key)

    # get video metadata
    video_meta = extra_video_meta(
        CommenHelpers.get_full_download_path(file_name))

    # check if this video has been uploaded or not
    # if the video was already uploaded, then dismiss the job
    # is_video_exist, youtube_url, youtube_playlist_id = check_if_video_exist(file_name,
    #                                        video_meta['date_time_original'],
    #                                        tags['projectId'],
    #                                        tags['site'],
    #                                        tags['subSite'],
    #                                        tags['cameraLocation'])

    try:
        # get authorization
        client_instance = get_authenticated_service()

        location_path = CommenHelpers.generate_location_path(
            tags['projectId'], tags['site'], tags['subSite'],
            tags['cameraLocation'])
        relocate_path = 'video/orig/{}'.format(location_path)
        file_parts = file_name.split('.')
        ext = file_parts.pop()
        date_time_original_timestamp = int(
            pytz.timezone('Asia/Taipei').localize(
                video_meta['date_time_original']).timestamp()
        )  # 由 date_time_original 轉換而來
        base_file_name = '{}_{}.{}'.format('.'.join(file_parts),
                                           date_time_original_timestamp,
                                           ext.lower())
        relative_url = '{}/{}'.format(relocate_path, base_file_name)
        url_md5 = CommenHelpers.to_md5_hexdigest(relative_url)
        print(relative_url)

        found = search_list_by_keyword(client_instance,
                                       part='snippet',
                                       maxResults=1,
                                       forMine=1,
                                       q=url_md5,
                                       type='video')

        upload_meta = [
            tags['projectId'], tags['projectTitle'], tags['site'],
            tags['subSite'], tags['cameraLocation'], url_md5
        ]

        # if is_video_exist:
        if found['pageInfo']['totalResults'] > 0:
            if found['items'][0]['snippet']['title'] == url_md5:
                video_id = found['items'][0]['id']['videoId']
                print('{} was already uploaded. url: {}'.format(
                    file_name, video_id))

        else:
            # upload video
            args.title = url_md5
            video_id = initialize_upload(client_instance, args)

        # add video to target playlist
        youtube_url = '{}{}'.format(SYS_PARAMS.YOUTUBE_VIDEO_URL, video_id)

        youtube_playlist_id = add_video_to_playlist(client_instance, video_id,
                                                    upload_meta)

        # create mma/mmm json file and upload to s3 bucket
        json_gen = JsonFileGenerator(
            bucket=SYS_PARAMS.SRC_BUCKET,
            youtube_url=youtube_url,
            youtube_playlist_id=youtube_playlist_id,
            projectId=tags['projectId'],
            projectTitle=tags['projectTitle'],
            site=tags['site'],
            subSite=tags['subSite'],
            cameraLocation=tags['cameraLocation'],
            video_name=file_name,
            video_length=video_meta['duration'],
            video_org_datetime=video_meta['date_time_original'],
            video_mod_datetime=video_meta['date_last_modification'],
            video_width=video_meta['width'],
            video_height=video_meta['height'],
            userId=tags['userId'],
            upload_session_id=session_id,
            device_metadata=video_meta['device_metadata'],
            exif=video_meta['exif'],
            make=video_meta['make'],
            model=video_meta['model'])

        json_gen.do_process()

    except HttpError as e:
        print('An HTTP error %d occurred:\n%s' % (e.resp.status, e.content))

    except Exception as e:
        print(e)

    return {"statusCode": 200, "body": json.dumps('Success')}