forked from farizrifqi/Threads-Media-Downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedia.js
More file actions
74 lines (69 loc) · 2.53 KB
/
media.js
File metadata and controls
74 lines (69 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));
const getPostData = async (id) => {
let request = await fetch("https://www.threads.net/api/graphql", {
"headers": {
"content-type": "application/x-www-form-urlencoded",
"x-asbd-id": "129477",
"x-fb-friendly-name": "BarcelonaPostPageQuery",
"x-fb-lsd": "N1RcROyvW2TeIOAP1NF1Rw",
"x-ig-app-id": "238260118697367",
},
"body": new URLSearchParams({
lsd: "N1RcROyvW2TeIOAP1NF1Rw",
variables: `{"postID":"${id}"}`,
doc_id: 5587632691339264
}),
"method": "POST"
});
let response = await request.json()
console.log(response)
return response
}
const getPostId = async (url) => {
let request = await fetch(url)
let response = await request.text()
let postId = response.match(/{"post_id":"(.*?)"}/)
return postId[1]
}
const getAllMedia = async (url) => {
let postId = await getPostId(url)
let postData = await getPostData(postId)
let allMedia = postData.data.data.containing_thread.thread_items.map(thread => (getMedia(thread)))
return allMedia
}
const getMedia = (thread) => {
let media = thread.post
media = media.text_post_app_info.share_info.quoted_post ? media.text_post_app_info.share_info.quoted_post : media // quoted post
media = media.text_post_app_info.share_info.reposted_post ? media.text_post_app_info.share_info.reposted_post : media // reposted post
if (media.carousel_media) {
if (media.carousel_media.video_versions) return {
user: media.user,
type: "videos",
media: media.carousel_media.map(media => (media.video_versions[0])),
width: media.original_width,
height: media.original_height
}
return {
user: media.user,
type: "photos",
media: media.carousel_media.map(media => (media.image_versions2.candidates[0])),
width: media.original_width,
height: media.original_height
}
}
if (media.video_versions.length > 0) return {
user: media.user,
type: "video",
media: media.video_versions[0],
width: media.original_width,
height: media.original_height
}
return {
user: media.user,
type: "photo",
media: media.image_versions2.candidates,
width: media.original_width,
height: media.original_height
}
}
module.exports = { getAllMedia }