Skip to content

Buzzertech/DatSongBot

Repository files navigation

datsongbot_cover

DatSongBot 🎵

CircleCI semantic-release

DatSongBot is a friendly, geek bot which uploads a music "video" to YouTube (Check it out on YouTube) everyday. The track is picked from soundcloud. The bot uploads trap/chill music. This bot is written entirely in Typescript and runs on node. P.S. - None of these videos uploaded to YouTube are monetized

Tech used

DatSongBot runs on node and uses these few technologies to make things happen:

  • ffmpeg - we use this library to encode videos
  • puppeteer - to generate static image to be used in the video
  • AWS Lambda - The bot runs on a node v8.10 lambda paired with a cloudwatch event which runs everyday at a specified time

Third party services

  • Soundcloud

    DatSongBot uses the soundcloud's tracks API which returns a bunch of songs to choose from. Now, the bot simply picks a song based on certain criterias.

  • Unsplash

    We use the tags added to this song to fetch a stock image. The stock image library that the bot prefers is Unsplash (Unsplash has a great API + the entire image library has a bunch of free, amazing images). You can even use their source api which returns a random image directly instead of a json response. I am using the integral api instead because it returns other metadata such as uploader's user id, name, etc. so that the photographer can be credited in the video's description.

  • YouTube

    Ofcourse, we use YouTube's v3 API to upload videos to the platform. You can read up a little more about how the insert api works here - https://developers.google.com/youtube/v3/docs/videos/insert

Try it yourself

Prerequisite: Make sure the below env vars are set before running the bot -

  • SOUNDCLOUD_CLIENT_ID - Soundcloud has shut down its developer application program but there is still a workaround to get a client id for your apps. Check this answer on stackoverflow
  • UNSPLASH_ACCESS_KEY - Get the unsplash access key from here - https://unsplash.com/developers
  • YOUTUBE_REFRESH_TOKEN, YOUTUBE_CLIENT_ID, YOUTUBE_CLIENT_SECRET - YouTube's API requires you to be authenticated using its OAuth2 client. To get all these three auth keys refer this stackoverflow answer

Once done, clone the repo and run

$ yarn

Once all the dependencies are installed, you can use the following command to run the function locally using the serverless framework

$ yarn dev

Deploy (AWS Lambda)

If you have the awscli installed locally, make sure you've configured the credentials i.e. Access Key ID and Secret Access Key on your machine

$ aws configure

Or

$ export AWS_ACCESS_KEY_ID=*
$ export AWS_SECRET_ACCESS_KEY=*

Once done, you can run

$ yarn deploy

After the lambda function is created and deployed, there are couple more steps needed to be followed.

So, the problem here is that puppeteer runs on chromium which needs around 300-400mb of storage space which is not possible on a lambda (cause lambda's 50mb limit.) Now, to run puppeteer on a lambda, we use chrome-aws-lambda which provides a brotli compressed binary of chromium which is mere 33 mb! We also need the ffmpeg binary which is installed using @ffmpeg-installer - the linux-x64 binary bundled with this library is of ~60mb. I've deployed these two dependencies using aws layers (read more)

Run tests

$ yarn test

Motivation

I love listening songs on soundcloud and it is one amazing music library with great artists who put in a sheer amount of time and energy into their work to entertain us. Thousands of songs are uploaded to soundcloud everyday but only a few of them get to share the spotlight. The goal here is to build a community which loves listening to chill/trap music and also would like to give new artists a chance!

Also, before getting into programming, I used to make a bunch of how-to YouTube Videos and I must've had edited and rendered ~100 videos till date but never knew what went along under the hood. This project helped me understand a little about how videos/audio are encoded and stuff!

Contribute

Issues and PRs are most welcomed! If you are submitting a PR, make sure you are following these commit message conventions

License

MIT