update sc
added soundcloud plugin
This commit is contained in:
parent
819c9aa4a3
commit
1be3fe8dbd
79
plugins/soundcloud_plugin.py
Normal file
79
plugins/soundcloud_plugin.py
Normal file
@ -0,0 +1,79 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from irc3.plugins.command import command
|
||||
import irc3
|
||||
from urllib.request import Request, urlopen
|
||||
from urllib.error import URLError, HTTPError
|
||||
from urllib.parse import quote_plus
|
||||
import html
|
||||
import requests
|
||||
from lxml.html import fromstring
|
||||
###########################################################################################
|
||||
###########################################################################################
|
||||
@irc3.plugin
|
||||
class Plugin:
|
||||
#######################################################################################
|
||||
#######################################################################################
|
||||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
#######################################################################################
|
||||
#######################################################################################
|
||||
def __soundcloud__request(self,command,query):
|
||||
USER_AGENT_CURL="curl/7.29.0"
|
||||
STATUS_BAD_CODE=":( - error code: {}"
|
||||
STATUS_BAD_REASON=":( - failed reason: {}"
|
||||
STATUS_OK=":)"
|
||||
COMMANDS=['search']
|
||||
for _ in COMMANDS:
|
||||
if _ == command.lower():
|
||||
url=f'https://soundcloud.com/{_}?q={quote_plus(query)}'
|
||||
r = Request(url,data=None,headers={ 'User-Agent': USER_AGENT_CURL })
|
||||
try:
|
||||
response = urlopen(r,timeout=10).read().decode('utf-8')
|
||||
except HTTPError as e:
|
||||
return STATUS_BAD_CODE.format(e.code)
|
||||
except URLError as e:
|
||||
return STATUS_BAD_REASON.format(e.reason)
|
||||
else:
|
||||
return STATUS_OK + response
|
||||
#######################################################################################
|
||||
#######################################################################################
|
||||
@command(permission='view')
|
||||
def sc(self, mask, target, args):
|
||||
"""Soundcloud artist/song search.. example: ?sc yung innanet nano tapes cronjob
|
||||
%%sc <query>...
|
||||
"""
|
||||
query = ' '.join(args['<query>'])
|
||||
result=self.__soundcloud__request('search',query)
|
||||
pop=[]; _path=[];_name=[];
|
||||
for _ in result.splitlines():
|
||||
if _.find('<li><h2>') != -1:
|
||||
pop.append(_.strip())
|
||||
for _ in pop:
|
||||
_path.append(_.split('="/').pop().split('">')[0])
|
||||
_name.append(_.split('="/').pop().split('">')[1].split('<')[0])
|
||||
title=''
|
||||
try:
|
||||
url=f'https://soundcloud.com/{_path[0]}'
|
||||
try:
|
||||
read_size = 0
|
||||
r = requests.get(url, timeout=3, stream=True)
|
||||
content_length = r.headers.get('Content-Length')
|
||||
if not content_length:
|
||||
content_length = 0
|
||||
while read_size <= (2000 * 10):
|
||||
for content in r.iter_content(chunk_size=2000):
|
||||
tree = fromstring(content)
|
||||
title = tree.find(".//title")
|
||||
if title is not None:
|
||||
title = title.text.strip()[:100]
|
||||
break
|
||||
except:
|
||||
pass
|
||||
url=f'https://soundcloud.com/{_path}'
|
||||
title = title.split('|')[0].split('by')[-1].strip()
|
||||
msg="\x02\x0302{NICK}\x0F\x02\x0304 ▶ \x0F\x02\x0313{NAME}\x0F\x02\x0304 ▶ \x0F\x02\x0312https://soundcloud.com/{PATH}\x0F\x02\x0313\x0F\x02\x0304 ▶ \x0F\x1D\x0314{TITLE}\x0F\x02\x0304 ".format(NICK=mask.nick,NAME=html.unescape(_name[0]),PATH=_path[0],TITLE=title)
|
||||
except:
|
||||
msg="\x02\x0302{NICK}\x0F\x02\x0304 ▶ \x0F\x02\x0313no result\x0F\x02\x0304 ".format(NICK=mask.nick,)
|
||||
self.bot.privmsg(target,self.bot.emo(f"{msg}"))
|
||||
###########################################################################################
|
||||
###########################################################################################
|
Loading…
Reference in New Issue
Block a user