-
Notifications
You must be signed in to change notification settings - Fork 0
/
nowplaying.py
189 lines (162 loc) · 6.86 KB
/
nowplaying.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
###
# Copyright (c) 2021, cottongin
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions, and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions, and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author of this software nor the name of
# contributors to this software may be used to endorse or promote products
# derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
import asyncio
import io
import tempfile
import sys, traceback
import time
import requests
from shazamio import Shazam
# https://github.com/Horrendus/streamscrobbler-python
from streamscrobbler import *
threaded = True
from sopel import config, trigger
from sopel.module import commands, example
from sopel.config.types import StaticSection, ChoiceAttribute, ListAttribute
class NowplayingSection(StaticSection):
channel_stream_urls = ListAttribute('channel_stream_urls', default=None)
announceListening = ListAttribute('announceListening', default='Channel')
mismatches = ListAttribute('mismatches', default='Channel')
def configure(config):
config.define_section('nowplaying', NowplayingSection, validate=False)
config.nowplaying.configure_setting('channel_stream_urls', 'List of <channel;stream URL> pairs, semicolon separated.')
config.nowplaying.configure_setting('announceListening', '[Channel/Private/Notice/Silent] Where/how/if the bot should announce it is listening to the stream.')
config.nowplaying.configure_setting('mismatches', '[Channel/Private/Notice/Silent] Where/how/if the bot should announce that it cannot identify what is playing.')
def setup(bot):
bot.config.define_section('nowplaying', NowplayingSection)
def _channelConfig(config, channel):
# checking the calling-user's channel against the list of channel;setting pairs
for item in config:
pair = item.split(';')
# found a match, return variable
if channel in pair[0]:
return pair[1]
else:
bot.reply('This channel (' + channel + ') has no stream URL configured - please contact your resident bot herder!')
bot.error(trigger)
return
def _fetch_mp3(url: str):
# this is (still) really dumb
# TODO: handle unexpected content better in the request
data = None
with requests.Session() as conn:
response = conn.get(url, stream=True, timeout=5)
data = bytes()
loops = 0
for chunk in response.iter_content(1024*5):
loops += 1
if loops >= 25 or not chunk:
break
data += chunk
return data
async def _parse_shazam(stream_info=None, audio_segment=None):
# this is also really dumb
out = {}
shazam = Shazam()
if not audio_segment:
# TODO: fix this so it honors fs permissions etc
try:
out = await shazam.recognize_song('stream.mp3')
except Exception as err:
print(f"[FILE method] {err}")
traceback.print_exc(file=sys.stdout)
pass
else:
try:
# so we make a NamedTemporaryFile because on *NIX we don't
# always get a path or link to a regular TemporaryFile
with tempfile.NamedTemporaryFile() as fake_file:
# TODO: abstract this out into a AudioSegmentClass
fake_file.write(audio_segment)
out = await shazam.recognize_song(fake_file.name)
except Exception as err:
print(f"[FAKE method] {err}")
traceback.print_exc(file=sys.stdout)
pass
append = ""
if stream_info:
metadata = stream_info.get('metadata')
if metadata:
append = " | {}".format(metadata.get('song', '???'))
message = (
f"Sorry, Shazam could not identify what is playing.{append}"
)
match = out.get('track')
if match:
message = "🎵 Now Playing: {title} by {subtitle} | {url}{append}"
message = message.format(
title=match.get('title', 'Unknown'),
subtitle=match.get('subtitle', 'Unknown'),
url=match.get('share', {}).get('href', 'via Shazam'),
append=append,
)
return match, message
@commands('np', 'nowplaying')
@example('!np - asks Shazam to listen to the stream and return any identified tracks')
def nowplaying(bot, trigger):
url = _channelConfig(bot.config.nowplaying.channel_stream_urls, trigger.sender)
announceDest = _channelConfig(bot.config.nowplaying.announceListening, trigger.sender)
mismatchDest = _channelConfig(bot.config.nowplaying.mismatches, trigger.sender)
announceText = "One second, listening..."
if 'Private' in announceDest:
bot.say(announceText, trigger.nick)
elif 'Notice' in announceDest:
bot.notice(announceText, trigger.nick)
elif 'Channel' in announceDest:
bot.reply(announceText, trigger.sender)
elif 'Silent' in announceDest:
pass
else:
bot.reply('Invalid configuration. Choose from: Channel/Notice/Private/Silent')
bot.error(trigger)
return
audio = _fetch_mp3(url)
stream_info = get_server_info(url)
match, reply = asyncio.run(
_parse_shazam(
stream_info=stream_info,
audio_segment=audio,
)
)
if not match:
if 'Private' in mismatchDest:
bot.say(reply, trigger.nick)
return
elif 'Notice' in mismatchDest:
bot.notice(reply, trigger.nick)
return
elif 'Channel' in mismatchDest:
bot.reply(reply, trigger.sender)
return
elif 'Silent' in mismatchDest:
return
else:
bot.reply('Invalid configuration. Choose from: Channel/Notice/Private/Silent')
bot.error(trigger)
return