# 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 sys
from epymc.extapi.onlinevideo import api_version, state_get, \
    fetch_url, play_url, report_error, item_add, call_ydl, local_resource, \
    relative_date, seconds_to_duration, url_encode, URLError, HTTPError, \
    ACT_NONE, ACT_FOLDER, ACT_MORE, ACT_PLAY, ACT_SEARCH
            poster = [c['link'] for c in cat['pictures']['sizes'] if c['width'] == 640][0]
            if '?' in poster: poster = poster.split('?')[0]
        except:
            poster = None

        videos = cat['metadata']['connections']['videos']['total']
        channels = cat['metadata']['connections']['channels']['total']
        groups = cat['metadata']['connections']['groups']['total']
        info = '<title>%s</><br>' \
               '<small><success>%d %s</> <name>/</> ' \
               '<info>%d %s</> <name>/</> <warning>%d %s</></small>' % (name,
                                                                        videos, ngettext('video', 'videos', videos),
                                                                        channels,
                                                                        ngettext('channel', 'channels', channels),
                                                                        groups, ngettext('group', 'groups', groups))

        item_add(NEXT_STATE, name, url, icon=icon_categories,
                 info=info, poster=poster)

        for sub in cat['subcategories']:
            subname = name + ' - ' + sub['name']
            url = api_base + sub['uri'] + URL + '?per_page=%d' % ITEMS_PER_PAGE
            item_add(NEXT_STATE, subname, url, icon=icon_categories,
                     info=info, poster=poster)


################################################################################
# play a video using youtube-dl to get the real url   \o/
################################################################################
elif STATE == ST_PLAY:

# 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/vimeo/vimeo.py
import sys
from epymc.extapi.onlinevideo import api_version, state_get, \
    fetch_url, play_url, report_error, item_add, call_ydl, local_resource, \
    relative_date, seconds_to_duration, url_encode, URLError, HTTPError, \
    ACT_NONE, ACT_FOLDER, ACT_MORE, ACT_PLAY, ACT_SEARCH

            poster = [c['link'] for c in cat['pictures']['sizes'] if c['width'] == 640][0]
            if '?' in poster: poster = poster.split('?')[0]
        except:
            poster = None

        videos = cat['metadata']['connections']['videos']['total']
        channels = cat['metadata']['connections']['channels']['total']
        groups = cat['metadata']['connections']['groups']['total']
        info = '<title>%s</><br>' \
               '<small><success>%d %s</> <name>/</> ' \
               '<info>%d %s</> <name>/</> <warning>%d %s</></small>' % (name,
                                                                        videos, ngettext('video', 'videos', videos),
                                                                        channels,
                                                                        ngettext('channel', 'channels', channels),
                                                                        groups, ngettext('group', 'groups', groups))

        item_add(NEXT_STATE, name, url, icon=icon_categories,
                 info=info, poster=poster)

        for sub in cat['subcategories']:
            subname = name + ' - ' + sub['name']
            url = api_base + sub['uri'] + URL + '?per_page=%d' % ITEMS_PER_PAGE
            item_add(NEXT_STATE, subname, url, icon=icon_categories,
                     info=info, poster=poster)


################################################################################
# play a video using youtube-dl to get the real url   \o/
################################################################################
elif STATE == ST_PLAY:

    play_url(call_ydl(URL))