# 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):


from epymc.extapi.onlinevideo import api_version, state_get, \
    fetch_url, play_url, item_add, call_ydl, report_error, \
    seconds_to_duration, relative_date, url_encode, \
    ACT_NONE, ACT_FOLDER, ACT_MORE, ACT_PLAY, ACT_SEARCH

# videos list
elif STATE == ST_VIDEO_LIST:
    data = fetch_url(URL, parser='json')
    try:
        build_video_list(URL, data['videos'])
    except KeyError:  # last page probably reached
        pass


# categories list
elif STATE == ST_CATEGORIES:
    data = fetch_url(URL, parser='json')
    for cat in data['categories']:
        url = API_BASE + '/search?' + url_encode({'category': cat['category']})
        item_add(ST_VIDEO_LIST, cat['category'], url + '&page=1')


# pornstars list
# elif STATE == ST_PORNSTARS:
# data = fetch_url(URL, parser='json')
# for star in data['stars']:
# star = star['star']
# url = star['star_url'] + '&page=1'  ## THIS IS WRONG (or must be scraped)
# title = '{} ({} vids)'.format(star['star_name'], star['videos_count_all'])
# item_add(ST_VIDEO_LIST, title, url, poster=star['star_thumb'])

# play (using youtube-dl)
elif STATE == ST_PLAY:
    url = call_ydl(URL)

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


# videos list
elif STATE == ST_VIDEO_LIST:
    data = fetch_url(URL, parser='json')
    try:
        build_video_list(URL, data['videos'])
    except KeyError:  # last page probably reached
        pass


# categories list
elif STATE == ST_CATEGORIES:
    data = fetch_url(URL, parser='json')
    for cat in data['categories']:
        url = API_BASE + '/search?' + url_encode({'category': cat['category']})
        item_add(ST_VIDEO_LIST, cat['category'], url + '&page=1')


# pornstars list
# elif STATE == ST_PORNSTARS:
# data = fetch_url(URL, parser='json')
# for star in data['stars']:
# star = star['star']
# url = star['star_url'] + '&page=1'  ## THIS IS WRONG (or must be scraped)
# title = '{} ({} vids)'.format(star['star_name'], star['videos_count_all'])
# item_add(ST_VIDEO_LIST, title, url, poster=star['star_thumb'])

# play (using youtube-dl)
elif STATE == ST_PLAY:
    url = call_ydl(URL)

    play_url(url) if url else report_error('Video not found')