LastfmからBlueskyへ

このPythonスクリプトは、ChatGPTの助けを借りて作成され、若干の修正が加えられています。 このスクリプトを使用すると、Last.fmからBlueskyへリスニングステータスを更新することができます。例は @scrobble.naturbrilian.dpdns.org でご覧いただけます。

アプリパスワードの取得方法(Bluesky)

  1. bskyで設定を開く
  2. 「プライバシーとセキュリティ」に移動する
  3. 「アプリパスワード」を選択する

lastfm用

  1. こちら
  2. からAPIを作成してください
  3. その後、APIキーをコピーしてください
注
これらは正常に動作させるためのわずかな修正に過ぎないため、自由に変更して構いません。

画像付き

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()
    

テキストのみ

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)

このサイトについて

このサイトは主にポートフォリオや雑記を含む個人サイトです。.

管理人について

暇な時に文章を書いたり、アニメを見たり、音楽を聴くのが好きな人です。

リンクス・ミー

Designed By a Human Not By AI

再生中

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.