-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Pull Request Info ## Description This pull request integrates the Twitter Media Upload API and Tweet API, enabling users to publish videos from the studio page to Twitter. The media upload process is divided into three phases: - Initialize: Send media type, command, and byte size to receive a media_id. - Append Phase: Split the video file into base64 chunks (≤ 5MB each) and send them to Twitter. - Finalize: Complete the media upload and process the video. Closes (#782 ) ## Type of change - [x] New feature (non-breaking change which adds functionality) - [x] This change requires a documentation update ## Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published ## Screenshots (if appropriate): ## Additional context: Twitter's media upload limits apply: - Video duration: 0.5 seconds to 140 seconds (https://developer.x.com/en/docs/twitter-api/v1/media/upload-media/uploading-media/media-best-practices) - Refer to [Twitter Media Upload API documentation](https://developer.x.com/en/docs/twitter-api/v1/media/upload-media/api-reference/post-media-upload-init) for more details. --------- Co-authored-by: greatsamist <[email protected]>
- Loading branch information
1 parent
0c6f94e
commit 163471b
Showing
11 changed files
with
1,357 additions
and
1,122 deletions.
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
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
import { logger } from './logger'; | ||
import crypto from 'crypto'; | ||
import OAuth from 'oauth-1.0a'; | ||
import fs from 'fs'; | ||
import fetch from 'node-fetch'; | ||
import { config } from 'dotenv'; | ||
config(); | ||
|
||
function delay(ms: number): Promise<void> { | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} | ||
const TWITTER_UPLOAD_URL = 'https://upload.twitter.com/1.1/media/upload.json'; | ||
const TWEET_URL_V2 = 'https://api.twitter.com/2/tweets'; | ||
const oauth = new OAuth({ | ||
consumer: { | ||
key: process.env.TWITTER_OAUTH_KEY, | ||
secret: process.env.TWITTER_OAUTH_SECRET, | ||
}, | ||
signature_method: 'HMAC-SHA1', | ||
hash_function(baseString, key) { | ||
return crypto.createHmac('sha1', key).update(baseString).digest('base64'); | ||
}, | ||
}); | ||
|
||
function getAuthHeaders( | ||
token: { key: string; secret: string }, | ||
url: string, | ||
method: string, | ||
params?: {} | ||
) { | ||
const payload = { | ||
url: url, | ||
method: method, | ||
data: params, | ||
}; | ||
return oauth.toHeader( | ||
oauth.authorize(payload, { | ||
key: token.key, | ||
secret: token.secret, | ||
}) | ||
); | ||
} | ||
|
||
function readChunk( | ||
filePath: string, | ||
start: number, | ||
end: number | ||
): Promise<Buffer> { | ||
return new Promise((resolve, reject) => { | ||
const chunks: Buffer[] = []; | ||
const stream = fs.createReadStream(filePath, { start, end: end - 1 }); | ||
stream.on('data', (chunk) => chunks.push(Buffer.from(chunk))); | ||
stream.on('end', () => resolve(Buffer.concat(chunks))); | ||
stream.on('error', reject); | ||
}); | ||
} | ||
|
||
async function initializeUpload( | ||
filePath: string, | ||
token: { key: string; secret: string } | ||
): Promise<string> { | ||
const params = { | ||
command: 'INIT', | ||
total_bytes: fs.statSync(filePath).size.toString(), | ||
media_type: 'video/mp4', | ||
}; | ||
const body = new URLSearchParams(params); | ||
const authHeader = getAuthHeaders(token, TWITTER_UPLOAD_URL, 'POST', params); | ||
const response = await fetch(TWITTER_UPLOAD_URL, { | ||
method: 'POST', | ||
body: body, | ||
headers: { | ||
...authHeader, | ||
'content-type': 'application/x-www-form-urlencoded', | ||
}, | ||
}); | ||
const data = await response.json(); | ||
return data.media_id_string; | ||
} | ||
|
||
async function uploadVideoChunks( | ||
filePath: string, | ||
mediaId: string, | ||
fileSize: number, | ||
token: { key: string; secret: string } | ||
) { | ||
const chunkSize = 5 * 1024 * 1024; // 5 MB in bytes | ||
let segmentIndex = 0; | ||
|
||
for (let start = 0; start < fileSize; start += chunkSize, segmentIndex++) { | ||
const end = Math.min(start + chunkSize, fileSize); | ||
const chunk = await readChunk(filePath, start, end); | ||
const base64Chunk = chunk.toString('base64'); | ||
const params = { | ||
command: 'APPEND', | ||
media_id: mediaId, | ||
media_data: base64Chunk, | ||
segment_index: segmentIndex.toString(), | ||
}; | ||
const queryParams = new URLSearchParams(params); | ||
const headers = getAuthHeaders(token, TWITTER_UPLOAD_URL, 'POST', params); | ||
const response = await fetch(TWITTER_UPLOAD_URL, { | ||
method: 'POST', | ||
body: queryParams, | ||
headers: { | ||
...headers, | ||
}, | ||
}); | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
} | ||
} | ||
|
||
async function finalizeUpload( | ||
mediaId: string, | ||
token: { key: string; secret: string } | ||
) { | ||
let status = 'pending'; | ||
do { | ||
const params = { | ||
command: 'FINALIZE', | ||
media_id: mediaId, | ||
}; | ||
const body = new URLSearchParams(params); | ||
const authHeader = getAuthHeaders( | ||
token, | ||
TWITTER_UPLOAD_URL, | ||
'POST', | ||
params | ||
); | ||
const response = await fetch(TWITTER_UPLOAD_URL, { | ||
method: 'POST', | ||
body: body, | ||
headers: { | ||
...authHeader, | ||
}, | ||
}); | ||
const data = await response.json(); | ||
status = data.processing_info.state; | ||
if (status === 'succeeded') { | ||
logger.info('Media processing succedded'); | ||
} else if (status === 'failed') { | ||
throw new Error('Media processing failed'); | ||
} else { | ||
logger.info( | ||
'Media processing is still pending, waiting for completion...' | ||
); | ||
await delay(data.processing_info.check_after_secs * 1000); | ||
} | ||
} while (status !== 'succeeded'); | ||
} | ||
|
||
async function tweetWithMediaV2( | ||
mediaId: string, | ||
text: string, | ||
token: { key: string; secret: string } | ||
) { | ||
try { | ||
const authHeader = getAuthHeaders(token, TWEET_URL_V2, 'POST'); | ||
const response = await fetch(TWEET_URL_V2, { | ||
method: 'POST', | ||
headers: { | ||
...authHeader, | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
text: text, | ||
media: { | ||
media_ids: [mediaId], | ||
}, | ||
}), | ||
}); | ||
const data = await response.json(); | ||
} catch (e) { | ||
logger.error(e.message); | ||
} | ||
} | ||
|
||
export async function uploadToTwitter( | ||
session: { | ||
name: string; | ||
description: string; | ||
slug: string; | ||
published: boolean; | ||
coverImage: string; | ||
}, | ||
videoFilePath: string, | ||
token: { key: string; secret: string } | ||
): Promise<void> { | ||
const fileSize = fs.statSync(videoFilePath).size; | ||
const mediaId = await initializeUpload(videoFilePath, token); | ||
await uploadVideoChunks(videoFilePath, mediaId, fileSize, token); | ||
await finalizeUpload(mediaId, token); | ||
await tweetWithMediaV2(mediaId, session.name, token); | ||
} |
Oops, something went wrong.