-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New process to upload a broadcast replay to IGTV #1364
Open
omouren
wants to merge
3
commits into
dilame:master
Choose a base branch
from
omouren:hotfix/new-post-live-to-igtv
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* tslint:disable:no-console */ | ||
import {IgApiClient, LiveEntity} from '../src'; | ||
import Bluebird = require('bluebird'); | ||
const pngToJpeg = require('png-to-jpeg') | ||
const sharp = require('sharp'); | ||
const axios = require('axios'); | ||
|
||
const ig = new IgApiClient(); | ||
|
||
async function login() { | ||
ig.state.generateDevice(process.env.IG_USERNAME); | ||
ig.state.proxyUrl = process.env.IG_PROXY; | ||
await ig.account.login(process.env.IG_USERNAME, process.env.IG_PASSWORD); | ||
} | ||
|
||
(async () => { | ||
// basic login-procedure | ||
await login(); | ||
|
||
const {broadcast_id, upload_url} = await ig.live.create({ | ||
// create a stream in 720x1280 (9:16) | ||
previewWidth: 720, | ||
previewHeight: 1280, | ||
// this message is not necessary, because it doesn't show up in the notification | ||
message: 'My message', | ||
}); | ||
// (optional) get the key and url for programs such as OBS | ||
const {stream_key, stream_url} = LiveEntity.getUrlAndKey({broadcast_id, upload_url}); | ||
console.log(`Start your stream on ${stream_url}.\n | ||
Your key is: ${stream_key}`); | ||
|
||
/** | ||
* make sure you are streaming to the url | ||
* the next step will send a notification / start your stream for everyone to see | ||
*/ | ||
const startInfo = await ig.live.start(broadcast_id); | ||
// status should be 'ok' | ||
console.log(startInfo); | ||
|
||
/** | ||
* now, your stream is running | ||
* the next step is to get comments | ||
* note: comments can only be requested roughly every 2s | ||
*/ | ||
|
||
// initial comment-timestamp = 0, get all comments | ||
let lastCommentTs = await printComments(broadcast_id, 0); | ||
|
||
// enable the comments | ||
await ig.live.unmuteComment(broadcast_id); | ||
/** | ||
* wait 2 seconds until the next request. | ||
* in the real world you'd use something like setInterval() instead of Bluebird.delay() / just to simulate a delay | ||
*/ | ||
// wait 2s | ||
await Bluebird.delay(2000); | ||
// now, we print the next comments | ||
lastCommentTs = await printComments(broadcast_id, lastCommentTs); | ||
|
||
// now we're commenting on our stream | ||
await ig.live.comment(broadcast_id, 'A comment'); | ||
|
||
/** | ||
* now, your stream is running, you entertain your followers, but you're tired and | ||
* we're going to stop the stream | ||
*/ | ||
await ig.live.endBroadcast(broadcast_id); | ||
|
||
// Get live thumbnails, required to post on IGTV | ||
let data = await ig.live.getPostLiveThumbnails(broadcast_id); | ||
|
||
// Use an HTTP client to download any thumb | ||
let {data: file} = await axios.get(data.thumbnails[0], {responseType: 'arraybuffer'}); | ||
|
||
// (optional) Resize thumb to a vertical one and convert to jpg | ||
file = await sharp(file) | ||
.resize({width: 720, height: 1280}) | ||
.jpeg({ | ||
quality: 100, | ||
}) | ||
.toBuffer(); | ||
|
||
// Upload the thumbnail with a broadcast id for a replay and get uploadId | ||
let upload = await ig.upload.photo({file, broadcastId: broadcast_id}); | ||
|
||
let igtv = await ig.media.configureToIgtv({ | ||
upload_id: upload.upload_id, | ||
title: 'A title', | ||
caption: 'A description', | ||
igtv_share_preview_to_feed: '1', | ||
}, 2000) | ||
|
||
console.log(`Live posted to IGTV : ${igtv.upload_id}`)); | ||
// now you're basically done | ||
})(); | ||
|
||
async function printComments(broadcastId, lastCommentTs) { | ||
const {comments} = await ig.live.getComment({broadcastId, lastCommentTs}); | ||
if (comments.length > 0) { | ||
comments.forEach(comment => console.log(`${comment.user.username}: ${comment.text}`)); | ||
return comments[comments.length - 1].created_at; | ||
} else { | ||
return lastCommentTs; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This variable doesn't need to be here. It can live in the
while
body.