from flask import jsonify


def insta1(json_data):
    if json_data:
        json_data_ = json_data
        try:
            json_result = json_data_['items'][0]
            user_metadata = json_result['user']
            location_data = json_result['location']
            caption_data = json_result['caption']
            caption_edited = json_result['caption_is_edited']
            media_count = json_result['carousel_media_count']
            media_data = json_result['carousel_media']
            media_result = []
            for i in media_data:
                if 'video_versions' in i:
                    typ = 'video'
                    media_data_ = i['video_versions']
                else:
                    typ = 'image'
                    media_data_ = i['image_versions2']['candidates']
                width = i['original_width']
                height = i['original_height']
                media_result.append({
                    'type':typ,
                    'url':media_data_[0]['url']
                })
            location = location_data['name']
            owner = {
                'username':user_metadata['username'],
                'full_name':user_metadata['full_name'],
                'verified':user_metadata['is_verified']
            }
            caption = caption_data['text']
            results = dict(
                status = True,
                caption = caption,
                caption_edited = caption_edited,
                owner = owner,
                location = location,
                media_result = media_result
            )
            return results
        except Exception as e:
            print(e)
            return {'status': False}

def insta2(json_data):
    if json_data:
        json_data_ = json_data
        try:
            json_result = json_data_
            type_name = json_result['graphql']['shortcode_media']['__typename']
            if type_name == 'GraphVideo':
                result_url = json_result['graphql']['shortcode_media']['video_url']
                view_count = json_result['graphql']['shortcode_media']['video_view_count']
                caption    = json_result['graphql']['shortcode_media']['edge_media_to_caption']['edges'][0]['node']['text']
                caption_edited = json_result['graphql']['shortcode_media']['caption_is_edited']
                owner = json_result['graphql']['shortcode_media']['owner']
                owner_verifed = owner['is_verified']
                owner_username = owner['username']
                owner_name = owner['full_name']
                video_duration = json_result['graphql']['shortcode_media']['video_duration']
                loc = json_result['graphql']['shortcode_media']['location']
                if loc == None:
                    location = "null"
                else:
                    location = loc['name']
                ac_caption = json_result['graphql']['shortcode_media']['accessibility_caption']
                if not ac_caption == None:
                    accessibility_caption = ac_caption,
                else:
                    accessibility_caption = "null"
                media_url = []
                media_url.append(dict(
                    type = 'video',
                    url = result_url
                    ))
                results = dict(
                    status = True,
                    caption = caption,
                    caption_edited = caption_edited,
                    owner = dict(
                        username = owner_username,
                        full_name = owner_name,
                        verified = owner_verifed
                        ),
                    location = location,
                    media_result = media_url
                    )
            elif type_name == 'GraphSidecar':
                caption    = json_result['graphql']['shortcode_media']['edge_media_to_caption']['edges'][0]['node']['text']
                caption_edited = json_result['graphql']['shortcode_media']['caption_is_edited']
                owner = json_result['graphql']['shortcode_media']['owner']
                owner_verifed = owner['is_verified']
                owner_username = owner['username']
                owner_name = owner['full_name']
                loc = json_result['graphql']['shortcode_media']['location']
                if loc == None:
                    location = "null"
                else:
                    location = loc['name']
                media = json_result['graphql']['shortcode_media']['edge_sidecar_to_children']['edges']
                media_result = []
                for i in media:
                    node = i['node']
                    type_name_ = node['__typename']
                    if type_name_ == 'GraphVideo':
                        result_url = node['video_url']
                        view_count = node['video_view_count']
                        ac_caption = node['accessibility_caption']
                        if not ac_caption == None:
                            accessibility_caption = ac_caption,
                        else:
                            accessibility_caption = "null"
                        media_result.append(dict(
                            type= 'video',
                            url = result_url
                            ))
                    else:
                        result_url = node['display_url']
                        ac_caption = node['accessibility_caption']
                        if not ac_caption == None:
                            accessibility_caption = ac_caption,
                        else:
                            accessibility_caption = "null"
                        media_result.append(dict(
                            type='image',
                            url= result_url
                            ))
                results = dict(
                    status = True,
                    caption = caption,
                    caption_edited = caption_edited,
                    owner = dict(
                        username = owner_username,
                        full_name = owner_name,
                        verified = owner_verifed
                        ),
                    location = location,
                    media_result = media_result
                    )
            else:
                result_url = json_result['graphql']['shortcode_media']['display_url']
                caption    = json_result['graphql']['shortcode_media']['edge_media_to_caption']['edges'][0]['node']['text']
                caption_edited = json_result['graphql']['shortcode_media']['caption_is_edited']
                owner = json_result['graphql']['shortcode_media']['owner']
                owner_verifed = owner['is_verified']
                owner_username = owner['username']
                owner_name = owner['full_name']
                loc = json_result['graphql']['shortcode_media']['location']
                if loc == None:
                    location = "null"
                else:
                    location = loc['name']
                ac_caption = json_result['graphql']['shortcode_media']['accessibility_caption']
                if not ac_caption == None:
                    accessibility_caption = ac_caption,
                else:
                    accessibility_caption = "null"
                media_result = []
                media_result.append(dict(
                    type = 'image',
                    url = result_url
                    ))
                results = dict(
                    status = True,
                    caption = caption,
                    caption_edited = caption_edited,
                    owner = dict(
                        username = owner_username,
                        full_name = owner_name,
                        verified = owner_verifed
                        ),
                    location = location,
                    media_result = media_result
                    )
            return (results)
        except Exception as e:
            print(e)
            return {'status': False}

