Create a media

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)

    # ADD
    new_media = sp_api.add_media(category="file", name=random_string(8), file=join(dirname(abspath(__file__)), "image.png"), materials=[
                                 m["code"] for m in sp_api.get_materials()], tags=["TestAPI", "Local"])  # Create a new media (you will be the creator)
		# Audio
    #new_media = sp_api.add_media(category="audio", name=random_string(8), file="/home/delbos-d/Music/Train - Hey, soul sister.mp3")  # Create a new media (you will be the creator)
    # Instagram
    #new_media = sp_api.add_media(category="instagram", name=random_string(8), post_accounts=["@selenagomez", "@itsdougthepug"])  # Create a new media (you will be the creator)
    # Twitter
    #new_media = sp_api.add_media(category="twitter", name=random_string(8), post_accounts=["@BarackObama", "@Youtube"])  # Create a new media (you will be the creator)
    # RSS
    #new_media = sp_api.add_media(category="rss", 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"])  # Create a new media (you will be the creator)
    # Youtube
    #new_media = sp_api.add_media(category="youtube", name=random_string(8), url="https://www.youtube.com/watch?v=ReEgXh-wURs", keep_audio=True)  # Create a new media (you will be the creator)
    # Web
    #new_media = sp_api.add_media(category="web", name=random_string(8), url="https://smartprospective.com")  # Create a new media (you will be the creator)
    # Template
    # Note: webview_details contains all the value (json) to fill the template
    #new_media = sp_api.add_media(category="template", name=random_string(8), webview_details=json.dumps(
    #    {"text1": "Value1", "text2": "Value2"}), webviewtemplate=sp_api.get_webviewtemplates()[0]["code"])  # Create a new media (you will be the creator)
    # 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 new media has been created: {new_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}")
# Media category: file, audio, twitter, rss, youtube, web, template
curl --location --request POST 'https://app.smartprospective.com/api/medias/add/<MEDIA_CATEGORY>' \
--form 'token="XXXXXX"' \
--form 'name="Media Name"' \
--form 'description="Media Description"' \
--form 'tags=["TestAPI", "Local"]' \
# Give the Material Group is Optionnal & no limit in the list size
--form 'materials=["MATERIAL_CODE"]' \
# Give the Material Group is Optionnal & no limit in the list size
--form 'material_groups=["MATERIAL_GROUP_CODE"]' \
# Give the Buildings is Optionnal & no limit in the list size
--form 'buildings=["BUILDING_CODE"]' \
# If File Media (need to specify the code of the file. You can get the code of a file when you upload it (See below for curl of upload))
--form 'file_input=<FILE_CODE>' \
# If 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>' # 


# UPLOAD A FILE
curl --location --request POST 'https://app.smartprospective.com/api/medias/upload' \
--form 'token="XXXXXX"' \
--form 'file=@path_to_your_file'

The media category value are:

  • "file"
  • "audio"
  • "twitter"
  • "rss"
  • "youtube"
  • "web"
  • "template"

Not working:

  • "meteo" (not ready)
  • "traffic" (not ready)
  • "facebook" (not api ready)
  • "instagram" (not api ready)
  • "tiktok" (not api ready)
  • "canva" (not allowed by canva for now)