# Path: epymc/extapi/onlinevideo.py
# ACT_DEFAULT = 0
# ACT_NONE = 1
# ACT_FOLDER = 2
# ACT_MORE = 3
# ACT_PLAY = 4
# ACT_SEARCH = 5
# def state_get():
# def language_get():
# def item_add(next_state, label, url, info=None, icon=None, poster=None, action=ACT_DEFAULT):
# def play_url(url):
# def report_error(msg):
# def local_resource(_file_, res):
# def fetch_url(url, headers=None, parser=None):
# def ydl_executable():
# def call_ydl(url):
# def url_encode(params):
# def seconds_to_duration(seconds):
# def relative_date(date):
# def format_date(date):


import os
from epymc.extapi.onlinevideo import api_version, state_get, fetch_url, \
    play_url, item_add, call_ydl, local_resource, seconds_to_duration, \
    relative_date, url_encode, \
    ACT_NONE, ACT_FOLDER, ACT_MORE, ACT_PLAY, ACT_SEARCH
    soup = fetch_url(URL, parser='bs4')
    for div in soup.findAll('div', class_='yt-lockup-video'):
        id = div['data-context-item-id']
        title = div.find('h3', class_='yt-lockup-title').find('a')['title']
        url = ytb_base + '/watch?v=' + id
        poster = 'http://i.ytimg.com/vi/' + id + '/hqdefault.jpg'

        duration = div.find('span', class_='video-time').string
        user = div.find('div', class_='yt-lockup-byline').find('a').string
        descr = div.find('div', class_='yt-lockup-description')
        meta = div.find('ul', class_='yt-lockup-meta-info')
        if len(meta.contents) != 2:
            continue  # skip Ad items
        uploaded = meta.contents[0].string
        views = meta.contents[1].string

        info = '<title>%s</> <small>%s</><br>' \
               '<small><name>%s</> %s <name>/ %s %s</><br>' \
               '<success>%s</></small>' \
               '<br>%s' % (
                   title, duration,
                   _('user'), user,
                   _('uploaded'), uploaded,
                   views, descr or '')

        item_add(ST_PLAY, title, url, info=info, poster=poster)

    # more items...
    try:
        url = ytb_base + soup.find('div', class_='search-pager').contents[-2]['href']

# Path: epymc/extapi/onlinevideo.py
# ACT_DEFAULT = 0
# ACT_NONE = 1
# ACT_FOLDER = 2
# ACT_MORE = 3
# ACT_PLAY = 4
# ACT_SEARCH = 5
# def state_get():
# def language_get():
# def item_add(next_state, label, url, info=None, icon=None, poster=None, action=ACT_DEFAULT):
# def play_url(url):
# def report_error(msg):
# def local_resource(_file_, res):
# def fetch_url(url, headers=None, parser=None):
# def ydl_executable():
# def call_ydl(url):
# def url_encode(params):
# def seconds_to_duration(seconds):
# def relative_date(date):
# def format_date(date):


# Path: epymc/plugins/onlinevideo/youtube/youtube.py
import os
from epymc.extapi.onlinevideo import api_version, state_get, fetch_url, \
    play_url, item_add, call_ydl, local_resource, seconds_to_duration, \
    relative_date, url_encode, \
    ACT_NONE, ACT_FOLDER, ACT_MORE, ACT_PLAY, ACT_SEARCH

    soup = fetch_url(URL, parser='bs4')
    for div in soup.findAll('div', class_='yt-lockup-video'):
        id = div['data-context-item-id']
        title = div.find('h3', class_='yt-lockup-title').find('a')['title']
        url = ytb_base + '/watch?v=' + id
        poster = 'http://i.ytimg.com/vi/' + id + '/hqdefault.jpg'

        duration = div.find('span', class_='video-time').string
        user = div.find('div', class_='yt-lockup-byline').find('a').string
        descr = div.find('div', class_='yt-lockup-description')
        meta = div.find('ul', class_='yt-lockup-meta-info')
        if len(meta.contents) != 2:
            continue  # skip Ad items
        uploaded = meta.contents[0].string
        views = meta.contents[1].string

        info = '<title>%s</> <small>%s</><br>' \
               '<small><name>%s</> %s <name>/ %s %s</><br>' \
               '<success>%s</></small>' \
               '<br>%s' % (
                   title, duration,
                   _('user'), user,
                   _('uploaded'), uploaded,
                   views, descr or '')

        item_add(ST_PLAY, title, url, info=info, poster=poster)

    # more items...
    try:
        url = ytb_base + soup.find('div', class_='search-pager').contents[-2]['href']

        item_add(ST_SEARCH, _('More items...'), url, icon='icon/next', action=ACT_MORE)