Create a Post with YouTube Video in Lead Media ============================================== This tutorial explains how to create an article with a YouTube video in lead media spot. .. include:: includes/requirements.rst Procedure --------- .. include:: includes/initialize-variables.rst 2. Obtain a shortcode using a YouTube video URL *********************************************** This code snippet uses the :doc:`/api/reference/scraper` to obtain a shortcode and URL that can be embedded. .. code:: python youtube_video_url = 'https://www.youtube.com/watch?v=_KcjSQ0gjFs' response = requests.get( 'https://{}/api/1.3/scraper'.format(API_DOMAIN), params={ 'api_key': API_KEY, 'url': youtube_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 and URL to be embedded, create a new draft using the :doc:`/api/reference/drafts` with the video in the lead media position. .. code:: python response = requests.post( 'https://{}/api/1.3/drafts'.format(API_DOMAIN), params={'api_key': API_KEY}, json={ 'headline': 'Article with YouTube video in lead media', 'body': '

This is a HTML piece.

', '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. .. code:: python 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'])