Created
July 29, 2017 14:29
-
-
Save Seklfreak/444f153afeb0ce064d4fa80be962ff28 to your computer and use it in GitHub Desktop.
Discord Last.FM status
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import discord | |
import asyncio | |
import logging | |
import pylast | |
import urllib.request | |
import json | |
import sys | |
DISCORD_TOKEN = '' | |
LASTFM_API_KEY = '' | |
LASTFM_API_SECRET = '' | |
LASTFM_USERNAME = '' | |
LASTFM_PASSWORDHASH = pylast.md5('') | |
TRAKT_USERNAME = '' | |
TRAKT_API_ID = '' | |
TRAKT_URL_WATCHING = 'https://api.trakt.tv/users/{!s}/watching'.format(TRAKT_USERNAME) | |
TRAKT_BASEURL_EPISODE = 'https://trakt.tv/shows/{!s}/seasons/{!s}/episodes/{!s}' | |
TRAKT_BASEURL_MOVIE = 'https://trakt.tv/movies/{!s}' | |
TRAKT_HEADERS = { | |
'Content-Type': 'application/json', | |
'trakt-api-version': '2', | |
'trakt-api-key': TRAKT_API_ID | |
} | |
logging.basicConfig(level=logging.WARNING) | |
client = discord.Client() | |
network = pylast.LastFMNetwork( | |
api_key = LASTFM_API_KEY, | |
api_secret = LASTFM_API_SECRET, | |
username = LASTFM_USERNAME, | |
password_hash = LASTFM_PASSWORDHASH) | |
user = network.get_user(LASTFM_USERNAME) | |
@asyncio.coroutine | |
def update_gamestatus(): | |
new_track = None; | |
playing_track = None; | |
new_video = None; | |
playing_video = None; | |
yield from client.wait_until_ready() | |
#yield from client.change_status(idle=True) | |
while not client.is_closed: | |
print('L', end='', flush=True) | |
try: | |
new_track = user.get_now_playing() | |
except Exception as e: | |
sys.exit('Error EXIT: {!s}'.format(e)) | |
#new_track = None | |
# listening to a song on last.fm? | |
if new_track != playing_track: | |
print(''); | |
playing_track = new_track | |
if playing_track == None: | |
print('stopped last.fm status') | |
yield from client.change_presence(game=None,afk=True,status=discord.Status.dnd) | |
else: | |
name = '{!s} by {!s} 🎵'.format( | |
playing_track.title, | |
playing_track.artist) | |
new_video = None; | |
playing_video = None; | |
yield from client.change_presence(game=discord.Game( | |
name=name, | |
url=str(playing_track.get_url())),afk=True,status=discord.Status.dnd | |
) | |
print('Now playing: {!s} ({!s})'.format( | |
name, playing_track.get_url())) | |
if playing_track == None: | |
print('T', end='', flush=True) | |
# instead watching something on trakt.tv? | |
try: | |
resp = urllib.request.urlopen( | |
urllib.request.Request( | |
TRAKT_URL_WATCHING, | |
headers=TRAKT_HEADERS) | |
).read().decode('utf-8') | |
if len(resp) > 0: | |
new_video = json.loads(resp) | |
else: | |
new_video = None | |
except Exception as e: | |
sys.exit('Error EXIT: {!s}'.format(e)) | |
#new_video = None | |
if new_video != playing_video: | |
print(''); | |
playing_video = new_video | |
if playing_video == None: | |
print("stopped trakt.tv status") | |
#yield from client.change_status(idle=True) | |
else: | |
new_track = None; | |
playing_track = None; | |
if ('movie' in playing_video): | |
name = '{!s} ({!s})'.format( | |
playing_video['movie']['title'], | |
playing_video['movie']['year']) | |
url = TRAKT_BASEURL_MOVIE.format( | |
playing_video['movie']['ids']['slug']) | |
#yield from client.change_status(game=discord.Game( | |
# name=name, | |
# url=url), idle=True | |
#) | |
print('Now watching: {!s}'.format(name)) | |
elif ('show' in playing_video): | |
name = "{!s} S{!s:0>2}E{!s:0>2}".format( | |
playing_video['show']['title'], | |
playing_video['episode']['season'], | |
playing_video['episode']['number']) | |
url = TRAKT_BASEURL_EPISODE.format( | |
playing_video['show']['ids']['slug'], | |
playing_video['episode']['season'], | |
playing_video['episode']['number']) | |
#yield from client.change_status(game=discord.Game( | |
# name=name, | |
# url=str(url)), idle=True | |
# ) | |
print("Now watching: {!s} ({!s})".format(name, url)) | |
yield from asyncio.sleep(5) | |
@client.event | |
@asyncio.coroutine | |
def on_ready(): | |
print(''); | |
print('Logged in as: {!s} ({!s})'.format(client.user.name, client.user.id)) | |
client.loop.create_task(update_gamestatus()) | |
client.run(DISCORD_TOKEN, bot=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment