Using chatGPT to create simple code

View this article in: EN | ID | JP

Yesterday, I remembered that I had a Bluesky account that I planned to use to post songs on Last.fm. Previously, I had tried LegatoNowPlaying, but unfortunately, it only supported Misskey, which I strongly disagreed with at the time because there was no Bluesky option.

Unfortunately, it turns out that the developer LegatoNowPlaying only provides support for one player, namely AIMP and in the end I couldn’t use it for Spotify and etc.

Finally, it occurred to me about my account at @scrobble.naturbrilian.dpdns.org which I created a few months ago. I actually separated this account from my main account, and it only contains playback statuses.

How do I instruct ChatGPT to create a simple tool?

The way to give commands to chatGPT is very easy. First, I give commands like this, just as if you were chatting with a friend.

                
I have an account on Bluesky with this username: https://bsky.app/profile/namapengguna.bsky.social. I plan to use this account to post what I'm listening to using the Last.fm API. I've tried tools like LegatoNowPlaying from GitHub, but it only works on Misskey. Do you know of any tools or scripts that currently work?
                
            

Wait for chatGPT to respond. Usually, chatGPT will respond with recommendations for several tools that you are referring to, but sometimes the answers you receive may not be appropriate. So, try having another conversation as usual, like this I want to automatically post what I’m listening to, not post top artists every week. Or more simply, “not a top artist.” Then ChatGPT will respond with “how to create an autopost Now Playing to Bluesky.” The bot will then create a simple Python script. Just follow the instructions, and the bot will create the full version at the same time.

Setting up your lastfm API

Get your lastfm API from this links, then click create, several APIs will appear, then copy the API Key there.
http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=USERNAME&api_key=APIKEY&format=json&limit=1 Then, copy the code provided by ChatGPT into your favorite text editor. Here, I am using VSCode.

Then enter the username and API key you received earlier, then go to Bluesky to get the password.

NOTE

The password on Bluesky is not an account password, but an application password.

How do you get your app password?

Go to Settings > Privacy & Security > App Password, then click App Password, create any name you like, then click Next. The app password will then appear.

After that, enter the Bluesky Application Password in the section BSKY_PASS.

REMINDER

Labeled BSKY_HANDLE & BSKY_PASS filled as above

Once everything is set up, try testing it by running the Python script in your directory. If an error appears, try troubleshooting it yourself if you are familiar with programming. If not, send the log displayed in CMD back to ChatGPT.

After all problem have been resolved, let’s test whether the script is running properly and play a song using a music player that supports Last.fm scrobble, web scrobble, or Spotify. If successful, the log should appear as follows

How about it? Would you like to try it, or shall I put the modified script below if you want to refine it?

                
import requests, time, datetime

LASTFM_USER = "USERNAMEKAMU"
LASTFM_API_KEY = "APIKEYKAMU"
BSKY_HANDLE = "username.bsky.social"
BSKY_PASS = "APPSPASSWORDKAMU"

last_track = None

while True:
    r = requests.get("http://ws.audioscrobbler.com/2.0/", params={
        "method": "user.getrecenttracks",
        "user": "USERNAMEKAMU",
        "api_key": "APIKEYKAMU",
        "format": "json",
        "limit": 1
    }).json()

    track = r["recenttracks"]["track"][0]
    if "@attr" in track and track["@attr"].get("nowplaying") == "true":
        artist = track["artist"]["#text"]
        title = track["name"]
        text = f"🎵 Now Playing: {artist} – {title}"

        if text != last_track: 
            # login Bluesky
            session = requests.post("https://bsky.social/xrpc/com.atproto.server.createSession",
                json={"identifier": "username.bsky.social", "password": "APPSPASSWORDKAMU"}).json()
            headers = {
                "Authorization": f"Bearer {session['accessJwt']}",
                "Content-Type": "application/json"
            }

            post = {
                "repo": "DID AKUN KAMU",
                "collection": "app.bsky.feed.post",
                "record": {
                    "text": text,
                    "createdAt": datetime.datetime.utcnow().isoformat() + "Z"
                }
            }

            requests.post("https://bsky.social/xrpc/com.atproto.repo.createRecord",
                          headers=headers, json=post)
            print("Posted:", text)
            last_track = text

    time.sleep(30)
                
            
← Back