@@ -19,9 +19,10 @@ import {
1919 UploadVideoOptions ,
2020} from '../types' ;
2121import { PostingLocation , PostingStoryOptions } from '../types/posting.options' ;
22- import { IgConfigureVideoError , IgResponseError , IgUploadVideoError } from '../errors' ;
22+ import { IgConfigureVideoError , IgResponseError , IgUploadLiveIgtvError , IgUploadVideoError } from '../errors' ;
2323import { StatusResponse , UploadRepositoryVideoResponseRootObject } from '../responses' ;
2424import { PostingIgtvOptions } from '../types/posting.igtv.options' ;
25+ import { PostingLiveIgtvOptions } from "../types/posting.live-igtv.options" ;
2526import sizeOf = require( 'image-size' ) ;
2627import Bluebird = require( 'bluebird' ) ;
2728import Chance = require( 'chance' ) ;
@@ -52,6 +53,22 @@ export class PublishService extends Repository {
5253 } ;
5354 }
5455
56+ /**
57+ * @param transcodeDelayInMs The delay for instagram to transcode the video
58+ */
59+ public static catchLiveIgtvTranscodeError ( transcodeDelayInMs : number ) {
60+ return error => {
61+ if ( error . response . statusCode === 202 ) {
62+ PublishService . publishDebug (
63+ `Received trancode error: ${ JSON . stringify ( error . response . body ) } , waiting ${ transcodeDelayInMs } ms` ,
64+ ) ;
65+ return Bluebird . delay ( transcodeDelayInMs ) ;
66+ } else {
67+ throw new IgUploadLiveIgtvError ( error . response as IgResponse < UploadRepositoryVideoResponseRootObject > ) ;
68+ }
69+ } ;
70+ }
71+
5572 /**
5673 * Gets duration in ms, width and height info for a video in the mp4 container
5774 * @param buffer Buffer, containing the video-file
@@ -453,6 +470,39 @@ export class PublishService extends Repository {
453470 }
454471 }
455472
473+ public async liveIgtv ( options : PostingLiveIgtvOptions ) {
474+ const uploadedPhoto = await this . client . upload . photo ( {
475+ file : options . file ,
476+ broadcastId : options . broadcastId ,
477+ } ) ;
478+
479+ await Bluebird . try ( ( ) =>
480+ this . client . media . uploadFinish ( {
481+ upload_id : uploadedPhoto . upload_id ,
482+ source_type : '4' ,
483+ } ) ,
484+ ) . catch ( IgResponseError , PublishService . catchLiveIgtvTranscodeError ( options . transcodeDelay || 5000 ) ) ;
485+
486+ const configureOptions : MediaConfigureToIgtvOptions = {
487+ upload_id : uploadedPhoto . upload_id ,
488+ title : options . title ,
489+ caption : options . caption ,
490+ igtv_share_preview_to_feed : options . igtv_share_preview_to_feed ,
491+ length : 0
492+ } ;
493+
494+ for ( let i = 0 ; i < 6 ; i ++ ) {
495+ try {
496+ return await this . client . media . configureToIgtv ( configureOptions ) ;
497+ } catch ( e ) {
498+ if ( i >= 5 || e . response . statusCode >= 400 ) {
499+ throw new IgConfigureVideoError ( e . response , configureOptions ) ;
500+ }
501+ await Bluebird . delay ( ( i + 1 ) * 2 * 1000 ) ;
502+ }
503+ }
504+ }
505+
456506 private async regularVideo ( options : UploadVideoOptions ) {
457507 options = defaults ( options , {
458508 uploadId : Date . now ( ) ,
0 commit comments