Skip to content

Instantly share code, notes, and snippets.

@xreiju
Last active May 9, 2023 11:39
Show Gist options
  • Save xreiju/f1ed0977832f1bde2d5df546626d029c to your computer and use it in GitHub Desktop.
Save xreiju/f1ed0977832f1bde2d5df546626d029c to your computer and use it in GitHub Desktop.
Spotifyの#nowplayingをMisskeyに投稿したいだろ
# requirements: request, spotipy(needs MANUAL installation)
# $ python3 misskey_spotify_nowplaying.py <spotify ID> (silent)
# silentが指定されている場合、Misskeyにpostされずに投稿内容のプレビューのみを実行します。
# 2018/08/20: tokenではなくmisskey-hookを使うように変更しました。以前は"i"を使っていました。履歴参照。
import json, sys, urllib.request
import spotipy
import spotipy.util as util
if len(sys.argv) > 1:
username = sys.argv[1]
else:
print("Usage: {} username".format(sys.argv[0]))
sys.exit()
if len(sys.argv) > 2:
silent = sys.argv[2] == "silent"
else:
silent = False
##############################################
post_url = "ここをMisskey HookのWebHookのURLで埋めてください。"
##############################################
# Spotifyの権限要求時にscopeを指定する(今回はcurrently-playing叩きたい)
scope = 'user-read-currently-playing'
# Browser で Spotify の権限要求をします
spotify_token = util.prompt_for_user_token(username, scope)
if spotify_token:
sp = spotipy.Spotify(auth=spotify_token)
# get nowplaying
res = sp.currently_playing()
if not res:
print("Now playing anything, quit.")
post_str = "🎶 [{}](https://open.spotify.com/track/{}) by 🎤{}\n💿 {}\n⏰ ".format(
res["item"]["name"], res["item"]["id"],
res["item"]["artists"][0]["name"],
res["item"]["album"]["name"])
all_millis = res["item"]["duration_ms"]
prog_millis = res["progress_ms"]
all_minutes = int((all_millis/(1000*60))%60)
all_seconds = int((all_millis/1000)%60)
prog_minutes = int((prog_millis/(1000*60))%60)
prog_seconds = int((prog_millis/1000)%60)
# 全体が20秒より短い曲の場合msecで表示します
if all_minutes == 0 and all_seconds < 20:
post_str += "{}msec / {}msec".format(prog_millis, all_millis)
else:
post_str += "{0:02d}:{1:02d} / {2:02d}:{3:02d}".format(prog_minutes, prog_seconds, all_minutes, all_seconds)
if not res["is_playing"]:
post_str += " ⏸️Paused."
else:
post_str += " ▶️Playing."
post_str += "\t ❤️Popularity: {} / 100 \t #nowplaying".format(str(res["item"]["popularity"]))
print(post_str)
if not silent:
# Misskey Hookを叩く
if res["item"]["album"]["images"] != []:
img = [res["item"]["album"]["images"][0]["url"]]
else: img = []
# なんかプレビューに勝手に画像つくっぽいんで、imageUrlsは指定しないことにした
data = {"text": post_str}
json_data = json.dumps(data).encode("utf-8")
headers = {"Content-Type": "application/json"}
request = urllib.request.Request(post_url, data=json_data, method="POST", headers=headers)
with urllib.request.urlopen(request) as res:
res_body = res.read().decode("utf-8")
print(res_body)
print("OK")
else:
print("Can't get token for", username)
set SPOTIPY_CLIENT_ID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
set SPOTIPY_CLIENT_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
set SPOTIPY_REDIRECT_URI=http://localhost/
python misskey_spotify_nowplaying.py your_Spotify_ID
@YuzuRyo61
Copy link

Issue
spotipy: v2.4.4
OS: Linux Mint 19.1 (Ubuntu)
Python: 3.6.7
image

@xreiju
Copy link
Author

xreiju commented Apr 13, 2019

@YuzuRyo61 There's a problem with "spotipy". Since the package spotipy is not supporting the currently_playing function when you install it with pip.
You have to install that package manually, that means, we need to download the repository and run setup.py.

Sorry for inconvenience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment