-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube.py
36 lines (23 loc) · 827 Bytes
/
youtube.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
__author__ = 'vikesh'
""" ~youtube <seach query> will return the first youtube search result for the given term. """
import re
try:
from urllib import quote
except ImportError:
from urllib.request import quote
import requests
def youtube(searchquery):
url = "https://www.youtube.com/results?search_query={0}"
url = url.format(quote(searchquery))
r = requests.get(url)
results = re.findall('a href="(/watch[^&]*?)"', r.text)
if not results:
return "Sorry, charmander couldn't find any videos :crying_cat:"
return "https://www.youtube.com{0}".format(results[0])
def on_message(msg, server):
text = msg.get("text", "")
match = re.findall(r"~youtube (.*)", text)
if not match:
return
searchquery = match[0]
return youtube(searchquery.encode("utf8"))