Create a Post with JW Player Video in Lead Media

This tutorial explains how to create an article with a JW Player video in the lead media spot.

Requirements

For this tutorial you will need both the Python and requests libraries installed.

Procedure

1. Initialize variables

Set the global variables.

import requests
API_DOMAIN = '<your-secure-domain>'
API_KEY = '<your-api-key>'

2. Obtain a shortcode using a JW Player video URL

This code snippet uses the Scraper API to obtain a shortcode and a URL that can be embedded.

jwplayer_video_url = 'https://content.jwplatform.com/players/Iaud9pIL.js'
response = requests.get(
    'https://{}/api/1.3/scraper'.format(API_DOMAIN),
    params={
        'api_key': API_KEY,
        'url': jwplayer_video_url,
    }
)
response.raise_for_status()
scraper_response = response.json()
print(scraper_response['embed']['shortcode'])
print(scraper_response['videos'][0]['embed_url'])

3. Create a draft

Once you have the shortcode as well as the URL that needs to be embedded, create a new draft using the Drafts API and place the video in the lead media spot.

response = requests.post(
     'https://{}/api/1.3/drafts'.format(API_DOMAIN),
    params={'api_key': API_KEY},
    json={
        'headline': 'Article with JW Player video in lead media',
        'body': '<p>This is a HTML piece.</p>',
        'media': scraper_response['embed']['shortcode'],
        'video': scraper_response['videos'][0]['embed_url'],
        'image_external': scraper_response['videos'][0]['thumbnail_url'],
    }
)
response.raise_for_status()
draft_api_response = response.json()
print(draft_api_response['id'])

4. Publish the draft

Finally, publish the draft and print its URL.

response = requests.put(
    'https://{}/api/1.3/drafts/{}'.format(API_DOMAIN, draft_api_response['id']),
    params={'api_key': API_KEY},
    json={'action': 'publish'}
)
response.raise_for_status()
draft_api_response = response.json()
print(draft_api_response['post_url'])

Choosing a Video Player

Optionally, you can set a specific player for the video by sending an additional argument in the video URL. This will need to be added right after the video ID and prefixed by a colon.

For example, if you want to set the player with ID "FvQKszTI" in the example above, then the URL will be the following:

https://content.jwplatform.com/players/Iaud9pIL-FvQKszTI.js

5. Obtain a new shortcode using a JW Player video URL that includes a Player ID

This code snippet uses the Scraper API to obtain a shortcode and URL that can be embedded.

jwplayer_video_url = 'https://content.jwplatform.com/players/Iaud9pIL-FvQKszTI.js'
response = requests.get(
    'https://{}/api/1.3/scraper'.format(API_DOMAIN),
    params={
        'api_key': API_KEY,
        'url': jwplayer_video_url,
    }
)
response.raise_for_status()
scraper_response = response.json()
print(scraper_response['embed']['shortcode'])
print(scraper_response['videos'][0]['embed_url'])

6. Edit published article

response = requests.put(
    'https://{}/api/1.3/posts/{}'.format(API_DOMAIN, draft_api_response['id']),
    params={'api_key': API_KEY},
    json={
        'headline': 'Article with JW Player video in lead media',
        'media': scraper_response['embed']['shortcode'],
        'video': scraper_response['videos'][0]['embed_url'],
        'image_external': scraper_response['videos'][0]['thumbnail_url'],
    }
)
response.raise_for_status()
posts_api_response = response.json()
print(draft_api_response['post_url'])