def call_api(func, args=""): bottle.response.headers.replace("Content-type", "application/json") bottle.response.headers.append("Cache-Control", "no-cache, must-revalidate") s = bottle.request.environ.get("beaker.session") if "session" in bottle.request.POST: s = s.get_by_id(bottle.request.POST["session"]) if not s or not s.get("authenticated", False): return bottle.HTTPError(403, json_dumps("Forbidden")) if not PYLOAD.isAuthorized(func, {"role": s["role"], "permission": s["perms"]}): return bottle.HTTPError(401, json_dumps("Unauthorized")) args = args.split("/")[1:] kwargs = {} for x, y in itertools.chain(bottle.request.GET.iteritems(), bottle.request.POST.iteritems()): if x == "session": continue kwargs[x] = urllib.unquote(y) try: return callApi(func, *args, **kwargs) except Exception, e: traceback.print_exc() return bottle.HTTPError(500, json_dumps({"error": e.message, "traceback": traceback.format_exc()}))
def call_api(func, args=""): bottle.response.headers.replace("Content-type", "application/json") bottle.response.headers.append("Cache-Control", "no-cache, must-revalidate") s = bottle.request.environ.get('beaker.session') if 'session' in bottle.request.POST: s = s.get_by_id(bottle.request.POST['session']) if not s or not s.get("authenticated", False): return bottle.HTTPError(403, json_dumps("Forbidden")) if not PYLOAD.isAuthorized(func, {"role": s['role'], "permission": s['perms']}): return bottle.HTTPError(401, json_dumps("Unauthorized")) args = args.split("/")[1:] kwargs = {} for x, y in itertools.chain(bottle.request.GET.iteritems(), bottle.request.POST.iteritems()): if x == "session": continue kwargs[x] = urllib.unquote(y) try: return callApi(func, *args, **kwargs) except Exception, e: traceback.print_exc() return bottle.HTTPError(500, json_dumps({"error": e.message, "traceback": traceback.format_exc()}))
def callApi(func, *args, **kwargs): if not hasattr(PYLOAD.EXTERNAL, func) or func.startswith("_"): print "Invalid API call", func return bottle.HTTPError(404, json_dumps("Not Found")) result = getattr(PYLOAD, func)(*[SafeEval.const_eval(x) for x in args], **dict((x, SafeEval.const_eval(y)) for x, y in kwargs.iteritems())) # null is invalid json response return json_dumps(result or True, cls=TBaseEncoder)
def callApi(func, *args, **kwargs): if not hasattr(PYLOAD.EXTERNAL, func) or func.startswith("_"): print "Invalid API call", func return bottle.HTTPError(404, json_dumps("Not Found")) result = getattr(PYLOAD, func)( *[SafeEval.const_eval(x) for x in args], **dict((x, SafeEval.const_eval(y)) for x, y in kwargs.iteritems()) ) # null is invalid json response return json_dumps(result or True, cls=TBaseEncoder)
def setup(): add_json_header(response) return json_dumps({ "system": SETUP.check_system(), "deps": SETUP.check_deps() })
def handle_premium(self, pyfile): host = self._get_host(pyfile.url) json_params = json_dumps({ 'link': pyfile.url, 'type': host, 'username': self.user, 'password': self.account.getAccountData(self.user)['password'] }) r = self.load("http://gen.linksnappy.com/genAPI.php", post={'genLinks': json_params}) self.logDebug("JSON data: " + r) j = json_loads(r)['links'][0] if j['error']: self.error(_("Error converting the link")) pyfile.name = j['filename'] self.link = j['generated'] if host in self.SINGLE_CHUNK_HOSTERS: self.chunkLimit = 1 else: self.setup()
def api_response(self, **kwargs): """ Dispatch a call to the api, see https://mega.co.nz/#developers """ # generate a session id, no idea where to obtain elsewhere uid = random.randint(10 << 9, 10 ** 10) res = self.load(self.API_URL, get={'id': uid}, post=json_dumps([kwargs])) self.logDebug("Api Response: " + res) return json_loads(res)
def login(): bottle.response.headers.replace("Content-type", "application/json") bottle.response.headers.append("Cache-Control", "no-cache, must-revalidate") user = bottle.request.forms.get("username") password = bottle.request.forms.get("password") info = PYLOAD.checkAuth(user, password) if not info: return json_dumps(False) s = set_session(request, info) # get the session id by dirty way, documentations seems wrong try: sid = s._headers["cookie_out"].split("=")[1].split(";")[0] return json_dumps(sid) except Exception: return json_dumps(True)
def i18n(lang=None): add_json_header(response) if lang is None: pass # TODO use lang from PYLOAD.config or setup else: # TODO auto choose language lang = select_language(["en"]) return json_dumps({})
def login(): bottle.response.headers.replace("Content-type", "application/json") bottle.response.headers.append("Cache-Control", "no-cache, must-revalidate") user = bottle.request.forms.get("username") password = bottle.request.forms.get("password") info = PYLOAD.checkAuth(user, password) if not info: return json_dumps(False) s = set_session(request, info) # get the session id by dirty way, documentations seems wrong try: sid = s._headers['cookie_out'].split("=")[1].split(";")[0] return json_dumps(sid) except Exception: return json_dumps(True)
def handle_premium(self, pyfile): api_key = self.user premium_key = self.account.getAccountData(self.user)['password'] json_data = [api_key, ["download/direct_links", {"pass": premium_key, "link": pyfile.url}]] api_rep = self.load('http://api.letitbit.net/json', post={'r': json_dumps(json_data)}) self.logDebug("API Data: " + api_rep) api_rep = json_loads(api_rep) if api_rep['status'] == 'FAIL': self.fail(api_rep['data']) self.link = api_rep['data'][0][0]
def handlePremium(self, pyfile): api_key = self.user premium_key = self.account.getAccountData(self.user)['password'] json_data = [ api_key, [ "download/direct_links", { "pass": premium_key, "link": pyfile.url } ] ] api_rep = self.load('http://api.letitbit.net/json', post={'r': json_dumps(json_data)}) self.logDebug("API Data: " + api_rep) api_rep = json_loads(api_rep) if api_rep['status'] == 'FAIL': self.fail(api_rep['data']) self.link = api_rep['data'][0][0]
def handlePremium(self, pyfile): host = self._get_host(pyfile.url) json_params = json_dumps({'link' : pyfile.url, 'type' : host, 'username': self.user, 'password': self.account.getAccountData(self.user)['password']}) r = self.load("http://gen.linksnappy.com/genAPI.php", post={'genLinks': json_params}) self.logDebug("JSON data: " + r) j = json_loads(r)['links'][0] if j['error']: self.error(_("Error converting the link")) pyfile.name = j['filename'] self.link = j['generated'] if host in self.SINGLE_CHUNK_HOSTERS: self.chunkLimit = 1 else: self.setup()
def error(code, msg): return HTTPError(code, json_dumps(msg), **dict(response.headers))
def api_response(self, **kwargs): """ Dispatch a call to the api, see megacrypter.com/api_doc """ self.logDebug("JSON request: " + json_dumps(kwargs)) res = self.load(self.API_URL, post=json_dumps(kwargs)) self.logDebug("API Response: " + res) return json_loads(res)
def api_response(url): json_data = ["yw7XQy2v9", ["download/info", {"link": url}]] api_rep = getURL("http://api.letitbit.net/json", post={'r': json_dumps(json_data)}) return json_loads(api_rep)