util   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A YoutubeWrapper.search() 0 26 4
A YoutubeWrapper.__init__() 0 2 1

2 Functions

Rating   Name   Duplication   Size   Complexity  
A url_enc() 0 2 1
A url_dec() 0 2 1
1
# -*- coding: utf-8 -*-
2
#
3
# Copyright 2020 Quentin Kniep <[email protected]>
4
# Distributed under terms of the MIT license.
5
6
import urllib.parse
7
8
import googleapiclient.discovery as google
9
10
import config
11
12
13
def url_enc(title):
14
    return urllib.parse.quote(urllib.parse.quote(title, safe=''))
15
16
17
def url_dec(title):
18
    return urllib.parse.unquote(urllib.parse.unquote(title))
19
20
21
class YoutubeWrapper:
22
    def __init__(self):
23
        self.api = google.build('youtube', 'v3', developerKey=config.YOUTUBE_API_KEY)
24
25
    def search(self, query, maxres, recs=False):
26
        if recs:
27
            search_response = self.api.search().list(
28
                relatedToVideoId=query,
29
                part='id,snippet',
30
                maxResults=maxres,
31
                type='video',
32
                topicId='/m/04rlf',
33
            ).execute()
34
        else:
35
            search_response = self.api.search().list(
36
                q=query,
37
                part='id,snippet',
38
                maxResults=maxres,
39
                type='video',
40
                topicId='/m/04rlf',
41
            ).execute()
42
43
        videos = []
44
45
        for search_result in search_response.get('items', []):
46
            if search_result['id']['kind'] == 'youtube#video':
47
                videos.append([search_result['id']['videoId'],
48
                               search_result['snippet']['title'],
49
                               url_enc(search_result['snippet']['title'])])
50
        return videos
51