#####################################################################
#                                                                   #
#  THIS IS A SOURCE CODE FILE FROM A PROGRAM TO INTERACT WITH THE   #
# LBRY PROTOCOL ( lbry.com ). IT WILL USE THE LBRY SDK ( lbrynet )  #
# FROM THEIR REPOSITORY ( https://github.com/lbryio/lbry-sdk )      #
# WHICH I GONNA PRESENT TO YOU AS A BINARY. SINCE I DID NOT DEVELOP #
# IT AND I'M LAZY TO INTEGRATE IN A MORE SMART WAY. THE SOURCE CODE #
# OF THE SDK IS AVAILABLE IN THE REPOSITORY MENTIONED ABOVE.        #
#                                                                   #
#      ALL THE CODE IN THIS REPOSITORY INCLUDING THIS FILE IS       #
# (C) J.Y.Amihud and Other Contributors 2021. EXCEPT THE LBRY SDK.  #
# YOU CAN USE THIS FILE AND ANY OTHER FILE IN THIS REPOSITORY UNDER #
# THE TERMS OF GNU GENERAL PUBLIC LICENSE VERSION 3 OR ANY LATER    #
# VERSION. TO FIND THE FULL TEXT OF THE LICENSE GO TO THE GNU.ORG   #
# WEBSITE AT ( https://www.gnu.org/licenses/gpl-3.0.html ).         #
#                                                                   #
# THE LBRY SDK IS UNFORTUNATELY UNDER THE MIT LICENSE. IF YOU ARE   #
# NOT INTENDING TO USE MY CODE AND JUST THE SDK. YOU CAN FIND IT ON #
# THEIR OFFICIAL REPOSITORY ABOVE. THEIR LICENSE CHOICE DOES NOT    #
# SPREAD ONTO THIS PROJECT. DON'T GET A FALSE ASSUMPTION THAT SINCE #
# THEY USE A PUSH-OVER LICENSE, I GONNA DO THE SAME. I'M NOT.       #
#                                                                   #
# THE LICENSE CHOSEN FOR THIS PROJECT WILL PROTECT THE 4 ESSENTIAL  #
# FREEDOMS OF THE USER FURTHER, BY NOT ALLOWING ANY WHO TO CHANGE   #
# THE LICENSE AT WILL. SO NO PROPRIETARY SOFTWARE DEVELOPER COULD   #
# TAKE THIS CODE AND MAKE THEIR USER-SUBJUGATING SOFTWARE FROM IT.  #
#                                                                   #
#####################################################################

# While livestreams were never officially a part of LBRY, comments weren't
# either and people want to watch and interact with livestreams too while
# they do not want to use Odysee.com.

# The odysee devs told me that they do not want livestreams to be playable
# outside of Odysee. So a number of interesting sticks will have to be done
# to make them actually work outside of odysee.

import os
import json
import time
import urllib.request

from flbry import fetch

# This will make odysee think that we are just a normal browser running windows ( lol )
# and connecting from Odysee.com
headers = {"Origin":"https://odysee.com",
           "Referer":"https://odysee.com/",
           "User-Agent":"Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0"}

def get_data(channel_id):

    # This function will return data about channel livestreams.
    url = "https://api.odysee.live/livestream/is_live?channel_claim_id="+channel_id
    
    
    req = urllib.request.Request(url, data=None, headers=headers)

    f = urllib.request.urlopen(req)
    return json.loads(f.read().decode('utf-8'))


def get_all():

    # This function will return all current livestreams on Odysee.

    url = "https://api.odysee.live/livestream/all"

    req = urllib.request.Request(url, data=None, headers=headers)

    f = urllib.request.urlopen(req)
    return json.loads(f.read().decode('utf-8'))

def get_upcoming( channels=[], tags=[]):

    # This function will return all upcoming livestreams


    c = {"all_tags":["c:scheduled-livestream"],
         "release_time":">"+str(int(time.time())),
         "remove_duplicates":True}

    if tags:
        c["any_tags"] = tags
    if channels:
        c["channel_ids"] = channels

    out = fetch.lbrynet("claim_search", c)
    
    return out
