from os import environ
from os.path import abspath, dirname, join
from smart_prospective_api import SPApi, APIError
import json

PUBLIC_KEY = "pub_XXXXX"
PRIVATE_KEY = "sec_XXXXX"


# Optionnal / Utils method (only for the test)
def random_string(length):
    from random import choices
    from string import ascii_lowercase
    return ''.join(choices(ascii_lowercase, k=length))


try:
    # Setup the API credencial (do not perform any request at this moment)
    # Note: The public key starts by "pub_" & The secret key starts by "sec_"
    sp_api = SPApi(PUBLIC_KEY, PRIVATE_KEY)
    
    # EDIT
    media = sp_api.get_medias()[0] # Get the just created Media

    # ADD
    edit_media = sp_api.edit_media(media["code"], name=random_string(8), file=join(dirname(abspath(__file__)), "image.png"), materials=[
                                 m["code"] for m in sp_api.get_materials()], tags=["TestAPI", "Local"])  # Update a media
		# Audio
    #edit_media = sp_api.edit_media(media["code"], name=random_string(8), file="/home/delbos-d/Music/Train - Hey, soul sister.mp3")  # Update a media
    # Instagram
    #edit_media = sp_api.edit_media(media["code"], name=random_string(8), post_accounts=["@selenagomez", "@itsdougthepug"])  # Update a media
    # Twitter
    #edit_media = sp_api.edit_media(media["code"], name=random_string(8), post_accounts=["@BarackObama", "@Youtube"])  # Update a media
    # RSS
    #edit_media = sp_api.edit_media(media["code"], name=random_string(8), post_accounts=[
    #                             "https://www.asiaflash.com/horoscope/rss_horojour_poissons.xml", "https://www.ouest-france.fr/rss-en-continu.xml?tid=75258"])  # Update a media
    # Youtube
    #edit_media = sp_api.edit_media(media["code"], name=random_string(8), url="https://www.youtube.com/watch?v=ReEgXh-wURs", keep_audio=True)  # Update a media
    # Web
    #edit_media = sp_api.edit_media(media["code"], name=random_string(8), url="https://smartprospective.com")  # Update a media
    # Template
    # Note: webview_details contains all the value (json) to fill the template
    #edit_media = sp_api.edit_media(media["code"], name=random_string(8), webview_details=json.dumps(
    #    {"text1": "Value1", "text2": "Value2"}), webviewtemplate=sp_api.get_webviewtemplates()[0]["code"])  # Update a media
    # Note: If in webview_details you need to give a file, as value of a key use sp_api.upload_media_template_file(file="...absolute path...")["url"]
    
    print(f"A media has been updated: {edit_media['name']}")
    
    sp_api.logout()  # Logout the account, more safe to use it, to avoid potential attacks
except APIError as e:
    print(f"Failure using the Smart Prospective API: {e}")
curl --location --request POST 'https://app.smartprospective.com/api/medias/edit/<MEDIA_CODE>' \
--form 'token="XXXXXX"' \
--form 'name="name_of_the_media"' \
# If File Media (need to specify the path where the file is)
--form 'file="path_file"' \
# If Instagram, Twitter or RSS 
--form 'post_accounts=["@selenagomez", "@itsdougthepug"]' \
# If Youtube
--form 'url="https://www.youtube.com/watch?v=ReEgXh-wURs"' \
--form 'keep_audio=True' \
# If Web
--form 'url="https://smartprospective.com"' \
# If WebviewTemplate
--form 'webview_details={"text1": "Value1", "text2": "Value2"}' # Json
--form 'webviewtemplate=<WEBVIEWTEMPLATE_CODE>' # 

# replace <MEDIA_CODE> with the actual media code (If Get medias)
# replace <WEBVIEWTEMPLATE_CODE> with the actual webviewtemplate code (If Get webviewtemplates)