forked from albertlauncher/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube.py
84 lines (73 loc) · 3.74 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
# -*- coding: utf-8 -*-
"""Query and open YouTube videos and channels.
Synopsis: <trigger> <query>"""
import json
import re
import time
from os import path
from urllib.parse import urlencode
from urllib.request import Request, urlopen
from albertv0 import Item, UrlAction, iconLookup
__iid__ = 'PythonInterface/v0.1'
__prettyname__ = 'Youtube'
__version__ = '1.0'
__trigger__ = 'yt '
__author__ = 'Manuel Schneider'
__icon__ = iconLookup('youtube') # path.dirname(__file__) + '/icons/YouTube.png'
HEADERS = {
'User-Agent': (
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)'
' Chrome/62.0.3202.62 Safari/537.36'
)
}
re_videos = re.compile(r"^\s*window\[\"ytInitialData\"\] = (.*);$", re.MULTILINE)
def handleQuery(query):
if query.isTriggered and query.string.strip():
# avoid rate limiting
time.sleep(0.2)
if not query.isValid:
return
url_values = urlencode({'search_query': query.string.strip()})
url = 'https://www.youtube.com/results?%s' % url_values
req = Request(url=url, headers=HEADERS)
with urlopen(req) as response:
match = re.search(re_videos, response.read().decode())
if match:
results = json.loads(match.group(1))
results = results['contents']['twoColumnSearchResultsRenderer']['primaryContents']['sectionListRenderer']['contents'][0]['itemSectionRenderer']['contents']
items = []
for result in results:
for type, data in result.items():
try:
if type == 'videoRenderer':
id = data['videoId']
subtext = 'Video'
if 'lengthText' in data:
subtext = subtext + " | %s" % data['lengthText']['simpleText'].strip()
if 'shortViewCountText' in data:
subtext = subtext + " | %s" % data['shortViewCountText']['simpleText'].strip()
if 'publishedTimeText' in data:
subtext = subtext + " | %s" % data['publishedTimeText']['simpleText'].strip()
actions=[ UrlAction('Watch on Youtube', 'https://youtube.com/watch?v=%s' % id) ]
elif type == 'channelRenderer':
id = data['channelId']
subtext = 'Channel'
if 'videoCountText' in data:
subtext = subtext + " | %s" % data['videoCountText']['simpleText'].strip()
if 'subscriberCountText' in data:
subtext = subtext + " | %s" % data['subscriberCountText']['simpleText'].strip()
actions=[ UrlAction('Show on Youtube', 'https://www.youtube.com/channel/%s' % id) ]
else:
continue
except Exception as e:
critical(e)
critical(json.dumps(result, indent=4))
item = Item(id=__prettyname__,
icon=data['thumbnail']['thumbnails'][0]['url'].split('?', 1)[0] if data['thumbnail']['thumbnails'] else __icon__,
text=data['title']['simpleText'],
subtext=subtext,
completion=query.rawString,
actions=actions
)
items.append(item)
return items