import websocket import json import threading import time import urllib from bs4 import BeautifulSoup as bs ########## infos ########## channel = input('channel:\n>>') trip = input('passwd:\n>>') nick = input('nick:\n>>') if trip == '': pass else: nick = nick + '#' + trip def _search_tube(): while True: a=0 _input = json.loads(ws.recv()) if _input['cmd']=='chat': if len(_input['text'].strip())>9: if '`search ' in _input['text']: _input=_input['text'] term=_input[8::] encoded_search = urllib.parse.quote(term) print(encoded_search) url='https://youtube.com/results?search_query='+encoded_search req = urllib.request.Request(url) response = urllib.request.urlopen(req) output=bs(response,'html.parser') _result=[] for vid in output.select(".yt-uix-tile-link"): if a<1: if vid["href"].startswith("/watch?v="): a+=1 vid_info = {"title": vid["title"], "link": vid["href"], "id": vid["href"][vid["href"].index("=")+1:]} title=vid_info['title'] link=vid_info['link'] url=title+'\t'+'https://youtube.com'+link _send_msg(url) def _send_msg(msg): _send({"cmd": "chat", "text": msg}) def _send_to(target, msg): _send({"cmd": "whisper", "nick": target, "text": msg}) def _send(pkg): encoded = json.dumps(pkg) ws.send(encoded) def _stay_in_chat(): while ws.connected: _send({"cmd": "ping"}) time.sleep(60) def _send_msg_in_chat(): while True: msg = input() _send({"cmd": "chat", "text": msg}) ws = websocket.create_connection('wss://hack.chat/chat-ws') _send({"cmd": "join", "channel": channel, "nick": nick}) threading.Thread(target=_stay_in_chat).start() url= threading.Thread(target=_search_tube).start() msg = threading.Thread(target=_send_msg_in_chat).start()