-
Notifications
You must be signed in to change notification settings - Fork 0
/
lastfm.py
158 lines (125 loc) · 5.44 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"""
lastfm.py - last.fm now playing module for Sopel
Copyright 2015, Marcus Leivo <[email protected]>
Licensed under the Eiffel Forum License 2.
http://sopel.chat/
"""
from sopel import web
from sopel.module import commands, example
from bs4 import BeautifulSoup
import json
import urllib
def get_lastfm_username(args, db_nick):
if args and len(args.strip()) > 0:
return args
if db_nick:
return db_nick
return None
def get_np_info(username):
username = web.quote(username)
api_key = "API_KEY"
recent_tracks = web.get("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&format=json&user=%s&api_key=%s" % (username, api_key))
#now playing track, or most recently scrobbled track
now_playing = json.loads(recent_tracks)
# if the user does not exist
if 'recenttracks' not in now_playing:
return None
now_playing = now_playing['recenttracks']['track'][0]
track = now_playing['name']
album = now_playing['album']['#text']
artist = now_playing['artist']['#text']
import sys
reload(sys)
sys.setdefaultencoding('utf8')
# why the fuck doesnt this work with web.get() ???
track_page = urllib.urlopen("http://ws.audioscrobbler.com/2.0/?method=track.getInfo&format=json&artist=%s&track=%s&username=%s&api_key=%s" % (web.quote(artist), web.quote(track), username, api_key))
track_info = json.loads(track_page.read().decode(), encoding='utf-8')['track']
user_playcount = "0"
if 'userplaycount' in track_info:
user_playcount = track_info['userplaycount']
user_loved = False
if int(track_info['userloved']) > 0:
user_loved = True
return {"track": track, "album": album, "artist": artist, "user_playcount": user_playcount, "user_loved": user_loved}
def generate_np_output(trackinfo):
output = ""
if trackinfo['user_loved']:
# a red heart
output += "\x035\u2665\x03 "
output += "%s: %s" % (trackinfo['artist'], trackinfo['track'])
if trackinfo['album']:
output += " (%s)" % (trackinfo['album'])
if trackinfo['user_playcount']:
output += " (%s plays)" % (trackinfo['user_playcount'])
return output
@commands('fm', 'np', 'last', 'lastfm')
def lastfm(bot, trigger):
username = get_lastfm_username(trigger.group(2), bot.db.get_nick_value(trigger.nick, 'lastfm_user'))
if not username:
bot.reply("No username given. Set a username with .fmset <username>")
return
trackinfo = get_np_info(username)
if not trackinfo:
bot.reply("Couldn't find user.")
return
output = generate_np_output(trackinfo)
bot.say(trigger.nick + ' is playing ' + output)
@commands('fmset')
def set_lastfm_username(bot, trigger):
if not trigger.group(2):
bot.reply("No username given.")
return
user = trigger.group(2)
bot.db.set_nick_value(trigger.nick, 'lastfm_user', user)
bot.reply('Thanks, ' + user)
def get_lastfm_collage(url, username, modes):
if len(modes) == 1:
return 'http://%s/lastfm/collage.php?user=%s&type=7day&size=3x3' % (url, modes[0])
elif len(modes) == 2:
return 'http://%s/lastfm/collage.php?user=%s&type=%s&size=3x3' % (url, modes[0], modes[1])
elif len(modes) == 3:
return 'http://%s/lastfm/collage.php?user=%s&type=%s&size=%s' % (url, modes[0], modes[1], modes[2])
elif len(modes) == 4:
return 'http://%s/lastfm/collage.php?user=%s&type=%s&size=%s&caption=%s' % (url, modes[0], modes[1], modes[2], modes[3])
elif len(modes) == 5:
return 'http://%s/lastfm/collage.php?user=%s&type=%s&size=%s&caption=%s&artistonly=%s' % (url, modes[0], modes[1], modes[2], modes[3], modes[4])
elif len(modes) == 6:
return 'http://%s/lastfm/collage.php?user=%s&type=%s&size=%s&caption=%s&artistonly=%s&playcount=%s' % (url, modes[0], modes[1], modes[2], modes[3], modes[4], modes[5])
elif len(modes) > 6:
return 'Too many arguments! Input has to be: User Period Size Caption ArtistOnly Playcount'
else:
return 'http://%s/lastfm/collage.php?user=%s&type=7day&size=3x3' % (url, username)
@commands('col')
@example('.col neekeri 1month 4x4 true false true')
def lastfm_collage(bot, trigger):
"""user, type, size, caption, artistonly, playcount"""
modes = trigger.group()[5:].split()
username = get_lastfm_username(trigger.group(2), bot.db.get_nick_value(trigger.nick, 'lastfm_user'))
output = get_lastfm_collage("tapmusic.net", username, modes)
bot.reply(output)
@commands('col2')
@example('.col2 neekeri 1month 4x4 true false true')
def lastfm_collage_2(bot, trigger):
"""user, type, size, caption, artistonly, playcount"""
modes = trigger.group()[5:].split()
username = get_lastfm_username(trigger.group(2), bot.db.get_nick_value(trigger.nick, 'lastfm_user'))
output = get_lastfm_collage("nsfcd.com", username, modes)
bot.reply(output)
def get_lastfm_status():
url = "http://status.last.fm/"
soup = BeautifulSoup(web.get(url))
output = ""
statukset = soup.find_all('td', attrs={"class": "statussvc"})
tilat = soup.find_all('span', attrs={"class": True})
output = ""
for i in range(len(statukset)):
try:
name = statukset[i].text
status = tilat[i].text[2:]
output += "%s %s; " % (name, status)
except:
pass
return(output[:-2])
@commands('fmstatus', 'npstatus')
def fmstatusget(bot, trigger):
bot.say(get_lastfm_status() + " - http://status.last.fm/")