def instatv1(json_data):
    if json_data:
        json_data_ = json_data
        if True:
            json_result = json_data_['items'][0]
            user_metadata = json_result['user']
            caption_data = json_result['caption']
            caption_edited = json_result['caption_is_edited']
            media_data_ = json_result['video_versions']
            width = json_result['original_width']
            height = json_result['original_height']
            like_count = json_result['like_count']
            video_duration = json_result['video_duration']
            view_count = json_result['view_count']
            media_result = {
                'url':media_data_[0]['url'],
                'width':width,
                'height':height
            }
            owner = {
                'username':user_metadata['username'],
                'full_name':user_metadata['full_name'],
                'verified':user_metadata['is_verified']
            }
            caption = caption_data['text']
            results = dict(
                status = True,
                caption = caption,
                caption_edited = caption_edited,
                owner = owner,
                like_count = like_count,
                video_duration = video_duration,
                view_count = view_count,
                media_result = media_result
            )
            return results

from flask import jsonify


def insta1(json_data):
    if json_data:
        json_data_ = json_data
        try:
            json_result = json_data_['items'][0]
            user_metadata = json_result['user']
            location_data = json_result['location']
            caption_data = json_result['caption']
            caption_edited = json_result['caption_is_edited']
            media_count = json_result['carousel_media_count']
            media_data = json_result['carousel_media']
            media_result = []
            for i in media_data:
                if 'video_versions' in i:
                    typ = 'video'
                    media_data_ = i['video_versions']
                else:
                    typ = 'image'
                    media_data_ = i['image_versions2']['candidates']
                width = i['original_width']
                height = i['original_height']
                media_result.append({
                    'type':typ,
                    'url':media_data_[0]['url']
                })
            location = location_data['name']
            owner = {
                'username':user_metadata['username'],
                'full_name':user_metadata['full_name'],
                'verified':user_metadata['is_verified']
            }
            caption = caption_data['text']
            results = dict(
                status = True,
                caption = caption,
                caption_edited = caption_edited,
                owner = owner,
                location = location,
                media_result = media_result
            )
            return results
        except Exception as e:
            print(e)
            return {'status': False}

