-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
120 lines (105 loc) · 3.26 KB
/
Copy pathtest.js
File metadata and controls
120 lines (105 loc) · 3.26 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
'use strict';
const axios = require("axios");
const cheerio = require("cheerio");
const fs = require('fs');
const Path = require('path')
/**
* @param {object} mediaData graphql JSON object
* @returns {string} image or video
*/
function getMediaType(mediaData) {
if (mediaData.is_video === true) {
return "video";
}
return "image";
}
/**
* @param {string} oriUrl
* @returns {string}
*/
function createNewUrl(oriUrl) {
if (oriUrl.slice(-1) != "/") {
oriUrl += "/";
}
return oriUrl + "?__a=1";
}
/**
* Download graphql data
* @param {string} url media url (https://www.instagram.com/p/[mediaCode]/)
* @returns {object} graphql JSON object
*/
async function downloadMetaData(url) {
try {
const metaData = await axios.get(url);
const $ = cheerio.load(metaData.data);
return {
description: $("meta[property='og:description']").attr("content"),
url: $("meta[property='og:url']").attr("content"),
thumbnail: $("meta[property='og:image']").attr("content"),
title: $("meta[property='og:title']").attr("content"),
is_video: $("video").attr("src") ? true : false
};
} catch (error) {
throw error;
}
}
/**
* Download media with axios
* @param {string} url media url
* @param {string} filename name of media
* @param {string} savePath path to save media downloaded
*/
async function download(url, filename, savePath) {
const path = Path.resolve(savePath, filename)
const writer = fs.createWriteStream(path)
const response = await axios({
url,
method: 'GET',
responseType: 'stream'
})
response.data.pipe(writer)
return new Promise((resolve, reject) => {
writer.on('finish', resolve)
writer.on('error', reject)
})
}
/**
* @typedef {Object} DownlodResult
* @property {string} file - Main file path
* @property {string} type - The type of media, video or image
* @property {string} thumbnail - Thumbnail path if video
*/
/**
* Download media with axios
* @param {string} url media url
* @param {string} savePath path to save media downloaded
* @returns {DownlodResult}
*/
async function downloadMedia(url, savePath) {
const newUrl = createNewUrl(url);
const getMetaData = await downloadMetaData(newUrl);
const getType = getMediaType(getMetaData);
const result = {
file: '',
type: '',
thumbnail: ''
}
if (getType == "image") {
await download(getMetaData.url, `${getMetaData.thumbnail}.jpg`, savePath);
result.file = Path.resolve(savePath, `${getMetaData.shortcode_media.shortcode}.jpg`)
result.type = 'Image'
} else {
await download(getMetaData.url, `${getMetaData.title}.mp4`, savePath);
await download(getMetaData.thumbnail, `${getMetaData.title}-thumb.jpg`, savePath);
result.file = Path.resolve(savePath, `${getMetaData.title}.mp4`)
result.thumbnail = Path.resolve(savePath, `${getMetaData.title}-thumb.jpg`)
result.type = 'Video'
}
return result
}
module.exports = {
createNewUrl,
getMediaType,
downloadMetaData,
downloadMedia
};