<commit_before>"""
Created on 12/dic/2013

@author: <NAME>
"""

from django import template

from sorl.thumbnail import get_thumbnail, delete

from django_instagram import settings
from django_instagram.scraper import instagram_profile_json, instagram_profile_obj

register = template.Library()

def get_profile_media(profile, page = 0):
    """
    Parse a generated media object
    :param profile:
    :param page:
    :return:
    """
    return profile['entry_data']['ProfilePage'][page]['user']['media']['nodes']


class InstagramUserRecentMediaNode(template.Node):
    """
    Template node for showing recent media from an user.
    """

    def __init__(self, username):
        self.username = username

    def render(self, context):
        profile = instagram_profile_obj(username=self.username)
        context['profile'] = profile
        context['recent_media'] = get_profile_media(profile)

        return ''



@register.tag
def instagram_user_recent_media(parser, token):
    """
    Tag for getting data about recent media of an user.
    :param parser:
    :param token:<commit_msg>Fix KeyError ,  get_profile_media function <commit_after>"""
Created on 12/dic/2013

@author: <NAME>
"""

from django import template

from sorl.thumbnail import get_thumbnail, delete

from django_instagram import settings
from django_instagram.scraper import instagram_profile_json, instagram_profile_obj

register = template.Library()

def get_profile_media(profile, page = 0):
    """
    Parse a generated media object
    :param profile:
    :param page:
    :return:
    """
    try:
        return profile['entry_data']['ProfilePage'][page]['user']['media']['nodes']
    except KeyError:
        return ''


class InstagramUserRecentMediaNode(template.Node):
    """
    Template node for showing recent media from an user.
    """

    def __init__(self, username):
        self.username = username

    def render(self, context):
        profile = instagram_profile_obj(username=self.username)
        context['profile'] = profile
        context['recent_media'] = get_profile_media(profile)

        return ''



@register.tag
def instagram_user_recent_media(parser, token):
    """
    Tag for getting data about recent media of an user.
    :param parser:
    :param token:
{'fraction_non_alphanumeric': 0.10358391608391608, 'fraction_numerical': 0.006118881118881119, 'mean_word_length': 3.7098765432098766, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5444416', 'n_tokens_mistral': 669, 'n_tokens_neox': 640, 'n_words': 179}