forked from Schroedinger-Hat/youtube-to-spotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
192 lines (151 loc) · 9.22 KB
/
index.js
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const { exec, execSync } = require('child_process');
const fs = require('fs');
function GetEnvironmentVar(varname, defaultvalue) {
var result = process.env[varname];
if (result != undefined)
return result;
else
return defaultvalue;
}
require('dotenv').config()
const email = process.env.ANCHOR_EMAIL;
const password = process.env.ANCHOR_PASSWORD;
const UPLOAD_TIMEOUT = process.env.UPLOAD_TIMEOUT || 60 * 5 * 1000;
const THUMBNAIL_FORMAT = "jpg";
const YT_URL = 'https://www.youtube.com/watch?v=';
const pathToEpisodeJSONdir = GetEnvironmentVar('EPISODE_PATH','.') ;
const outputFile = 'episode.mp3';
const draftMode = GetEnvironmentVar('SAVE_AS_DRAFT', 'false')
const url = GetEnvironmentVar('URL', '')
const saveDraftOrPublishButtonXPath = draftMode == 'False' ? '//button[text()="Save as draft"]' : '//button/div[text()="Publish now"]'
const thumbnailMode = GetEnvironmentVar('LOAD_THUMBNAIL', 'false')
const isExplicit = GetEnvironmentVar('IS_EXPLICIT', 'false')
const selectorForExplicitContentLabel = isExplicit == 'true' ? 'label[for="podcastEpisodeIsExplicit-true"]' : 'label[for="podcastEpisodeIsExplicit-false"]'
const urlDescription = GetEnvironmentVar('URL_IN_DESCRIPTION', 'false')
// Allow fine tunning of the converted audio file
// Example: "ffmpeg:-ac 1" for mono mp3
const postprocessorArgs = GetEnvironmentVar('POSTPROCESSOR_ARGS', "")
const postprocessorArgsCmd = postprocessorArgs == ""? "": `--postprocessor-args="${postprocessorArgs}"`
console.log('installing dependecies');
exec('sudo curl -k -L https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -o /usr/local/bin/youtube-dl && sudo chmod a+rx /usr/local/bin/youtube-dl && sudo npm i puppeteer --unsafe-perm=true --allow-root', (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
}
if (stderr) {
console.log(`stderr: ${stderr}`);
}
console.log(`stdout: ${stdout}`);
const youtubedl = require('youtube-dl');
const puppeteer = require('puppeteer');
let YT_IDs=[];
try {
if(url.includes('/video/')){
const epConfJSON = JSON.parse(fs.readFileSync(pathToEpisodeJSON, 'utf-8'));
const YT_ID = epConfJSON.id;
YT_IDs.push(YT_ID)}
else if (url.includes('/c/') || url.includes('/channel/')){
const channelid=url.split('/')[-1]
const dirPath = './videos/';
const files = fs.readdirSync(dirPath);
const arr = []
files.forEach((val, i) => {
const epConfJSON = JSON.parse(fs.readFileSync(path.join(dirPath, val), 'utf8'));
const YT_ID = epConfJSON.id;
const url = YT_URL + YT_ID;
const pathToEpisodeJSON=pathToEpisodeJSONdir+YT_ID +'/episode.json'
const thumbnailOutputFileTemplate = `thumbnail.%(ext)s`
const thumbnailOutputFile = `thumbnail.${THUMBNAIL_FORMAT}`
youtubedl.getInfo(url, function (err, info) {
if (err) throw err;
epConfJSON.title = info.title;
epConfJSON.description = urlDescription !== 'false' ? info.description + '\n' + url : info.description;
console.log(`title: ${epConfJSON.title}`)
console.log(`description: ${epConfJSON.description}`)
const youtubeDlThumbnailCommand = `youtube-dl -o "${thumbnailOutputFileTemplate}" --skip-download --write-thumbnail --convert-thumbnails ${THUMBNAIL_FORMAT} ${url}`
console.log(`Thumbnail download command: ${youtubeDlThumbnailCommand}`)
const thumbnailDownloadStdout = execSync(youtubeDlThumbnailCommand)
console.log(`stdout: ${thumbnailDownloadStdout}`)
const youtubeDlCommand = `youtube-dl -o ${outputFile} -f bestaudio -x --force-overwrites --audio-format mp3 ${postprocessorArgsCmd} ${url}`;
console.log(`Download command: ${youtubeDlCommand}`)
exec(youtubeDlCommand, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`)
return
}
if (stderr) {
console.log(`stderr: ${stderr}`)
return
}
console.log(`stdout: ${stdout}`)
fs.writeFileSync(pathToEpisodeJSON, JSON.stringify(epConfJSON));
const episode = JSON.parse(fs.readFileSync(pathToEpisodeJSON, 'utf-8'));
const jsonEpisode = JSON.stringify(episode)
console.log(`-- episode.json: ${jsonEpisode}`);
(async () => {
console.log("Launching puppeteer");
const browser = await puppeteer.launch({ args: ['--no-sandbox'] });
const page = await browser.newPage();
const navigationPromise = page.waitForNavigation();
await page.goto('https://anchor.fm/dashboard/episode/new');
await page.setViewport({ width: 1600, height: 789 });
await navigationPromise;
console.log("Trying to log in");
await page.type('#email', email);
await page.type('#password', password);
await page.click('button[type=submit]');
await navigationPromise;
console.log("Logged in");
await page.waitForSelector('input[type=file]');
console.log("Uploading audio file");
const inputFile = await page.$('input[type=file]');
await inputFile.uploadFile(outputFile);
console.log("Waiting for upload to finish");
await page.waitForTimeout(25 * 1000);
await page.waitForXPath('//div[contains(text(),"Save")]/parent::button[not(boolean(@disabled))]', { timeout: UPLOAD_TIMEOUT });
const [saveButton] = await page.$x('//div[contains(text(),"Save")]/parent::button[not(boolean(@disabled))]');
await saveButton.click();
await navigationPromise;
console.log("-- Adding title");
await page.waitForSelector('#title', { visible: true });
// Wait some time so any field refresh doesn't mess up with our input
await page.waitForTimeout(2000);
await page.type('#title', episode.title);
console.log("-- Adding description");
await page.waitForSelector('div[role="textbox"]', { visible: true });
await page.type('div[role="textbox"]', episode.description);
console.log("-- Selecting content type")
await page.waitForSelector(selectorForExplicitContentLabel, { visible: true})
const contentTypeLabel = await page.$(selectorForExplicitContentLabel)
await contentTypeLabel.click()
if (thumbnailMode !== 'false') {
console.log("-- Uploading episode art")
await page.waitForSelector('input[type=file][accept="image/*"]');
const inputEpisodeArt = await page.$('input[type=file][accept="image/*"]');
await inputEpisodeArt.uploadFile(thumbnailOutputFile);
console.log("-- Saving uploaded episode art")
await page.waitForXPath('//button/div[text()="Save"]')
const [saveEpisodeArtButton] = await page.$x('//button/div[text()="Save"]')
await saveEpisodeArtButton.click()
await page.waitForXPath('//div[@aria-label="image uploader"]', { hidden: true, timeout: UPLOAD_TIMEOUT})
}
console.log("-- Publishing");
const [button] = await page.$x(saveDraftOrPublishButtonXPath);
// If no button is found using label, try using css path
if (button) {
await button.click();
}
else {
await page.click('.styles__button___2oNPe.styles__purple___2u-0h.css-39f635');
}
await navigationPromise;
await browser.close()
return new Promise((resolve, reject) => resolve("yay"));
})().then(r => console.log(r), v => console.log(v));
});
});
})
}
} catch (error) {
throw error;
}
});