Skip to content

Commit

Permalink
Merge pull request #31 from matevskial/main
Browse files Browse the repository at this point in the history
Add a bunch of features and fixes
  • Loading branch information
TheJoin95 authored May 12, 2022
2 parents d7144c5 + 68808e2 commit 9d9ff4b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,22 @@ env:
POSTPROCESSOR_ARGS: "ffmpeg:-ac 1"
```

## Explicit Mode

By setting the `IS_EXPLICIT`, the new episode will be marked as explicit.
```yaml
env:
IS_EXPLICIT: true
```

## Thumbnail Mode

By setting the `LOAD_THUMBNAIL`, the new episode will include the video thumbnail as the episode art.
```yaml
env:
LOAD_THUMBNAIL: true
```

# Credits

[@thejoin](https://github.com/thejoin95) & [@wabri](https://github.com/wabri)
Expand Down
38 changes: 34 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { exec } = require('child_process');
const { exec, execSync } = require('child_process');
const fs = require('fs');

function GetEnvironmentVar(varname, defaultvalue) {
Expand All @@ -13,13 +13,18 @@ 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 pathToEpisodeJSON = GetEnvironmentVar('EPISODE_PATH','.') + '/episode.json';
const outputFile = 'episode.mp3';

// Just save as draft, to allow approval flow.
const draftMode = GetEnvironmentVar('SAVE_AS_DRAFT', 'false')
const actionText = draftMode == 'true' ? 'Save as draft' : 'Publish now'
const saveDraftOrPublishButtonXPath = draftMode == 'true' ? '//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"]'

// Allow fine tunning of the converted audio file
// Example: "ffmpeg:-ac 1" for mono mp3
Expand All @@ -45,6 +50,8 @@ exec('sudo curl -k -L https://github.com/yt-dlp/yt-dlp/releases/latest/download/
const YT_ID = epConfJSON.id;
console.log(`Processing: ${YT_ID}`);
const url = YT_URL + YT_ID;
const thumbnailOutputFileTemplate = `thumbnail.%(ext)s`
const thumbnailOutputFile = `thumbnail.${THUMBNAIL_FORMAT}`

youtubedl.getInfo(url, function (err, info) {
if (err) throw err;
Expand All @@ -54,6 +61,11 @@ exec('sudo curl -k -L https://github.com/yt-dlp/yt-dlp/releases/latest/download/
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}`)

Expand Down Expand Up @@ -115,8 +127,26 @@ exec('sudo curl -k -L https://github.com/yt-dlp/yt-dlp/releases/latest/download/
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(`//button[text()="${actionText}"]`);
const [button] = await page.$x(saveDraftOrPublishButtonXPath);

// If no button is found using label, try using css path
if (button) {
Expand Down

0 comments on commit 9d9ff4b

Please sign in to comment.