This repository has been archived by the owner on Apr 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
youtube.py
113 lines (95 loc) · 2.76 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""
Get suggestions from Youtube
"""
from albertv0 import *
from os import path
import requests
import json
import re
__iid__ = 'PythonInterface/v0.1'
__prettyname__ = 'Youtube Suggestions'
__version__ = '1.0'
__trigger__ = 'yt '
__author__ = 'Angelo Gazzola'
__dependencies__ = []
__icon__ = path.dirname(__file__) + '/icons/YouTube.png'
REQUEST_HEADERS = {
'User-Agent': (
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)'
' Chrome/62.0.3202.62 Safari/537.36'
)
}
session = requests.Session()
session.trust_env = False
re_videos = re.compile(r'"contents":(\[{"videoRenderer":.*}\]),"continuations"')
class SuggestionItem(Item):
def __init__(self, suggestion):
super().__init__(
id=str(hash(suggestion)),
icon=__icon__,
text=suggestion,
completion=__trigger__ + suggestion + '_',
actions=[
UrlAction(
'Search on YouTube',
'https://youtube.com/search?q={}'.format(suggestion)
)
]
)
class ResultItem(Item):
def __init__(self, video, query):
try:
text = video['title']['simpleText']
subtext = '{} \t| {} | {}'.format(
video['shortViewCountText']['simpleText'],
video['lengthText']['simpleText'],
video['ownerText']['runs'][0]['text'],
)
actions = [
UrlAction('Watch on Youtube', 'https://youtube.com/watch?v={}'.format(video['videoId']))
]
super().__init__(
id=video['videoId'],
text=text,
icon=__icon__,
completion=__trigger__ + query + '_',
subtext=subtext,
actions=actions
)
except:
debug(json.dumps(video))
def search(query):
response = session.get('https://www.youtube.com/results',
headers=REQUEST_HEADERS,
params={
'search_query': query,
}
)
match = re_videos.search(response.text)
if not match:
debug(query)
return [Item(text='No results found')]
videos = json.loads(match.groups()[0])
return [ResultItem(video['videoRenderer'], query) for video in videos if video.get('videoRenderer')]
def complete(query):
response = session.get('https://clients1.google.com/complete/search',
headers=REQUEST_HEADERS,
params={
'client': 'youtube',
'output': 'toolbar',
'hl': 'en',
'q': query,
}
)
response = response.text.lstrip('window.google.ac.h(').rstrip(')')
suggestions = json.loads(response)[1]
return [SuggestionItem(suggestion[0]) for suggestion in suggestions]
def handleQuery(query):
if query.isTriggered and len(query.string) > 0:
if query.string[-1] == '_':
items = search(query.string[:-1])
else:
items = complete(query.string)
items.insert(0, SuggestionItem(query.string))
return items
return [Item(icon=__icon__, text='YouTube')]