Programmatically Updating My GitHub Repository Using Python

Learn how I use PyGithub to update the GitHub repository @verfasor. You may refer to this step for clarity. And I am fully aware my method is overkill.

So I made a Python script to fetch my Spotify listening history. This isn’t new, though. I’ve been testing it inside Telegram for a while.

The script dumps the song information as next.txt. And as you can see, a GeneratePress hook fetch, prettify, and display the song data on the top bar of this website.

But that doesn’t end there.

To spice things up, I devised another script two days ago to convert the song details into images using PIL. Here’s how the script looks like:

# import libraries
from PIL import Image, ImageFont, ImageDraw
import qrcode
import os

bars = open('/folder/next.txt', 'r')
first_line = bars.readlines()
length = len(first_line)
artist = first_line[0]
song = first_line[1]
url = first_line[2]
by = "by "
text = "Last played: "
message1 = song 
message2 = artist

yo1 = message1.replace("\n", " ")
yo2 = message2.replace("\n", " ")
print(text + yo1 + 'by ' + yo2)

qr = qrcode.QRCode(

img = qr.make_image(fill_color="wheat", back_color=(33, 33, 33))"/folder/qr.png")

github = 'Last played on Spotify' + "\n" + "-----------------------"

from PIL import Image, ImageFont, ImageDraw, ImageOps
img ="/folder/qr.png")
img = ImageOps.expand(img, (10, 10, 1900, 10), fill=(33, 33, 33))
draw = ImageDraw.Draw(img)
font1 = ImageFont.truetype("Inter-Regular.ttf", 50)
font2 = ImageFont.truetype("Inter-Medium.ttf", 80)
font3 = ImageFont.truetype("Inter-Regular.ttf", 60)

draw.text((400, 32),str(github),(245,222,179),font=font1)
draw.text((400, 160),str(yo1),(245,222,179),font=font2)
draw.text((400, 282),str(yo2),(245,222,179),font=font3)'/folder/sp0.png')
print ('Converted song URL to QR Code')
print ('Dynamic URL:')

The script would render an image like this:

The QR code takes you to the specific song on Spotify. Pretty rad, isn’t it?

Well, I didn’t stop there. 

Updating My GitHub Repository Using PyGitHub

Note: You need a GitHub personal access token to use PyGitHub.
Example user case: Programmatically replace an image URL inside the GitHub markdown file.

I decided to play with the unique corresponding to my username @verfasor. Here’s the Python code snippet I’m using:

# import libraries
from github import Github
import os
import random
import string

# generate a random string
letters = string.ascii_lowercase
img_id = ''.join(random.choice(letters) for i in range(10))

print('Image ID: ' + img_id)

seed = img_id

# markdown content

code = """<p>
  <h3>๐Ÿ‘‹ Hello there, I'm Mighil</h3>
<p dir="auto">Music producer and digital native. Currently leading the content and marketing operations at HeyForm and TinySnap.</p>
<p dir="auto">Is hard-synth + cool riffs + dubstep your thing? Try <a href="">VOL I</a></p>
<a href="">
    <img src="" /> 
<a href="">
    <img src="" />
  <a href="">
    <img src="" />
  <a href="">
    <img src="" />
  <a href="">
    <img src="" />
  <a href="">
    <img src="" />
  <a href="mailto:[email protected]">
    <img src="" />

  <a href=""><img alt="spotify" src="" /></a>
""" % seed

# github token

token = os.getenv('GITHUB_TOKEN', 'token')
g = Github(token)

# chooose repo

repo = g.get_repo("verfasor/verfasor")

# delete old

contents = repo.get_contents("")
repo.delete_file(contents.path, "removed old read me", contents.sha, branch="main")

# add new

file_content1 = ''

repo.create_file(file_content1, "updated song" , code, branch="main")

So the logic is:

  1. Delete the old
  2. Add new with the new song data
  3. Loops every two minute according to the schedule

Now the looks something like this:

A bit more about the new

As mentioned above in the article, the source image file gets replaced programmatically in my server. But, the image URL remains the same, which is So I set the Python script to add a random string in front of as ?string. Here’s how it looks inside the

<a href=""><img alt="spotify" src="" /></a>

Overkill 101

To be on the safe side, redirects to .htaccess file generates the SEED value, and it changes every second.

Email verf[email protected] or wechat: mighil to work together. I can help you with content, product ops, scripting, strategy, and technology.

Random: something I've read, browsed, or watched.

*Previously Powered by DigitalOcean, GeneratePress, and BunnyCDN.