# 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


# 3. show a list of videos in a given channel
elif STATE == ST_CHN_VIDEOS:
    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
        descr = div.find('div', class_='yt-lockup-description')
        meta = div.find('ul', class_='yt-lockup-meta-info')
        li = meta.find('li')
        uploaded = li.string
        views = li.next_sibling.string
        info = '<title>%s</title> <small>%s</small><br>' \
               '<small><name>%s %s</name><br>' \
               '<success>%s</success></small>' \
               '<br>%s' % (
                   title, duration,
                   _('uploaded'), uploaded,
                   views, descr)  # or '')

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


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

# 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



# 3. show a list of videos in a given channel
elif STATE == ST_CHN_VIDEOS:
    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
        descr = div.find('div', class_='yt-lockup-description')
        meta = div.find('ul', class_='yt-lockup-meta-info')
        li = meta.find('li')
        uploaded = li.string
        views = li.next_sibling.string
        info = '<title>%s</title> <small>%s</small><br>' \
               '<small><name>%s %s</name><br>' \
               '<success>%s</success></small>' \
               '<br>%s' % (
                   title, duration,
                   _('uploaded'), uploaded,
                   views, descr)  # or '')

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


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

    play_url(call_ydl(URL))