m4pl1mp/plugins/soundcloud_plugin.py
2022-07-24 08:09:17 -05:00

89 lines
4.1 KiB
Python

# -*- 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>...
"""
NICK=mask.nick
query=' '.join(args['<query>'])
result=self.__soundcloud__request('search',query)
pop=[];_path=[];_name=[];title='';
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])
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[0]}'
TITLE=title.split('|')[0].split('by')[-1].strip()
NAME=html.unescape(_name[0]);
######## SOUNDCLOUD <-> BOOMBOX_PLUGIN HOOK ######## SOUNDCLOUD <-> BOOMBOX_PLUGIN HOOK ########
self.bot.bbs.enter(NICK,URL,TITLE)
######## SOUNDCLOUD <-> BOOMBOX_PLUGIN HOOK ######## SOUNDCLOUD <-> BOOMBOX_PLUGIN HOOK ########
msg=f"\x02\x0302{NICK}\x0F\x02\x0304 ▶ "
msg+=f"\x0F\x02\x0313{NAME}\x0F\x02\x0304 ▶ "
msg+=f"\x0F\x02\x0312{URL}\x0F\x02\x0313\x0F\x02\x0304 ▶ "
msg+=f"\x0F\x1D\x0314{TITLE}\x0F\x02\x0304 "
except:
MSG='no result'
msg=f"\x02\x0302{NICK}\x0F\x02\x0304 ▶ "
msg+=f"\x0F\x02\x0313{MSG}\x0F\x02\x0304 "
self.bot.privmsg(target,self.bot.emo(f"{msg}"))
###########################################################################################
###########################################################################################