-
Notifications
You must be signed in to change notification settings - Fork 8
/
manager.py
51 lines (40 loc) · 1.47 KB
/
manager.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
try:
import json
from argparse import Namespace
import requests
from urllib import urlencode
except ImportError:
import json
from argparse import Namespace
import requests
from urllib.parse import urlencode
class YoutubeManager:
def __init__(self):
self.author = "ahmetkotan"
self.author_website = "http://ahmetkotan.com.tr"
self.api_key = None
self.APIs = {
'videos': 'https://www.googleapis.com/youtube/v3/videos',
'search': 'https://www.googleapis.com/youtube/v3/search',
'channels': 'https://www.googleapis.com/youtube/v3/channels',
'playlists': 'https://www.googleapis.com/youtube/v3/playlists',
'playlistItems':
'https://www.googleapis.com/youtube/v3/playlistItems'
}
def set_api_key(self, api_key):
self.api_key = api_key
return True
def get_api_key(self):
return self.api_key
def get_api(self, apiname):
return self.APIs[apiname]
def json_to_object(self, json_data):
x = json.loads(json_data, object_hook=lambda d: Namespace(**d))
return x
def api_request(self, url, parameters):
parameters['key'] = self.api_key
req_url = url + '?' + urlencode(parameters)
con = requests.get(req_url)
data = con.text
youtube_object = self.json_to_object(data)
return youtube_object