youtube.ts
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -23,4 +23,3 @@ /// <reference types="node" /> | ||
*/ | ||
downloadThumbnail: (videoResolvable: string, folder?: string) => Promise<string>; | ||
private readonly getVideo; | ||
downloadThumbnail: (videoResolvable: string, folder?: string, noDL?: boolean) => Promise<string>; | ||
} |
@@ -481,4 +481,4 @@ "use strict"; | ||
*/ | ||
this.downloadThumbnail = function (videoResolvable, folder) { return __awaiter(_this, void 0, void 0, function () { | ||
var video, thumbnail, dest, arrayBuffer; | ||
this.downloadThumbnail = function (videoResolvable, folder, noDL) { return __awaiter(_this, void 0, void 0, function () { | ||
var id, url, info, thumbnail, dest, arrayBuffer; | ||
return __generator(this, function (_a) { | ||
@@ -491,9 +491,15 @@ switch (_a.label) { | ||
fs_1["default"].mkdirSync(folder, { recursive: true }); | ||
return [4 /*yield*/, this.getVideo(videoResolvable)]; | ||
return [4 /*yield*/, this.resolveID(videoResolvable, "video")]; | ||
case 1: | ||
video = _a.sent(); | ||
thumbnail = video.snippet.thumbnails.maxres ? video.snippet.thumbnails.maxres.url : video.snippet.thumbnails.high.url; | ||
dest = path_1["default"].join(folder, video.snippet.title + ".png"); | ||
id = _a.sent(); | ||
url = "https://www.youtube.com/watch?v=" + id; | ||
return [4 /*yield*/, ytdl_core_1["default"].getInfo(url)]; | ||
case 2: | ||
info = _a.sent(); | ||
thumbnail = info.videoDetails.thumbnails[info.videoDetails.thumbnails.length - 1].url; | ||
if (noDL) | ||
return [2 /*return*/, thumbnail]; | ||
dest = path_1["default"].join(folder, info.videoDetails.title + ".png"); | ||
return [4 /*yield*/, axios_1["default"].get(thumbnail, { responseType: "arraybuffer" }).then(function (r) { return r.data; })]; | ||
case 2: | ||
case 3: | ||
arrayBuffer = _a.sent(); | ||
@@ -505,20 +511,2 @@ fs_1["default"].writeFileSync(dest, Buffer.from(arrayBuffer, "binary")); | ||
}); }; | ||
this.getVideo = function (videoResolvable, params) { return __awaiter(_this, void 0, void 0, function () { | ||
var id, response; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!params) | ||
params = {}; | ||
return [4 /*yield*/, this.resolveID(videoResolvable, "video")]; | ||
case 1: | ||
id = _a.sent(); | ||
params.id = id; | ||
return [4 /*yield*/, this.api.get("video", params)]; | ||
case 2: | ||
response = _a.sent(); | ||
return [2 /*return*/, response.items[0]]; | ||
} | ||
}); | ||
}); }; | ||
} | ||
@@ -525,0 +513,0 @@ return Util; |
@@ -44,9 +44,13 @@ "use strict"; | ||
require("dotenv").config(); | ||
var youtube = new youtube_1["default"](process.env.GOOGLE_API_KEY); | ||
var youtube = new youtube_1["default"](); | ||
(function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var c; | ||
var d, c; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, youtube.util.downloadThumbnail("https://www.youtube.com/watch?v=OJf04b6884E", "./videos")]; | ||
case 0: return [4 /*yield*/, youtube.util.downloadMP3("https://www.youtube.com/watch?v=OJf04b6884E", "./videos")]; | ||
case 1: | ||
d = _a.sent(); | ||
console.log(d); | ||
return [4 /*yield*/, youtube.util.downloadThumbnail("https://www.youtube.com/watch?v=OJf04b6884E", "./videos")]; | ||
case 2: | ||
c = _a.sent(); | ||
@@ -53,0 +57,0 @@ console.log(c); |
@@ -12,5 +12,5 @@ import api from "./API"; | ||
info: Info; | ||
constructor(apiKey: string); | ||
constructor(apiKey?: string); | ||
} | ||
export * from "./entities/index"; | ||
export * from "./types/index"; |
@@ -159,3 +159,3 @@ import axios from "axios" | ||
const writeStream = fs.createWriteStream(`${dest}/${clean}.mp4`) | ||
await new Promise((resolve) => { | ||
await new Promise<void>((resolve) => { | ||
ytdl(url, options).pipe(writeStream).on("finish", () => resolve()) | ||
@@ -208,3 +208,3 @@ }) | ||
const writeStream = fs.createWriteStream(`${dest}/${clean}.mp3`) | ||
await new Promise((resolve) => { | ||
await new Promise<void>((resolve) => { | ||
ytdl(url, {filter: "audioonly"}).pipe(writeStream).on("finish", () => resolve()) | ||
@@ -257,8 +257,11 @@ }) | ||
*/ | ||
public downloadThumbnail = async (videoResolvable: string, folder?: string) => { | ||
public downloadThumbnail = async (videoResolvable: string, folder?: string, noDL?: boolean) => { | ||
if (!folder) folder = "./" | ||
if (!fs.existsSync(folder)) fs.mkdirSync(folder, {recursive: true}) | ||
const video = await this.getVideo(videoResolvable) | ||
const thumbnail = video.snippet.thumbnails.maxres ? video.snippet.thumbnails.maxres.url : video.snippet.thumbnails.high.url | ||
const dest = path.join(folder, `${video.snippet.title}.png`) | ||
const id = await this.resolveID(videoResolvable, "video") | ||
const url = `https://www.youtube.com/watch?v=${id}` | ||
const info = await ytdl.getInfo(url) | ||
const thumbnail = info.videoDetails.thumbnails[info.videoDetails.thumbnails.length - 1].url | ||
if (noDL) return thumbnail | ||
const dest = path.join(folder, `${info.videoDetails.title}.png`) | ||
const arrayBuffer = await axios.get(thumbnail, {responseType: "arraybuffer"}).then((r) => r.data) | ||
@@ -268,10 +271,2 @@ fs.writeFileSync(dest, Buffer.from(arrayBuffer, "binary")) | ||
} | ||
private readonly getVideo = async (videoResolvable: string, params?: YoutubeVideoParams) => { | ||
if (!params) params = {} | ||
const id = await this.resolveID(videoResolvable, "video") | ||
params.id = id | ||
const response = await this.api.get("video", params) | ||
return response.items[0] as Promise<YoutubeVideo> | ||
} | ||
} |
{ | ||
"name": "youtube.ts", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Wrapper for the YouTube API with typings.", | ||
@@ -39,4 +39,4 @@ "main": "dist/youtube.js", | ||
"axios": "^0.19.0", | ||
"ytdl-core": "^4.0.3" | ||
"ytdl-core": "^4.9.1" | ||
} | ||
} |
import Youtube from "./youtube" | ||
require("dotenv").config() | ||
const youtube = new Youtube(process.env.GOOGLE_API_KEY); | ||
const youtube = new Youtube(); | ||
@@ -18,4 +18,6 @@ (async () => { | ||
// console.log(result.items[0]) | ||
const d = await youtube.util.downloadMP3("https://www.youtube.com/watch?v=OJf04b6884E", "./videos") | ||
console.log(d) | ||
const c = await youtube.util.downloadThumbnail("https://www.youtube.com/watch?v=OJf04b6884E", "./videos") | ||
console.log(c) | ||
})() |
@@ -13,3 +13,3 @@ import api from "./API" | ||
public info = new Info(this.api) | ||
public constructor(apiKey: string) { | ||
public constructor(apiKey?: string) { | ||
Youtube.apiKey = apiKey | ||
@@ -16,0 +16,0 @@ this.api = new api(Youtube.apiKey) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2
158862
3353
Updatedytdl-core@^4.9.1