tubelib: fix token refresh not working by doing some python magic. Fix Makefile.am
[enigma2-plugins.git] / tubelib / src / youtube / Base.py
1 from apiclient.discovery import build
2 from oauth2client.file import Storage
3 from oauth2client.client import OAuth2Credentials
4 import httplib2
5
6 from YoutubeAuth import YoutubeAuth
7
8 DEVELOPER_KEY = "AIzaSyDUp-wKizINw0m6n_7_DInK6A4qKnMaKFU"
9 YOUTUBE_API_SERVICE_NAME = "youtube"
10 YOUTUBE_API_VERSION = "v3"
11
12 def scopedCredentials(scope):
13         original_apply = OAuth2Credentials.apply
14         def apply(self, headers):
15                 if not scope in headers:
16                         headers["scope"] = scope
17                 original_apply(self, headers)
18         OAuth2Credentials.apply = apply
19
20 def loadCredentials(credentials_file):
21         credentials = None
22         storage = Storage(credentials_file)
23         credentials = storage.get()
24         return credentials
25
26 def saveCredentials(credentials_file, credentials):
27         try:
28                 storage = Storage(credentials_file)
29                 storage.put(credentials)
30                 return True
31         except:
32                 return False
33
34 def buildYoutube(credentials=None,scope=YoutubeAuth.AUTH_SCOPE_YT_RO):
35         if credentials:
36                 scopedCredentials(scope)
37                 return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY, http=credentials.authorize(httplib2.Http()))
38         else:
39                 return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY)