twitter-api-client
Version:
Node.js / JavaScript client for Twitter API
179 lines (178 loc) • 10.7 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var utils_1 = require("../base/utils");
var MediaClient = /** @class */ (function () {
function MediaClient(transport) {
if (!transport) {
throw Error('Transport class needs to be provided.');
}
this.transport = transport;
}
/**
* The INIT command request is used to initiate a file upload session. It returns a media_id which should be used to execute all subsequent requests. The next step after a successful return from INIT command is the APPEND command. See the Uploading media guide for constraints and requirements on media files.
*
* @link https://developer.twitter.com/en/docs/twitter-api/v1/media/upload-media/api-reference/post-media-upload-init
* @param parameters
*/
MediaClient.prototype.mediaUploadInit = function (parameters) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.transport.doPostRequest('https://upload.twitter.com/1.1/media/upload.json', parameters)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* The APPEND command is used to upload a chunk (consecutive byte range) of the media file. For example, a 3 MB file could be split into 3 chunks of size 1 MB, and uploaded using 3 APPEND command requests. After the entire file is uploaded, the next step is to call the FINALIZE command.
*
* @link https://developer.twitter.com/en/docs/twitter-api/v1/media/upload-media/api-reference/post-media-upload-append
* @param parameters
*/
MediaClient.prototype.mediaUploadAppend = function (parameters) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.transport.doPostRequest('https://upload.twitter.com/1.1/media/upload.json', parameters)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* The STATUS command is used to periodically poll for updates of media processing operation. After the STATUS command response returns succeeded, you can move on to the next step which is usually create Tweet with media_id.
*
* @link https://developer.twitter.com/en/docs/twitter-api/v1/media/upload-media/api-reference/get-media-upload-status
* @param parameters
*/
MediaClient.prototype.mediaUploadStatus = function (parameters) {
return __awaiter(this, void 0, void 0, function () {
var params;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
params = utils_1.createParams(parameters);
return [4 /*yield*/, this.transport.doGetRequest('https://upload.twitter.com/1.1/media/upload.json' + params)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* The FINALIZE command should be called after the entire media file is uploaded using APPEND commands. If and (only if) the response of the FINALIZE command contains a processing_info field, it may also be necessary to use a STATUS command and wait for it to return success before proceeding to Tweet creation.
*
* @link https://developer.twitter.com/en/docs/twitter-api/v1/media/upload-media/api-reference/post-media-upload-finalize
* @param parameters
*/
MediaClient.prototype.mediaUploadFinalize = function (parameters) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.transport.doPostRequest('https://upload.twitter.com/1.1/media/upload.json', parameters)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Use this endpoint to upload images to Twitter. This endpoint returns a media_id by default and can optionally return a media_key when a media_category is specified. These values are used by Twitter endpoints that accept images. For example, a media_id value can be used to create a Tweet with an attached photo using the POST statuses/update endpoint. All Ads API endpoints require a media_key. For example, a media_key value can be used to create a Draft Tweet with a photo using the POST accounts/:account_id/draft_tweets endpoint.
*
* @link https://developer.twitter.com/en/docs/twitter-api/v1/media/upload-media/api-reference/post-media-upload
* @param parameters
*/
MediaClient.prototype.mediaUpload = function (parameters) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.transport.doPostRequest('https://upload.twitter.com/1.1/media/upload.json', parameters)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* This endpoint can be used to provide additional information about the uploaded media_id. This feature is currently only supported for images and GIFs.
*
* @link https://developer.twitter.com/en/docs/twitter-api/v1/media/upload-media/api-reference/post-media-metadata-create
* @param parameters
*/
MediaClient.prototype.mediaMetadataCreate = function (parameters) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.transport.doPostRequest('https://upload.twitter.com/1.1/media/metadata/create.json', parameters, 'application/json')];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Use this endpoint to dissociate subtitles from a video and delete the subtitles. You can dissociate subtitles from a video before or after Tweeting.
*
* @link https://developer.twitter.com/en/docs/twitter-api/v1/media/upload-media/api-reference/post-media-subtitles-delete
*/
MediaClient.prototype.mediaSubtitlesDelete = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.transport.doPostRequest('https://upload.twitter.com/1.1/media/subtitles/delete.json')];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Use this endpoint to associate uploaded subtitles to an uploaded video. You can associate subtitles to video before or after Tweeting. Request flow for associating subtitle to video before the video is Tweeted : 1. Upload video using the chunked upload endpoint and get the video media_id. 2. Upload subtitle using the chunked upload endpoint with media category set to “Subtitles” and get the subtitle media_id. 3. Call this endpoint to associate the subtitle to the video. 4. Create Tweet with the video media_id.
*
* @link https://developer.twitter.com/en/docs/twitter-api/v1/media/upload-media/api-reference/post-media-subtitles-create
*/
MediaClient.prototype.mediaSubtitlesCreate = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.transport.doPostRequest('https://upload.twitter.com/1.1/media/subtitles/create.json')];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
return MediaClient;
}());
exports.default = MediaClient;