forked from friendly-telegram/modules-repo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlastfm.py
70 lines (60 loc) · 3.67 KB
/
lastfm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Some parts are Copyright (C) LakesideMiners (@LakesideMiners) 2021-Present
# I DO NOT OFFER ANY SUPPORT!
# All licensed under project license
# Friendly Telegram (telegram userbot)
# Copyright (C) 2018-2019 The Authors
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import asyncio
import logging
import requests
import json
from .. import loader, utils
logger = logging.getLogger(__name__)
# Setup all the config bs
@loader.tds
class LastfmMod(loader.Module):
"""Get LastFM Last Played
Get an API key and Secret at https://www.last.fm/api/account/create"""
strings = {"name": "LastFM",
"doc_username": "Your LastFM Username",
"doc_api_key": "API Key from https://www.last.fm/api/account/create"
}
def __init__(self):
self.config = loader.ModuleConfig("USERNAME", None, lambda m: self.strings("doc_username", m),
"API_KEY", None, lambda m: self.strings("doc_api_key", m)
)
# When we type ".np" we get the curretnly playing song of the LastFM user in the config. Else, get the last played song
async def npcmd(self, message):
"""Print The Currently Playing Song On LastFM, last played if no song is playing."""
r = requests.get('http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=' + str(self.config["USERNAME"]) + '&' + 'api_key=' + str(self.config["API_KEY"]) + '&format=json' + '&limit=1')
json_output = r.json()
try:
playing = json_output['recenttracks']['track'][0]['@attr']['nowplaying']
pass
except KeyError:
playing = False
pass
if playing == 'true':
track_name = json_output['recenttracks']['track'][0]['name']
artist_name = json_output['recenttracks']['track'][0]['artist']['#text']
album_name = json_output['recenttracks']['track'][0]['album']['#text']
song_url = json_output['recenttracks']['track'][0]['url']
formated_message = "<b>Now Playing</b>" + "\n" + "<b>Track: </b>" + track_name + "\n" + "<b>Artist: </b> " + artist_name + "\n" + "<b>Album: </b>" + album_name + "\n" + "<a href='" + song_url + "'>" + "LastFM URL" + "</a>"
await utils.answer(message, str(formated_message))
else:
track_name = json_output['recenttracks']['track'][0]['name']
artist_name = json_output['recenttracks']['track'][0]['artist']['#text']
album_name = json_output['recenttracks']['track'][0]['album']['#text']
song_url = json_output['recenttracks']['track'][0]['url']
date_played = json_output['recenttracks']['track'][0]['date']['#text']
formated_message = "<b>Last Played</b>" + "\n" + "<b>Track: </b>" + track_name + "\n" + "<b>Artist: </b> " + artist_name + "\n" + "<b>Album: </b>" + album_name + "\n" + "<a href='" + song_url + "'>" + "LastFM URL" + "</a>" + "\n" + "<b>Played On: </b>" + date_played + "UTC"
await utils.answer(message, str(formated_message))