如何获取twitter api的url图片并将其放在googlesheet中



我的代码在appscript中使用带有fetch的GET调用,然后fetch给我一个嵌套了不同数据的数组,我的代码破坏数据以获得我需要的数据,并放入一个新数组,每个数组都有每个tweet的数据,然后这个新数组被推送到mainArray中,依此类推,我使用SetValues将每个arrayData放在每一行中,并在谷歌表单的文档中用列分隔每个数据

该代码适用于链接、推特、用户名、日期、点赞、评论和分享。但是,如果推特有视频图像,或者如果有media_key,如果推文有多个图像,我想在新的列中添加URL或preview_URL,我只想要第一个。

这是我用来管理获取的所有数据的代码

if(huboResultados){
const values = () => {
const {data, includes} = datos
let arrayData = [] 
let arrayMetricas = []
data.map(item => {
const { author_id, id, text, created_at, public_metrics} = item 
const {retweet_count, reply_count, like_count} = public_metrics;
const totalinteracciones = retweet_count + reply_count + like_count 
const idTweet = id;
Logger.log(public_metrics)
includes.users.map(user => {
const { id, name, username} = user
if(author_id === id){
arrayData = ["twitter.com/anyuser/status/" + idTweet, text, username, created_at, like_count, reply_count, retweet_count, totalinteracciones]
}
return arrayData
})

Logger.log(mainArray)
return mainArray.push(arrayData)
})   
}

values()
var totaltweets = mainArray.length
Logger.log(query)
Logger.log(totaltweets)
Logger.log (activesheet.getLastRow())
activesheet.getRange("B10:I"+ activesheet.getLastRow()).clearContent()
activesheet.getRange(10,2,totaltweets, mainArray[0].length).setValues(mainArray);
}
}

这是我从Fetch获得的数据类型的一个例子:

{
"data": [
{
"text": "RT @PabloDa65125285: Oye #TataMartino porque No te quieren en La Selección:n#FueraTata #SeleccionMexicana #Qatar2022 #Colombia #Chiringuito…",
"public_metrics": {
"retweet_count": 4,
"reply_count": 0,
"like_count": 0,
"quote_count": 0
},
"created_at": "2022-09-28T14:59:33.000Z",
"author_id": "1297299916654608389",
"id": "1575138146433499138"
},
{
"attachments": {
"media_keys": [
"7_1575137009743659009"
]
},
"text": "@miseleccionmx Ya tiene chamba asegurada el Tata en la sección de "Quién es quien en las mentiras" n#QuienEsQuienEnLasMentiras n#AMLOBurlaMundial n#SeleccionMexicana n#MasAccionMasDiversion https://  t.co/73jOxVjydh",
"public_metrics": {
"retweet_count": 0,
"reply_count": 0,
"like_count": 0,
"quote_count": 0
},
"created_at": "2022-09-28T14:55:07.000Z",
"author_id": "974663527104356352",
"id": "1575137031843373063"
},
}
],
"includes": {
"users": [
{
"id": "1297299916654608389",
"name": "Juanma76",
"username": "Juanma7611"
},
{
"id": "974663527104356352",
"name": "Ro2 ",
"username": "ro2var"
},
],
"media": [
{
"media_key": "7_1575137009743659009",
"preview_image_url": "https://pbs.twimg.com/ext_tw_video_thumb/1575137009743659009/pu/img/qluOCv0Uw_u2J_SG.jpg",
"type": "video"
},
{
"media_key": "3_1575014644439212032",
"type": "photo",
"url": "https://pbs.twimg.com/media/FduTeW3WYAAEm7W.jpg"
}
]
},
"meta": {
"newest_id": "1575138146433499138",
"oldest_id": "1575088073242574849",
"result_count": 10,
"next_token": "b26v89c19zqg8o3fpzblrml2r91pb8ibeaxwgyo2ee8ot"
}
}

感谢您的阅读,并为糟糕的英语感到抱歉

我相信您想要的是在includes中获取mediaURL。请尝试以下代码的第一部分:


var mainArray = [];
const values = () => {
const {data, includes} = datos;
let arrayData = [] ;
let arrayMetricas = [];
var mapImages = {};
for (var i=0;i<includes.media.length;i++){
if (includes.media[i].preview_image_url != undefined ){
mapImages[includes.media[i].media_key] = {"preview_url": includes.media[i].preview_image_url}
}
if (includes.media[i].url != undefined ){
mapImages[includes.media[i].media_key]  = {"url":includes.media[i].url}
}
}
data.map(item => {
const { author_id, id, text, created_at, public_metrics} = item 
const {retweet_count, reply_count, like_count} = public_metrics;
const totalinteracciones = retweet_count + reply_count + like_count 
const idTweet = id;
var preview_url = ""
var url = ""
if (item.attachments != undefined){
for (var i=0;i<item.attachments.media_keys.length;i++){
if (mapImages[item.attachments.media_keys[i]] != undefined){
if(mapImages[item.attachments.media_keys[i]].url != undefined){
url = mapImages[item.attachments.media_keys[i]].url
}
else if (mapImages[item.attachments.media_keys[i]].preview_url != undefined){
preview_url = mapImages[item.attachments.media_keys[i]].preview_url
}
}
}
}
Logger.log(public_metrics)
includes.users.map(user => {
const { id, name, username} = user
if(author_id === id){
arrayData = ["twitter.com/anyuser/status/" + idTweet, text, username, created_at, like_count, reply_count, retweet_count, totalinteracciones, url, preview_url]
}
return arrayData
})

return mainArray.push(arrayData)
})   
}
values()

编辑:包含mapImages以收集到需要在每条推文中查找的media_key的映射

相关内容

  • 没有找到相关文章

最新更新