# 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
ST_CHN_VIDEOS = 12

STATE, URL = state_get()

# this is the first page, show fixed categories
if STATE == ST_HOME:
    item_add(ST_SEARCH, _('Search videos'), 'search', None, action=ACT_SEARCH)
    item_add(ST_CHN_CATEGORIES, _('Browse channels'),
             ytb_base + '/channels', None, action=ACT_FOLDER)

###############################################################################
# youtube site scraper ########################################################
###############################################################################

# 0. search results
if STATE == ST_SEARCH:
    if not URL.startswith(ytb_base):
        # first page, URL is the search query entered by the user
        URL = ytb_base + '/results?' + url_encode(
            {'search_query': URL, 'filters': 'video'})

    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

# 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

ST_CHN_VIDEOS = 12

STATE, URL = state_get()

# this is the first page, show fixed categories
if STATE == ST_HOME:
    item_add(ST_SEARCH, _('Search videos'), 'search', None, action=ACT_SEARCH)
    item_add(ST_CHN_CATEGORIES, _('Browse channels'),
             ytb_base + '/channels', None, action=ACT_FOLDER)

###############################################################################
# youtube site scraper ########################################################
###############################################################################

# 0. search results
if STATE == ST_SEARCH:
    if not URL.startswith(ytb_base):
        # first page, URL is the search query entered by the user
        URL = ytb_base + '/results?' + url_encode(
            {'search_query': URL, 'filters': 'video'})

    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')