Skip to content

Instantly share code, notes, and snippets.

@vorbeiei
Created July 16, 2017 16:15
Show Gist options
  • Save vorbeiei/d6a281e6237ec1370f446ab9ccbff175 to your computer and use it in GitHub Desktop.
Save vorbeiei/d6a281e6237ec1370f446ab9ccbff175 to your computer and use it in GitHub Desktop.
simple musicbot using query to follow user in TS3 which is set up to play music from the PC(using spotify or youtube) controlled from chat commands. Not perfect and buggy but works for me
#simple musicbot using query to follow user in TS3 which is set up to play music from the PC(using spotify or youtube) controlled from chat commands
#not perfect and buggy but works for me
from dbus import DBusException
import dbus
import getpass
import sys
import time
import telnetlib
import os
import subprocess
HOST = "XXXXXXXXX"
port = "10011"
name = "bot"
pw = "XXXXXXXXX"
cur = ""
querid = ""
vorid = ""
chanid = ""
vorchan = ""
recv = ""
count = 0
yt = False
y = subprocess.Popen("ping")
tn = telnetlib.Telnet(HOST,port,1000)
tn.set_debuglevel(0)
tn.read_until("command.\n\r")
tn.write("use 291\n") #server id
tn.read_until("msg=ok\n\r")
tn.write("login bot XXXXXXXX\n")
tn.read_until("msg=ok\n\r")
tn.write("whoami\n")
tn.read_until("client_id=")
querid = tn.read_until(" ")
tn.read_until("client_channel_id=")
chanid = tn.read_until(" ")
tn.read_until("msg=ok\n\r")
tn.write("clientfind pattern=vorbeieieieieiei\n") #clientname to follow
tn.read_until("clid=")
vorid = tn.read_until(" ")
tn.read_until("msg=ok\n\r")
tn.write("servernotifyregister event=textchannel\n")
tn.read_until("msg=ok\n\r")
while True:
time.sleep(0.5)
if (count > 300):
count = 0
tn.write("whoami\n")
tn.read_until("msg=ok\n\r")
else:
count+=1
tn.read_until("notifytextmessage targetmode=2 msg=",0.5) #0.01
recv = tn.read_until(" ",0.01)
tn.read_until("msg=ok\n\r",0.01)
session_bus = dbus.SessionBus()
try:
spotify_bus = session_bus.get_object("org.mpris.MediaPlayer2.spotify",
"/org/mpris/MediaPlayer2")
spotify_properties = dbus.Interface(spotify_bus,
"org.freedesktop.DBus.Properties")
metadata = spotify_properties.Get("org.mpris.MediaPlayer2.Player", "Metadata")
title = metadata['xesam:title']
artist = metadata['xesam:artist'][0]
title = title.replace(" ","\s")
artist = artist.replace(" ","\s")
except DBusException as e:
title = "Spotify\snot\srunning"
artist = "error"
try:
vlc_bus = session_bus.get_object("org.mpris.MediaPlayer2.vlc",
"/org/mpris/MediaPlayer2")
vlc_properties = dbus.Interface(vlc_bus,
"org.freedesktop.DBus.Properties")
vmetadata = vlc_properties.Get("org.mpris.MediaPlayer2.Player", "Metadata")
run = vmetadata['xesam:url']
except DBusException as e:
run = "error"
tn.write("whoami\n")
tn.read_until("client_id=")
querid = tn.read_until(" ")
tn.read_until("client_channel_id=")
chanid = tn.read_until(" ")
tn.read_until("msg=ok\n\r")
tn.write("clientfind pattern=vorbeieieieieiei\n")
tn.read_until("clid=")
vorid = tn.read_until(" ")
tn.read_until("msg=ok\n\r")
tn.write("clientlist\n")
tn.read_until("clid=" + vorid + "cid=")
vorchan = tn.read_until(" ")
tn.read_until("msg=ok\n\r")
if (vorchan != chanid):
tn.write("clientmove clid=" + querid + " cid=" + vorchan + "\n")
tn.read_until("msg=ok\n\r")
if (cur != title):
tit = "sendtextmessage targetmode=2 msg=" + title + "\s-\s" + artist + "\n"
tit = tit.encode('ascii', 'replace')
tn.write(tit)
cur = title
tn.read_until("msg=ok\n\r")
if (run == "error"):
yt = False
if (recv != ""):
print recv
if (recv == "play "):
if (yt == False):
os.system("bash sp play")
tn.write("sendtextmessage targetmode=2 msg=start\splaying\n")
else:
tn.write("sendtextmessage targetmode=2 msg=youtube\scurrently\splaying\n")
elif (recv == "pause ") or (recv == "stop "):
y.kill()
yt = False
os.system("bash sp pause")
tn.write("sendtextmessage targetmode=2 msg=pausing\n")
elif (recv == "next "):
if (yt == False):
os.system("bash sp next")
tn.write("sendtextmessage targetmode=2 msg=next\sTrack\n")
else:
tn.write("sendtextmessage targetmode=2 msg=youtube\scurrently\splaying\n")
elif (recv == "prev "):
if (yt == False):
os.system("bash sp prev")
tn.write("sendtextmessage targetmode=2 msg=previous\sTrack\n")
else:
tn.write("sendtextmessage targetmode=2 msg=youtube\scurrently\splaying\n")
elif (recv.startswith("search\s")):
y.kill()
yt = False
recv = recv.lstrip("search")
txts = recv.decode('utf-8','ignore').encode('ascii', 'replace')
tn.write("sendtextmessage targetmode=2 msg=searching\sfor\s" + txts + "\n")
recv = recv.replace("\s"," ")
recv = recv.replace("'","")
os.system("bash sp search " + recv)
print recv
elif (recv.startswith("open\s")):
y.kill()
yt = False
recv = recv.lstrip("open")
recv = recv.replace("\s"," ")
txts = recv.encode('ascii', 'replace')
tn.write("sendtextmessage targetmode=2 msg=opening\s" + txts + "\n")
os.system("bash sp open " + recv)
elif (recv.startswith("yt\s")):
recv = recv.lstrip("yt\s[URL]")
recv = recv.replace("\s","")
recv = recv.replace("\\","")
print "TEST " + recv
recv = recv.rstrip("[/URL] ")
txts = recv.encode('ascii', 'replace')
print txts
if(recv.startswith("https://www.youtube.com/watch?v=")):
y.kill()
yt = False
tn.write("sendtextmessage targetmode=2 msg=trying:\s" + txts + "\n")
y = subprocess.Popen(["cvlc", "--no-video", "--play-and-exit", txts],stdout=subprocess.PIPE)
yt = True
else:
tn.write("sendtextmessage targetmode=2 msg=no\svalid\sURL:\s" + txts + "\n")
elif (recv == "help "):
tn.write("sendtextmessage targetmode=2 msg=\\navailable\scommands:\\nhelp\s-\sshows\sthis\sinfo\\nplay\s-\sstart\splaying\\nstop\ppause\s-\sstop\splaying\\nnext\s-\sskip\sto\snext\strack\\nprev\s-\sskip\sto\sprevious\strack\\nyt\s<URL>\s-\stries\sto\sopen\sYouTube\sURL\\nsearch\s<title>\s-\ssearches\sfor\s<title>\sand\splays\sfirst\sresult\\nopen\s<uri>\s-\sopens\sspotify\s<uri>\sand\sstarts\splaying\sit\\n\\nfor\sfinding\splaylists\sto\suse\swith\sthe\sopen\scommand\svisit:\shttp:\/\/playlists.net\sselect\splaylist\sand\srightclick\son\splay\snow\sand\scopy\slink\sthen\spaste\sto\sTS3\swith\sopen\scommand\sto\slook\slike\sthis:\sopen\sspotify:user:topsify:playlist:5iMj9hv66v3LNU2vP0aMJr\n")
elif (recv == "notifytextmessage "):
tn.write("sendtextmessage targetmode=2 msg=OOOps,\stry\sagain\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment