Lastfm to Blueskys

This python script is created with the help of chatGPT with some modifications. This script allows you to update your listening status from last fm to bluesky. An example can be found at @scrobble.naturbrilian.dpdns.org

How to Get your App Password (Bluesky)

  1. Open settings on bsky
  2. Go to Privacy & Security
  3. And select Apps Password

For lastfm

  1. Create your api from here
  2. Then copy your apikey
Note
Since these are just a few edits to work perfectly, you are free to modify them.

With Image

Preview
import time
import requests
from atproto import Client, models

# Ubah kredensial di bawah ini
LASTFM_USER = "Your_Username"
LASTFM_API_KEY = "Your_LastFM_Apikey"

BLUESKY_HANDLE = "Your_Handle_Bsky"
BLUESKY_PASSWORD = "Your_bsky_password"

def get_now_playing(user, api_key):
    url = "http://ws.audioscrobbler.com/2.0/"
    params = {
        "method": "user.getrecenttracks",
        "user": user,
        "api_key": api_key,
        "format": "json",
        "limit": 1
    }
    resp = requests.get(url, params=params).json()
    track = resp["recenttracks"]["track"][0]
    return track

def main():
    client = Client()
    client.login(BLUESKY_HANDLE, BLUESKY_PASSWORD)

    last_posted = None 

    while True:
        try:
            track = get_now_playing(LASTFM_USER, LASTFM_API_KEY)

            artist = track["artist"]["#text"]
            title = track["name"]
            album = track["album"]["#text"]
            cover_url = track["image"][-1]["#text"]

            unique_id = f"{artist}-{title}"
            if unique_id != last_posted:
                message = f"Recently Played:
Artist: {artist}
Title: {title}
Album: {album}"

                img_data = requests.get(cover_url).content
                upload = client.com.atproto.repo.upload_blob(img_data)

                embed = models.AppBskyEmbedImages.Main(
                    images=[models.AppBskyEmbedImages.Image(
                        image=upload.blob,
                        alt=f"Album cover for {album}"
                    )]
                )

                client.send_post(text=message, embed=embed)
                print("Post sukses:", message)

                last_posted = unique_id

            time.sleep(30)

        except Exception as e:
            print("❌ Error:", e)
            time.sleep(60)

if __name__ == "__main__":
    main()
    

Text Only

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": LASTFM_USER,
        "api_key": LASTFM_API_KEY,
        "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": BSKY_HANDLE, "password": BSKY_PASS}).json()
            
            headers = {
                "Authorization": f"Bearer {session['accessJwt']}",
                "Content-Type": "application/json"
            }

            post = {
                "repo": session["did"],
                "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)

About this Website

This site is a personal website that contains mainly portfolios and random thoughts.

About Owner

Just someone who likes to write anything, watch anime and listen to songs when she's has free time.

Links Me

Ditulis oleh Manusia, bukan AI

Now Listening

Loading...

Commision Status

Close
see this status and please visit this page if you want to see the price I offer and works page for more sample.