Table of Contents

  1. Updating My GitHub Repository Using PyGitHub

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)
bars.close()
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(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=10,
    border=2,
)
qr.add_data(str(url))
qr.make(fit=True)

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


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


from PIL import Image, ImageFont, ImageDraw, ImageOps
img = Image.open("/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)
img.save('/folder/sp0.png')
print ('Converted song URL to QR Code')
print ('Dynamic URL: https://verfasor.com/sqr')

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 README.md 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="https://signalsiren.bandcamp.com/album/vol-i">VOL I</a></p>
 
<p> 
<a href="https://twitter.com/verfasor">
    <img src="https://img.shields.io/twitter/follow/verfasor?style=social" /> 
</a>
<a href="https://github.com/migftw">
    <img src="https://img.shields.io/github/followers/migftw?label=MIGFTW&logo=GitHub&style=social" />
</a>   
  <a href="https://verfasor.com/">
    <img src="https://img.shields.io/badge/blog-verfasor.com-blueviolet" />
  </a>      
  <a href="https://signalsiren.bandcamp.com/">
    <img src="https://img.shields.io/badge/music-SIGANSIREN-red" />
  </a>  
  <a href="https://m1qnet.bandcamp.com/">
    <img src="https://img.shields.io/badge/music-m1qnet-red" />
  </a>     
  <a href="https://wr8.in/">
    <img src="https://img.shields.io/badge/get-wr8.in-green" />
  </a> 
  <a href="mailto:[email protected]">
    <img src="https://img.shields.io/badge/contact-email-important" />
  </a>   
</p> 

  <p>  
  <a href="https://verfasor.com/sqr"><img alt="spotify" src="https://verfasor.com/sqr?%s" /></a>
  </p>
</p>
 
""" % seed

# github token

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

# chooose repo

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

# delete old readme.md

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

# add new readme.md

file_content1 = 'README.md'

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

So the logic is:

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

Now the README.md looks something like this:

A bit more about the new README.md

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 verfasor.com/sqr. So I set the Python script to add a random string in front of verfasor.com/sqr as ?string. Here’s how it looks inside the README.md:

<a href="https://verfasor.com/sqr"><img alt="spotify" src="https://verfasor.com/sqr?hmnrsmljfk" /></a>

Overkill 101

To be on the safe side, verfasor.com/sqr redirects to https://u88.in/sp0.png?c=?SEED. .htaccess file generates the SEED value, and it changes every second.

Written by MighilMighil is an indie musician and tinkerer with diverse work experience in technology and writing. He has had the privilege of serving in various capacities, encompassing generalist and specialist roles. He is currently based in Chengdu.

Newsletter

Opt-in to receive long-form essays in your inbox. Unsubscribe anytime. Follow me on 𝕏 if you like.

Powered by DigitalOcean, BunnyCDN, WordPress.