def insta2(json_data):
    if json_data:
        json_data_ = json_data
        try:
            json_result = json_data_
            type_name = json_result['graphql']['shortcode_media']['__typename']
            if type_name == 'GraphVideo':
                result_url = json_result['graphql']['shortcode_media']['video_url']
                view_count = json_result['graphql']['shortcode_media']['video_view_count']
                caption    = json_result['graphql']['shortcode_media']['edge_media_to_caption']['edges'][0]['node']['text']
                caption_edited = json_result['graphql']['shortcode_media']['caption_is_edited']
                owner = json_result['graphql']['shortcode_media']['owner']
                owner_verifed = owner['is_verified']
                owner_username = owner['username']
                owner_name = owner['full_name']
                video_duration = json_result['graphql']['shortcode_media']['video_duration']
                loc = json_result['graphql']['shortcode_media']['location']
                if loc == None:
                    location = "null"
                else:
                    location = loc['name']
                ac_caption = json_result['graphql']['shortcode_media']['accessibility_caption']
                if not ac_caption == None:
                    accessibility_caption = ac_caption,
                else:
                    accessibility_caption = "null"
                media_url = []
                media_url.append(dict(
                    type = 'video',
                    url = result_url
                    ))
                results = dict(
                    status = True,
                    caption = caption,
                    caption_edited = caption_edited,
                    owner = dict(
                        username = owner_username,
                        full_name = owner_name,
                        verified = owner_verifed
                        ),
                    location = location,
                    media_result = media_url
                    )
            elif type_name == 'GraphSidecar':
                caption    = json_result['graphql']['shortcode_media']['edge_media_to_caption']['edges'][0]['node']['text']
                caption_edited = json_result['graphql']['shortcode_media']['caption_is_edited']
                owner = json_result['graphql']['shortcode_media']['owner']
                owner_verifed = owner['is_verified']
                owner_username = owner['username']
                owner_name = owner['full_name']
                loc = json_result['graphql']['shortcode_media']['location']
                if loc == None:
                    location = "null"
                else:
                    location = loc['name']
                media = json_result['graphql']['shortcode_media']['edge_sidecar_to_children']['edges']
                media_result = []
                for i in media:
                    node = i['node']
                    type_name_ = node['__typename']
                    if type_name_ == 'GraphVideo':
                        result_url = node['video_url']
                        view_count = node['video_view_count']
                        ac_caption = node['accessibility_caption']
                        if not ac_caption == None:
                            accessibility_caption = ac_caption,
                        else:
                            accessibility_caption = "null"
                        media_result.append(dict(
                            type= 'video',
                            url = result_url
                            ))
                    else:
                        result_url = node['display_url']
                        ac_caption = node['accessibility_caption']
                        if not ac_caption == None:
                            accessibility_caption = ac_caption,
                        else:
                            accessibility_caption = "null"
                        media_result.append(dict(
                            type='image',
                            url= result_url
                            ))
                results = dict(
                    status = True,
                    caption = caption,
                    caption_edited = caption_edited,
                    owner = dict(
                        username = owner_username,
                        full_name = owner_name,
                        verified = owner_verifed
                        ),
                    location = location,
                    media_result = media_result
                    )
            else:
                result_url = json_result['graphql']['shortcode_media']['display_url']
                caption    = json_result['graphql']['shortcode_media']['edge_media_to_caption']['edges'][0]['node']['text']
                caption_edited = json_result['graphql']['shortcode_media']['caption_is_edited']
                owner = json_result['graphql']['shortcode_media']['owner']
                owner_verifed = owner['is_verified']
                owner_username = owner['username']
                owner_name = owner['full_name']
                loc = json_result['graphql']['shortcode_media']['location']
                if loc == None:
                    location = "null"
                else:
                    location = loc['name']
                ac_caption = json_result['graphql']['shortcode_media']['accessibility_caption']
                if not ac_caption == None:
                    accessibility_caption = ac_caption,
                else:
                    accessibility_caption = "null"
                media_result = []
                media_result.append(dict(
                    type = 'image',
                    url = result_url
                    ))
                results = dict(
                    status = True,
                    caption = caption,
                    caption_edited = caption_edited,
                    owner = dict(
                        username = owner_username,
                        full_name = owner_name,
                        verified = owner_verifed
                        ),
                    location = location,
                    media_result = media_result
                    )
            return (results)
        except Exception as e:
            print(e)
            return {'status': False}

def instatv1(json_data):
    if json_data:
        json_data_ = json_data
        if True:
            json_result = json_data_['items'][0]
            user_metadata = json_result['user']
            caption_data = json_result['caption']
            caption_edited = json_result['caption_is_edited']
            media_data_ = json_result['video_versions']
            width = json_result['original_width']
            height = json_result['original_height']
            like_count = json_result['like_count']
            video_duration = json_result['video_duration']
            view_count = json_result['view_count']
            media_result = {
                'url':media_data_[0]['url'],
                'width':width,
                'height':height
            }
            owner = {
                'username':user_metadata['username'],
                'full_name':user_metadata['full_name'],
                'verified':user_metadata['is_verified']
            }
            caption = caption_data['text']
            results = dict(
                status = True,
                caption = caption,
                caption_edited = caption_edited,
                owner = owner,
                like_count = like_count,
                video_duration = video_duration,
                view_count = view_count,
                media_result = media_result
            )
            return results
