gpt4 book ai didi

node.js - Twit ,直接从 url 上传媒体

转载 作者:太空宇宙 更新时间:2023-11-03 22:29:01 27 4
gpt4 key购买 nike

我想从某个网址下载一个 img 并将其直接上传到 Twitter,而不先将其保存到文件中。

到目前为止,我已经测试了网上找到的所有方法,但没有成功。我总是收到“媒体类型无法识别”错误。

  http.get("http://localhost:9000/screenshot/capture/" + shot.slug,
function(response){
if (response.statusCode == 200) {

response.setEncoding('binary');
var imageFile = "";
response.on('data', function(chunk){
imageFile += chunk;
});

response.on('end', function(){
imageFile = imageFile;
// first we must post the media to Twitter
T.post('media/upload', {media_data: imageFile.toString('base64')}, function (err, media, response) {
if(err) return console.log("ERRR2: ", err);
console.log("DATA: ", media);
console.log("RESPONSE: ", response);
// now we can assign alt text to the media, for use by screen readers and
// other text-based presentations and interpreters
var mediaIdStr = media.media_id;
var altText = "Alt text for the shot";
var meta_params = { media_id: mediaIdStr, alt_text: { text: altText } };
console.log(meta_params);

T.post('media/metadata/create', meta_params, function (err, data, response) {
if (!err) {
// now we can reference the media and post a tweet (media will attach to the tweet)
var params = { status: 'by @' + twitter_handle + ' at ' + "http://localhost:9000/" + shot.slug, media_ids: [mediaIdStr] };

T.post('statuses/update', params, function (err, data, response) {
console.log("DATA: ", data);
})
}
});
});
})
//console.log(image);
}

Twit 可以工作。我可以发送状态更新并自始至终使用 API。另外,我可以先将媒体文件保存到磁盘,然后使用 fs 读取它,然后发送它。但我想从 url 获取它作为二进制文件并以某种方式将其上传到 Twitter。

我怎样才能实现这个目标?

最佳答案

您将必须使用流:https://nodejs.org/api/stream.html或更具体地说:https://github.com/request/request#streaming

特别是,类似

request('http://google.com/doodle.png').pipe(T.postMediaChunked)

不过,执行写入磁盘的中间步骤没有问题,您只需在之后处理清理即可。

关于node.js - Twit ,直接从 url 上传媒体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40562851/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com