Last active
May 30, 2020 11:02
-
-
Save sidelux/fb302544e4299ac8d31bc9735da702d2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const got = require('@/utils/got'); | |
const cheerio = require('cheerio'); | |
module.exports = async (ctx) => { | |
const tag = ctx.params.tag; | |
const response = await got({ | |
method: 'get', | |
url: `https://www.instagram.com/explore/tags/${tag}/`, | |
headers: { | |
Referer: `https://www.instagram.com/explore/tags/${tag}/`, | |
}, | |
}); | |
const data = JSON.parse(response.data.match(/<script type="text\/javascript">window._sharedData = (.*);<\/script>/)[1]) || {}; | |
const list = data.entry_data.TagPage[0].graphql.hashtag.edge_hashtag_to_media.edges; | |
const $ = cheerio.load(response.data); | |
// retrieve media objects | |
var media = await Promise.all( | |
list.map(async (item) => { | |
item = item.node; | |
const url = `https://www.instagram.com/p/${item.shortcode}`; | |
const cache = await ctx.cache.get(url); | |
if (cache) { | |
return Promise.resolve(JSON.parse(cache)); | |
} | |
if (item.__typename === 'GraphImage') { | |
const single = { | |
image: item.display_url, | |
}; | |
ctx.cache.set(url, JSON.stringify([single])); | |
return Promise.resolve([single]); | |
/* | |
} else if (item.__typename === 'GraphSidecar') { | |
const response = await got({ | |
method: 'get', | |
url, | |
}); | |
const data = JSON.parse(response.data.match(/<script type="text\/javascript">window._sharedData = (.*);<\/script>/)[1]) || {}; | |
const single = data.entry_data.PostPage[0].graphql.shortcode_media.edge_sidecar_to_children.edges.map((item) => ({ | |
image: item.node.display_url, | |
video: item.node.video_url, | |
})); | |
ctx.cache.set(url, JSON.stringify(single)); | |
return Promise.resolve(single); | |
} else if (item.__typename === 'GraphVideo') { | |
const response = await got({ | |
method: 'get', | |
url, | |
}); | |
const data = JSON.parse(response.data.match(/<script type="text\/javascript">window._sharedData = (.*);<\/script>/)[1]) || {}; | |
console.log(JSON.stringify(data, null, 4)); | |
const single = { | |
image: data.entry_data.PostPage[0].graphql.shortcode_media.display_url, | |
video: data.entry_data.PostPage[0].graphql.shortcode_media.video_url, | |
}; | |
ctx.cache.set(url, JSON.stringify([single])); | |
return Promise.resolve([single]); | |
*/ | |
} | |
}) | |
); | |
media = media.filter(function (el) { | |
return el != undefined; | |
}); | |
ctx.state.data = { | |
title: `#${tag} hashtag on Instagram`, | |
link: `https://www.instagram.com/explore/tags/${tag}/`, | |
description: $('meta[name="description"]').attr('content'), | |
item: list.map((item, index) => { | |
item = item.node; | |
let image = 0; | |
let video = 0; | |
let content = ''; | |
content += `<img src="${item.display_url}"><br>`; | |
image++; | |
/* | |
for (let i = 0; i < media[index].length; i++) { | |
if (media[index][i].image && !media[index][i].video) { | |
content += `<img src="${media[index][i].image}"><br>`; | |
image++; | |
} | |
if (media[index][i].video) { | |
content += `<video width="100%" controls="controls" poster="${media[index][i].image}"> <source src="${media[index][i].video}" type="video/mp4"> Your RSS reader does not support video playback. </video>`; | |
video++; | |
} | |
} | |
*/ | |
let title = (item.edge_media_to_caption.edges && item.edge_media_to_caption.edges[0] && item.edge_media_to_caption.edges[0].node.text) || '无题/Untitled'; | |
if (image > 1) { | |
title = `[组图/Carousel]${title}`; | |
} | |
if (video > 0) { | |
title = `[视频/Video]${title}`; | |
} | |
return { | |
title: `${title}`, | |
description: `${title}<br>${content}`, | |
pubDate: new Date(item.taken_at_timestamp * 1000).toUTCString(), | |
link: `https://www.instagram.com/p/${item.shortcode}/`, | |
}; | |
}), | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment