I am going to dump this here.
import os
import sys
import uuid
import json
import orjson
import requests
import datetime
import websockets
import asyncio
from config import API_LIVE_URL
from config import API_DEMO_URL
from my_secrets import APP_ID, APP_VERSION, ACCOUNT, ACCOUNT_P, CID, CLIENT_SECRET, DEVICE_ID
def get_endpoint(endpoint, _data, DEMO, query=None):
url = None
json_response_data = None
if(DEMO):
__api_url = API_DEMO_URL
print("__TV_API -- get_endpoint -- DEMO URL -- [{0}]".format(__api_url))
else:
__api_url = API_LIVE_URL
print("__TV_API -- get_endpoint -- LIVE URL -- [{0}]".format(__api_url))
if(query):
url = "{0}/{1}/{2}".format(__api_url, endpoint, query)
else:
url = "{0}/{1}".format(__api_url, endpoint)
head = { 'content-type':'application/json', 'accept':'application/json', }
print("__TV_API -- get_endpoint -- header | {0}".format(head))
print("__TV_API -- get_endpoint -- payload | {0}".format(_data))
print("__TV_API -- get_endpoint -- url | {0}".format(url))
p = requests.post(url, headers=head, data=_data)
status = p.status_code
if(status == 200):
print("__TV_API -- get_endpoint -- status code 200 -- [{0}]".format(p.status_code))
print("__TV_API -- get_endpoint -- status code 200 -- [{0}]".format(p.text))
print("__TV_API -- get_endpoint -- status code 200 -- [{0}]".format(p.content))
try:
json_response_data = json.loads(p.text)
except Exception as e:
print("get_endpoint -- exception [{0}]\n[{1}]\n".format(str(e), p.text))
return json_response_data
else:
print("get_endpoint -- status code {0} -- [{1}]".format(status, p.text))
return json_response_data
def __get_access_token(DEMO):
_filename = "./accessToken.py"
diff = None
accessToken = None
mdAccessToken = None
expirationTime = None
_update_at = False
_ = None
package = None
if(os.path.isfile(_filename) and not(os.stat(_filename).st_size==0)):
time = None
with open(_filename) as infile:
package = json.load(infile)
try:
time = package['expirationTime']
except Exception as e:
print("__TV_API -- exception [{0}]\n".format(str(e)))
time = None
if(time == None):
print("FILE EXCEPTION\nRE-CREATING FILE")
_ = __authenticate(DEMO)
package = _
with open(_filename, "w") as outfile:
json.dump(_, outfile)
return package
else:
expirationTime = datetime.datetime.strptime(time, "%Y-%m-%dT%H:%M:%S.%fZ")
diff = (expirationTime - datetime.datetime.now()).total_seconds()
if(diff <= 0.0):
a = "__TV_API -- __get_access_token -- ACCESS TOKEN EXPIRED [{0}], UPDATE --\n[{1}]\n"
a = a.format(diff, package['accessToken'])
print(a)
_ = __authenticate(DEMO)
package = _
with open(_filename, "w") as outfile:
json.dump(_, outfile)
else:
a = "__TV_API -- DO NOT create file -- YET | [{0}] - [{1}] - [{2}]\n"
a = a.format(expirationTime, diff, _filename)
print(a)
else:
a = "__TV_API -- create file -- | [{0}] - [{1}]\n"
a = a.format(diff, _filename)
print(a)
_ = __authenticate(DEMO)
package = _
with open(_filename, "w") as outfile:
json.dump(_, outfile)
print(_)
return package
def __authenticate(DEMO):
print("__TV_API -- __authenticate -- BEGIN\n")
#"deviceId":"{}".format(str(uuid.getnode())),
data = json.dumps({
"name":str(ACCOUNT),
"password":str(ACCOUNT_P),
"appId":str(APP_ID),
"appVersion":str(APP_VERSION),
"deviceId":str(DEVICE_ID),
"cid":int(CID),
"sec":str(CLIENT_SECRET)
})
_ = get_endpoint("auth/accesstokenrequest", data, DEMO)
print("__TV_API -- __authenticate -- END [{0}]\n".format(_))
return _
if __name__ == "__main__":
_ = __get_access_token()
token = _["accessToken"]
print(token)
There are a few major sections…
If you are mainly going to do get/post requests the function get_endpoint
will be helpful and it’s similar to the example javascript code.
Take note of how I create post request url, header and data to create a valid http post request.
The function __get_access_token relies on get_endpoint. get_access_token takes a variable DEMO to see if you’ll be making demo or live requests.
For simplicity sake, I have a file called accessToken in the same directory, this is similar to the storage mechanism in the javascript example. There are rate limits on the get_access_token endpoint and one access token is valid for about 4000 seconds.
The my_secrets file is very important and should not be shared publicly, the contents are below.
APP_ID = "xxx"
APP_VERSION = "xxx"
ACCOUNT = "xxx"
ACCOUNT_P = "xxx"
CID = "xxx"
CLIENT_SECRET = "xxx"
DEVICE_ID = "xxx"
Focus on the get_endpoint function to see how to create a http request with python’s requests module. Try to create simple http get and post requests. If they are still failing post what you have and the errors that you are getting and we can assist you further.
Best,