Interesting Stuff: Dynamic Content in Github Readme.md file

Github badges are cool and lots of github repo use shields.io to generate one. With shields.io, we can also create our own custom badges like this one that has been used for a nepali calendar events repo

The github badges are just a static image on web that we can include in our markdown file. Shields.io also provides dynamic badges (dynamic images) generated by some webserver. Getting to know about dynamic github badges, got an idea to have today's date on one of open source BS Date conversion repo (pyBSDate) that I currently maintain. The idea is similar to dynamic github badges, we can generate a dynamic image based on today's BS date and place it in the Readme file for the repo. So every time we go to the pyBSDate repo we will get to see today's BS date. Sounds super cool, right? Let's get it done.

Things we need:

  1. First thing, we need is a tool that can generate an image with a dynamic text(for our case a Nepali BS date text).
  2. After we have that script, we can add that script to a cron job that would run every day to get a new image for that day.
  3. Finally we can serve that image using a webserver and have it placed in a Readme.md file as an image.

Hmmmm. Imagemagick library can do step number 1 and we can run a cronjob and a webserver for step 2 and 3 on our server.

After some hours of hit and trial and lots of duckduckgo, following script perfectly generates a image with dynamic date on a gif file.


convert -size 1000x140 xc:white -font Mukta-Light -pointsize 70 -fill '#bf092d' -gravity East -draw "text 200,0 \"`PYTHONIOENCODING=UTF-8 python -c \"from pyBSDate import bsdate;print(bsdate.today().strftime('%B %d %Y, %A', 'ne'))\"`\" " null: \( nepal_flag.gif -coalesce \) -gravity East   -layers Composite -layers Optimize today.gif

Now we can use a cronjob to automatically run that script once in a day at 00:00 UTC time using following cron script


0 0 */1 * * convert -size 1000x140 xc:white -font Mukta-Light -pointsize 70 -fill '#bf092d' -gravity East -draw "text 200,0 \"`PYTHONIOENCODING=UTF-8 python -c \"from pyBSDate import bsdate;print(bsdate.today().strftime('%B %d %Y, %A', 'ne'))\"`\" " null: \( nepal_flag.gif -coalesce \) -gravity East   -layers Composite -layers Optimize today.gif

And finally serve our dish (today.gif) using a webserver. Following is the final image we'll get and is supposed to have today's BS date.

Final product is also included in Readme.md file of the pyBSDate repository.

Stay hungry, stay foolish fellas.

PS: pyBSDate is an opensource python library to convert between BS date and AD date. The library is compactible with python's native date library. If you are interested in contribution to the project, feel free to send us the pull requests or just drop us an email to know about the development.

No comments :

Post a Comment