Skip to content

Commit

Permalink
fix: upload not working
Browse files Browse the repository at this point in the history
  • Loading branch information
Buzzertech committed Jun 16, 2019
1 parent c750ec2 commit 413b0d4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ const config = {
SOUNDCLOUD_CLIENT_ID: process.env.SOUNDCLOUD_CLIENT_ID,
SOUNDCLOUD_SEARCH_TAGS: ['Chill', 'Trap'],
SOUNDCLOUD_SEARCH_LICENSE: 'cc-by',
YOUTUBE_API_KEY: process.env.YOUTUBE_API_KEY,
YOUTUBE_CLIENT_ID: process.env.YOUTUBE_CLIENT_ID,
YOUTUBE_CLIENT_SECRET: process.env.YOUTUBE_CLIENT_SECRET,
YOUTUBE_REFRESH_TOKEN: process.env.YOUTUBE_REFRESH_TOKEN,
UNSPLASH_ACCESS_KEY: process.env.UNSPLASH_ACCESS_KEY,
};

export default config;
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,26 @@ import {
launchPage,
closePage,
processVideo,
uploadVideo,
} from './video';
import { getTracksFromSoundcloud } from './audio';
import { getUnsplashUrl } from './image';
import { getUnsplashPhoto } from './image';
import { resolve } from 'path';

const test = async () => {
launchPage();
const song = await getTracksFromSoundcloud();
const { data: image } = await getUnsplashPhoto(song.tag_list);
const svgContent = prepareSvg(
getUnsplashUrl(song.tag_list),
image.urls.custom,
song.title.replace(/(")|(')|(\.)/g, ''),
song.user.username
);
await generateImage(svgContent);
await processVideo(song, resolve(__dirname, '../assets/out.png'));
const response = await uploadVideo(song, image);

console.log('Youtube video id - ' + response.data.id);
};

test().catch(e => {
Expand Down
42 changes: 35 additions & 7 deletions src/video.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import ffmpeg from 'fluent-ffmpeg';
import { Track } from './audio';
import { PickedTrack } from './audio';
import { Browser, launch } from 'puppeteer';
import config from './config';
import path, { resolve } from 'path';
import { google } from 'googleapis';
import { addDays } from 'date-fns';
import { createReadStream } from 'fs';
import { IUnsplashResponse } from 'image';

const youtube = google.youtube({ version: 'v3', auth: config.YOUTUBE_API_KEY });
const oauthclient = new google.auth.OAuth2({
clientId: config.YOUTUBE_CLIENT_ID,
clientSecret: config.YOUTUBE_CLIENT_SECRET,
});

oauthclient.setCredentials({
refresh_token: config.YOUTUBE_REFRESH_TOKEN,
});

(async () =>
oauthclient.setCredentials({
access_token: (await oauthclient.getAccessToken()).token,
}))();

const youtube = google.youtube({ version: 'v3', auth: oauthclient });

let window: Browser;

Expand Down Expand Up @@ -60,7 +75,7 @@ export const generateImage = async (content: string) => {
};

export const processVideo = (
song: Pick<Track, 'duration' | 'download_url' | 'stream_url'>,
song: PickedTrack,
image: string
): Promise<void> => {
//@ts-ignore
Expand Down Expand Up @@ -95,7 +110,11 @@ export const processVideo = (
});
};

const getDescription = (songTitle: string, song: Track) => `
const getDescription = (
songTitle: string,
song: PickedTrack,
imageData: IUnsplashResponse
) => `
${songTitle}
⭐️ DatSongBot brings you another fresh, new music by ${
Expand All @@ -108,18 +127,27 @@ const getDescription = (songTitle: string, song: Track) => `
Follow ${song.user.username} on Soundcloud:
🔉${song.user.permalink_url}
The background image used in this video is provided by ${
imageData.user.name
} from Unsplash:
🔗Follow ${imageData.user.name} on Unsplash - ${imageData.user.links.html}
📂Download this background - ${imageData.links.html}
🎵 DatSongBot is a bot built by Buzzertech (https://buzzertech.com) which picks a new, trending song from soundcloud and uploads it to YouTube. This is an experimental tool. With that being said, be sure to subscribe to DatSongBot on YouTube and turn on notifications 'cause we post new music daily on this channel!
❌ DatSongBot doesn't owns this music. Just for entertainment purposes only!
❌ DatSongBot doesn't owns this music nor the image used in this video. Just for entertainment purposes only!
Cheers 🎵
`;

export const uploadVideo = (song: Track) => {
export const uploadVideo = (
song: PickedTrack,
imageData: IUnsplashResponse
) => {
const songTitle =
song.title.replace(/(")|(')|(\.)/g, '').trim() + ` | ${song.user.username}`;

const description = getDescription(songTitle, song);
const description = getDescription(songTitle, song, imageData);
return youtube.videos.insert({
part: 'snippet, status',
requestBody: {
Expand Down

0 comments on commit 413b0d4

Please sign in to comment.