-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsoma.py
183 lines (148 loc) · 4.91 KB
/
soma.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
from sopel.config.types import StaticSection, ChoiceAttribute, ValidatedAttribute
from sopel.module import commands, example
from sopel.formatting import *
from sopel.tools import get_logger
from urllib.parse import quote_plus
import html
from html.parser import HTMLParser
import re
import requests
import pendulum
###
# Module config/setup/shutdown
###
LOGGER = get_logger(__name__)
API_CHANNELS_URL = "https://somafm.com/channels.json"
API_SONGS_URL = "https://somafm.com/songs/{}.json"
class MLStripper(HTMLParser):
def __init__(self):
super().__init__()
self.reset()
self.fed = []
def handle_data(self, d):
self.fed.append(d)
def get_data(self):
return ''.join(self.fed)
def configure(config):
pass
def setup(bot):
pass
def shutdown(bot):
pass
###
# Commands
###
@commands('somafmlist', 'somalist', 'somal')
@example('.somafmlist')
def somafm_list(bot, trigger):
"""Lists available stations"""
stations = _fetch(bot, API_CHANNELS_URL)
tmp_replies = {}
replies = []
for station in stations['channels']:
listeners = int(station['listeners'])
tmp_replies[station['title']] = listeners
tmp_replies = sorted(tmp_replies.items(), key=lambda x: x[1], reverse=True)
for group in _chunker(tmp_replies, 15):
tmp = []
for station,listeners in group:
tmp.append(
"{} ({})".format(
bold(station),
listeners
)
)
replies.append(" | ".join(tmp))
for line in replies:
bot.say(line)
@commands('somafm', 'soma', 'somanp')
@example('.somafm groove salad')
def somafm_info(bot, trigger):
"""Fetches information for a provided station name from SomaFM"""
user_input = trigger.group(2)
if not user_input:
return bot.reply("I need a station to lookup!")
stations = _fetch(bot, API_CHANNELS_URL)
user_input = user_input.strip().lower()
station = []
for channel in stations['channels']:
if "*" in user_input:
check = user_input.replace("*", "")
if not check:
return bot.reply("I can't lookup ALL channels at once nimrod.")
if check in channel['title'].lower() \
or check in channel['id'].lower():
station.append(channel)
else:
if user_input == channel['title'].lower() \
or user_input == channel['id'].lower():
station.append(channel)
if not station:
return bot.reply("I couldn't find any stations by that name ({})".format(user_input))
for s in station:
channel_id = s["id"]
tracks = _fetch(bot, API_SONGS_URL.format(channel_id))
artist = tracks["songs"][0]["artist"]
song = tracks["songs"][0]["title"]
album = tracks["songs"][0]["album"]
station_url = "https://somafm.com/{}/".format(channel_id)
reply = (f"[SomaFM] {bold(s['title'])} ({s['listeners']} listeners)"
f" {s['description']} | {bold('DJ')}: {s['dj']} | {bold('Genre')}: {s['genre'].replace('|','/')}"
f" | {bold('Playing')}: {song} by {artist} [{album}] | Listen @ {station_url}"
)
bot.say(reply, max_messages=2)
###
# Internal Functions
###
def _chunker(seq, size):
return (seq[pos:pos + size] for pos in range(0, len(seq), size))
def _fetch(bot, url, data=None, headers=None):
try:
response = requests.get(url, headers=headers).json()
LOGGER.info(url)
return response
except:
return None
def _shorten(bot, long_url):
try:
key = bot.config.podcasts.bitly_key
headers = {
"accept": "application/json",
"content-type": "application/json",
"authorization": "Bearer {}".format(key)
}
payload = {
"long_url": long_url
}
url = "https://api-ssl.bitly.com/v4/shorten"
response = requests.post(url, json=payload, headers=headers)
short_url = response.json().get("link")
except:
short_url = long_url
return short_url or long_url
# from Supybot/Limnoria utils.str
def _normalizeWhitespace(s, removeNewline=True, newLineChar=" "):
r"""Normalizes the whitespace in a string; \s+ becomes one space."""
if not s:
return str(s) # not the same reference
s = html.unescape(s)
try:
s = _strip_tags(s)
except:
pass
starts_with_space = (s[0] in ' \n\t\r')
ends_with_space = (s[-1] in ' \n\t\r')
if removeNewline:
newline_re = re.compile('[\r\n]+')
s = newLineChar.join(filter(bool, newline_re.split(s)))
s = ' '.join(filter(bool, s.split('\t')))
s = ' '.join(filter(bool, s.split(' ')))
if starts_with_space:
s = ' ' + s
if ends_with_space:
s += ' '
return s
def _strip_tags(html):
s = MLStripper()
s.feed(html)
return s.get_data()