-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
50 lines (38 loc) · 1.43 KB
/
util.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
# -*- coding: utf-8 -*-
#
# Copyright 2020 Quentin Kniep <[email protected]>
# Distributed under terms of the MIT license.
import urllib.parse
import googleapiclient.discovery as google
import config
def url_enc(title):
return urllib.parse.quote(urllib.parse.quote(title, safe=''))
def url_dec(title):
return urllib.parse.unquote(urllib.parse.unquote(title))
class YoutubeWrapper:
def __init__(self):
self.api = google.build('youtube', 'v3', developerKey=config.YOUTUBE_API_KEY)
def search(self, query, maxres, recs=False):
if recs:
search_response = self.api.search().list(
relatedToVideoId=query,
part='id,snippet',
maxResults=maxres,
type='video',
topicId='/m/04rlf',
).execute()
else:
search_response = self.api.search().list(
q=query,
part='id,snippet',
maxResults=maxres,
type='video',
topicId='/m/04rlf',
).execute()
videos = []
for search_result in search_response.get('items', []):
if search_result['id']['kind'] == 'youtube#video':
videos.append([search_result['id']['videoId'],
search_result['snippet']['title'],
url_enc(search_result['snippet']['title'])])
return videos