-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
183 lines (151 loc) · 6.09 KB
/
plugin.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
###
# 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 get_server_info
from supybot import callbacks
from supybot.commands import wrap
try:
from supybot.i18n import PluginInternationalization
_ = PluginInternationalization('StreamStuff')
except ImportError:
# Placeholder that allows to run the plugin on a bot
# without the i18n module
_ = lambda x: x
class StreamStuff(callbacks.Plugin):
"""idk things and stuff"""
threaded = True
def __init__(self, irc):
self.__parent = super(StreamStuff, self)
self.__parent.__init__(irc)
def die(self):
super().die()
@staticmethod
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
@classmethod
async def _parse_shazam(self, 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
@wrap
def nowplaying(self, irc, msg, args):
"""
Identify what is playing on a stream via Shazam.
"""
url = self.registryValue('streamURL', msg.channel)
if not url:
return irc.error(
"No stream provided, please configure this plugin."
)
if self.registryValue('announceListening', msg.channel):
irc.reply(
"One second, listening...",
notice=True,
private=True,
)
audio = self._fetch_mp3(url)
stream_info = get_server_info(url)
match, reply = asyncio.run(
self._parse_shazam(
stream_info=stream_info,
audio_segment=audio,
)
)
if not match:
# we do not have a match, how do we handle that?
target = msg.channel or msg.nick
if self.registryValue('mismatches.failSilently', msg.channel):
if self.registryValue('mismatches.failToNotice', msg.channel):
return irc.reply(reply, notice=True, private=True)
elif self.registryValue('mismatches.failToNotice', msg.channel):
return irc.reply(reply, notice=True, private=True)
else:
# this might not strictly be necessary...
return irc.reply(reply, to=target)
return
return irc.reply(reply)
Class = StreamStuff
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: