facebook-api-video-upload
Advanced tools
Comparing version 1.4.1 to 2.0.0
157
index.js
@@ -1,103 +0,94 @@ | ||
'use strict'; | ||
const Promise = require('bluebird'); | ||
const streamToPromise = require('stream-to-promise'); | ||
const rp = require('request-promise'); | ||
const streamToPromise = require('stream-to-promise') | ||
const rp = require('request-promise') | ||
const fs = require('fs') | ||
const url = 'https://graph-video.facebook.com'; | ||
const retryMax = 10; | ||
let retry = 0; | ||
const url = 'https://graph-video.facebook.com' | ||
const version = 'v3.2' | ||
const retryMax = 10 | ||
let retry = 0 | ||
function apiInit(args, videoSize) { | ||
const options = { | ||
method: 'POST', | ||
uri: `${url}/v2.6/${args.id}/videos?access_token=${args.token}`, | ||
json: true, | ||
form: { | ||
upload_phase: 'start', | ||
file_size: videoSize | ||
} | ||
}; | ||
const options = { | ||
method: 'POST', | ||
uri: `${url}/${version}/${args.id}/videos?access_token=${args.token}`, | ||
json: true, | ||
form: { | ||
upload_phase: 'start', | ||
file_size: videoSize | ||
} | ||
} | ||
return rp(options) | ||
.then(res => { | ||
return res; | ||
}); | ||
return rp(options).then(res => res) | ||
} | ||
function apiFinish(args, id, video_id) { | ||
const videoTitle = args.title || ''; | ||
const description = args.description || ''; | ||
const options = { | ||
method: 'POST', | ||
uri: `${url}/v2.6/${args.id}/videos`, | ||
form: { | ||
access_token: args.token, | ||
upload_phase: 'finish', | ||
upload_session_id: id, | ||
title: videoTitle, | ||
description: description | ||
}, | ||
json: true | ||
}; | ||
function apiFinish(args, upload_session_id, video_id) { | ||
const {token, id, stream, ...extraParams} = args | ||
return rp(options) | ||
.then(res => { | ||
res.video_id = video_id; | ||
return res; | ||
}); | ||
const options = { | ||
method: 'POST', | ||
json: true, | ||
uri: `${url}/${version}/${args.id}/videos`, | ||
formData: { | ||
...extraParams, | ||
upload_session_id, | ||
access_token: args.token, | ||
upload_phase: 'finish', | ||
} | ||
} | ||
return rp(options) | ||
.then(res => ({...res, video_id})) | ||
} | ||
function uploadChunk(args, id, start, chunk) { | ||
const formData = { | ||
access_token: args.token, | ||
upload_phase: 'transfer', | ||
start_offset: start, | ||
upload_session_id: id, | ||
video_file_chunk: { | ||
value: chunk, | ||
options: { | ||
filename: 'chunk' | ||
} | ||
} | ||
}; | ||
const options = { | ||
method: 'POST', | ||
uri: `${url}/v2.6/${args.id}/videos`, | ||
formData: formData, | ||
json: true | ||
}; | ||
const formData = { | ||
access_token: args.token, | ||
upload_phase: 'transfer', | ||
start_offset: start, | ||
upload_session_id: id, | ||
video_file_chunk: { | ||
value: chunk, | ||
options: { | ||
filename: 'chunk' | ||
} | ||
} | ||
} | ||
const options = { | ||
method: 'POST', | ||
uri: `${url}/${version}/${args.id}/videos`, | ||
formData: formData, | ||
json: true | ||
} | ||
return rp(options) | ||
.then(res => { | ||
retry = 0; | ||
return res; | ||
}) | ||
.catch(err => { | ||
if (retry++ >= retryMax) { | ||
return err; | ||
} | ||
return uploadChunk(args, id, start, chunk); | ||
}); | ||
return rp(options) | ||
.then(res => { | ||
retry = 0 | ||
return res | ||
}) | ||
.catch(err => { | ||
if (retry++ >= retryMax) { | ||
return err | ||
} | ||
return uploadChunk(args, id, start, chunk) | ||
}) | ||
} | ||
function uploadChain(buffer, args, res, ids) { | ||
if (res.start_offset === res.end_offset) { | ||
return ids; | ||
} | ||
if (res.start_offset === res.end_offset) { | ||
return ids | ||
} | ||
var chunk = buffer.slice(res.start_offset, res.end_offset); | ||
return uploadChunk(args, ids[0], res.start_offset, chunk) | ||
.then(res => uploadChain(buffer, args, res, ids)); | ||
var chunk = buffer.slice(res.start_offset, res.end_offset) | ||
return uploadChunk(args, ids[0], res.start_offset, chunk) | ||
.then(res => uploadChain(buffer, args, res, ids)) | ||
} | ||
function facebookApiVideoUpload(args) { | ||
return streamToPromise(args.stream) | ||
.then(buffer => [buffer, apiInit(args, buffer.length)]) | ||
.spread((buffer, res) => { | ||
const ids = [res.upload_session_id, res.video_id]; | ||
return uploadChain(buffer, args, res, ids); | ||
}) | ||
.spread((id, video_id) => apiFinish(args, id, video_id)); | ||
return streamToPromise(args.stream) | ||
.then(buffer => Promise.all([buffer, apiInit(args, buffer.length)])) | ||
.then(([buffer, res]) => uploadChain(buffer, args, res, [res.upload_session_id, res.video_id])) | ||
.then(([id, video_id]) => apiFinish(args, id, video_id)) | ||
} | ||
module.exports = facebookApiVideoUpload; | ||
module.exports = facebookApiVideoUpload |
{ | ||
"name": "facebook-api-video-upload", | ||
"version": "1.4.1", | ||
"version": "2.0.0", | ||
"description": "A handy function to upload video in chunk on the facebook graph.", | ||
@@ -9,2 +9,3 @@ "license": "MIT", | ||
"scripts": { | ||
"fixture": "youtube-dl https://www.youtube.com/watch?v=NrB35qqOyhU -o 'fixture.%(ext)s' --format mp4 --write-thumbnail", | ||
"test": "" | ||
@@ -30,10 +31,6 @@ }, | ||
"dependencies": { | ||
"bluebird": "^3.4.0", | ||
"request-promise": "^3.0.0", | ||
"stream-to-promise": "^2.1.1" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.15.2", | ||
"nock": "^8.0.0" | ||
} | ||
"devDependencies": {} | ||
} |
@@ -1,3 +0,1 @@ | ||
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo) | ||
# facebook-api-video-upload | ||
@@ -18,18 +16,27 @@ > Upload a video in chunk on the facebook api. [more info](https://developers.facebook.com/docs/graph-api/video-uploads) | ||
const args = { | ||
token: yourtoken, // with the permission to upload | ||
id: yourid, //The id represent {page_id || user_id || event_id || group_id} | ||
stream: fs.createReadStream('./test/fixture/fixture.mp4'), //path to the video, | ||
title: "my video", | ||
description: "my description" | ||
token: "YOURTOKEN", // with the permission to upload | ||
id: "YOURID", //The id represent {page_id || user_id || event_id || group_id} | ||
stream: fs.createReadStream('./fixture.mp4'), //path to the video, | ||
title: "my video", | ||
description: "my description", | ||
thumb: { | ||
value: fs.createReadStream('./fixture.jpg'), | ||
options: { | ||
filename: 'fixture.jpg', | ||
contentType: 'image/jpg' | ||
} | ||
} | ||
// if you want the default thumb from the video just remove the field | ||
// you can add any extra fields from the api https://developers.facebook.com/docs/graph-api/reference/page/videos/#Creating | ||
// all keys except token, id, stream are passed to the final request | ||
}; | ||
fbUpload(args).then((res) => { | ||
console.log('res: ', res); | ||
//res: { success: true, video_id: '1838312909759132' } | ||
console.log('res: ', res); | ||
//res: { success: true, video_id: '1838312909759132' } | ||
}).catch((e) => { | ||
console.error(e); | ||
}); | ||
``` | ||
console.error(e); | ||
});``` | ||
## License | ||
MIT © [MrdotB](https://github.com/MRdotB) |
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
6335
2
0
106
42
2
- Removedbluebird@^3.4.0