Exemple #1
0
def save_obj_from_entry(response):
    """
    Takes response object and stores it into db
    """
    v = Video()
    v.title = response['snippet']['title']
    v.ytid = response['id']
    unique_slugify(v, v.title)
    v.published_on = isodate.parse_datetime(response['snippet']['publishedAt'])

    # get duration of video
    duration = response["contentDetails"]["duration"]
    v.duration = int(isodate.parse_duration(duration).total_seconds())

    # get description of video
    try:
        v.description = response['snippet']['description']
    except:
        pass

    # get category
    try:
        category_response = youtube_api.videoCategories().list(
            part="snippet", id=response['snippet']['categoryId']).execute()
        v.category = category_response.get("items", [])[0]['snippet']['title']
    except:
        pass

    # get thumbnail - "default"
    try:
        v.thumbnail = response['snippet']['thumbnails']['default']['url']
    except:
        pass

    # get view count
    try:
        v.views = int(response['statistics']['viewCount'])
    except:
        pass

    v.save()
    return v
def save_obj_from_entry(entry):
    """
    Takes entry(gdata) object and stores it into db
    """
    v = Video()
    v.title = entry.media.title.text
    v.ytid = getid(entry)
    unique_slugify(v, v.title)
    v.duration = int(entry.media.duration.seconds)
    v.description = entry.media.description.text
    v.published_on = get_date(entry.published.text)
    v.category = entry.media.category[0].text
    if entry.rating is not None:
        v.rating = float(entry.rating.average)
    if entry.media.thumbnail is not None:
        v.thumbnail = entry.media.thumbnail[0].url
    try:
        v.views = int(entry.statistics.view_count)
    except:
        v.views = 0
    v.save()
    return v