# coding=utf-8 import urllib2 import urllib import time try: import simplejson as json except: import json from framework.utils import notify from framework.gui import Settings, XMLWindow from thread import start_new_thread from copy import deepcopy CLIENT_ID = "a332b9d61df7254dffdc81a260373f25592c94c9" CLIENT_SECRET = '744a52aff20ec13f53bcfd705fc4b79195265497' SCOPE = 'com.etvnet.media.browse com.etvnet.media.watch com.etvnet.media.bookmarks com.etvnet.media.history \ com.etvnet.media.live com.etvnet.media.fivestar com.etvnet.media.comments com.etvnet.persons com.etvnet.notifications' BASE_SERVICE_URL = 'https://accounts.etvnet.com/' DEVICE_CODE_API_URL = BASE_SERVICE_URL + 'auth/oauth/device/code' TOKEN_API_URL = BASE_SERVICE_URL + 'auth/oauth/token' GRAND_TYPE = 'http://oauth.net/grant_type/device/1.0' def run_application(): from etvnet.client import eTVxbmc wn = eTVxbmc() wn.doModal() del wn class Auth(object): terminated = False ERROR, PENDING_STATUS, SUCCESS = range(3) config = Settings() def __init__(self, window=None): self.window = window self.client_id = CLIENT_ID self.scope = SCOPE self.refresh_token = self.config.GetValue('refresh_token') self.client_secret = CLIENT_SECRET self.data = {'client_id': self.client_id, 'client_secret': self.client_secret, 'scope': self.scope} def close(self): if self.window is not None: self.window.close() def get_access_token(self): return self.config.GetValue('access_token') def set_access_token(self, value): if value is not None: value = value.encode('utf-8') self.config.SetValue('access_token', value) access_token = property(get_access_token, set_access_token) def request(self, url, data): try: print url print data udata = urllib.urlencode(data) req = urllib2.Request(url) resp = urllib2.urlopen(req, udata).read() print resp return json.loads(resp) except urllib2.URLError, e: # time out if hasattr(e, 'reason'): notify('Timeout, Try again!') # self.close() if hasattr(e, 'read'): resp = json.loads(e.read()) if e.code == 400: wn = AuthWindow() wn.doModal() del wn return resp def get_device_code(self, path=DEVICE_CODE_API_URL): data = self.data resp = self.request(path, data) self.device_code = resp['device_code'].encode('utf-8') # set device code in setting in case if application go out self.config.SetValue('device_code', str(self.device_code).encode('utf-8')) self.user_code = resp['user_code'].encode('utf-8') return resp def get_token(self, path=TOKEN_API_URL, refresh=False): data = deepcopy(self.data) if refresh: data['grant_type'] = 'refresh_token' data['refresh_token'] = self.refresh_token del data['scope'] else: data['grant_type'] = GRAND_TYPE data['code'] = self.device_code resp = self.request(path, data) error = resp.get('error') if error and error == "authorization_pending": return self.PENDING_STATUS, resp print resp self.access_token = resp.get('access_token') if self.access_token: for key, val in resp.items(): self.config.SetValue(key.encode('utf-8'), str(val).encode('utf-8')) self.config.Reset('device_code') return self.SUCCESS, resp return self.ERROR def check_if_coded_is_submitted(self): canceled = False success = False while not success == self.SUCCESS: if self.terminated is True: raise Exception time.sleep(5) success, resp = self.get_token() if canceled: return False else: return True class AuthWindow(XMLWindow): xml = "auth.xml" def __init__(self, *args, **kwargs): super(AuthWindow, self).__init__(*args, **kwargs) self.auth = Auth(window=self) def onInit(self): if not getattr(self, 'loaded', None): self.loaded = True self.showUserCode() def showUserCode(self): resp = self.auth.get_device_code() label = self.getControl(9112) label.setLabel(resp['user_code'].encode('utf-8')) self.thred = self.check_if_coded_is_submitted_in_thread() def onExit(self): self.auth.terminated = True def check_if_coded_is_submitted_in_thread(self): start_new_thread(self.check_if_coded_is_submitted, ()) def check_if_coded_is_submitted(self): success = self.auth.check_if_coded_is_submitted() if success: notify("Поздравляем! Вы успешно активировали приложение!") time.sleep(2) run_application() self.close() else: notify("Вы отменили регистрацыю eTVnet!")