将javascript覆盖到python

时间:2018-08-18 15:48:21

标签: javascript python

我有一个yale智能警报,并遇到了以下JavaScript,您可以通过它访问警报以获取状态并进行设置。我想在使用python的家庭助理集中使用它。

const fetch = require('node-fetch');
const setCookie = require('set-cookie-parser');

const urls = {
    login: 'https://www.yalehomesystem.co.uk/homeportal/api/login/check_login',
    getStatus: 'https://www.yalehomesystem.co.uk/homeportal/api/panel/get_panel_mode',
    setStatus: 'https://www.yalehomesystem.co.uk/homeportal/api/panel/set_panel_mode?area=1&mode=',
};

function getSessionCookie(username, password) {
    let sessionCookie = null;

    return fetch(urls.login, { 
        method: 'POST', 
        body: `id=${encodeURIComponent(username)}&password=${password}&rememberme=on&notify_id=&reg_id=Name`,
        headers: {
          'Accept': 'application/json, application/xml, text/plain, text/html, *.*',
          'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'
        },
    })
    .then((res) => {
        sessionCookie = res.headers._headers['set-cookie'];

        return res.json();
    }).then(json => {
        if (json.result === '0') {
            return Promise.reject('Incorrect account details');
        }
        else {
            return sessionCookie[0];
        }
    })
}

function getStatus(sessionCookie) {
    return fetch(urls.getStatus, { 
        method: 'POST', 
        headers: {
          'Cookie': sessionCookie,
        },
    }).then(res => res.text()).then(textResponse => {
        // When initially writing this code I found if cookie payload 
        // was invalid I got this text response so I added this code to
        // handle this, shouldn't happen but good to have an error message
        // for this use case
        if (textResponse === 'Disallowed Key Characters.') {
            return Promise.reject('Invalid request');
        }
        else {
            try {
                // Hopefully if we got to this point we can parse the json
                const json = JSON.parse(textResponse);

                if (json.result === '0') {
                    return Promise.reject('Unable to get status');
                }
                else {
                    return json;
                }
            } catch (error) {
                // If you get this error message I likely have not handled 
                // a error state that I wasnt aware of
                return Promise.reject('Unable to parse response');
            }
        }
    });
}

function setStatus (sessionCookie, mode) {
    return new Promise((resolve, reject) => {
        if (!sessionCookie || sessionCookie.length === 0) {
            reject('Please call getSessionCookie to get your session cookie first');
        }

        if (mode !== 'arm' && mode !== 'home' && mode !== 'disarm') {
            reject('Invalid mode passed to setStatus');
        }

        resolve(fetch(`${urls.setStatus}${mode}`, { 
            method: 'POST', 
            headers: {
              'Cookie': sessionCookie,
            },
        }));
    });
}

module.exports = {
    getSessionCookie,
    getStatus,
    setStatus,
}

我是编码的新手,但是能够将以下内容拼凑起来,以返回警报的当前状态。问题是我无法正常工作。根据上面的代码,有人可以告诉我我所缺少的内容,还是我走错了兔子洞。...

import requests
import webbrowser

url = “https://www.yalehomesystem.co.uk/homeportal/api/login/check_login”

payload = {‘username’: ‘email@domaim.com’, ‘password’: ‘mypass’}

with requests.session() as s:
# fetch the login page
s.get(url, data=payload)

url1='https://www.yalehomesystem.co.uk/homeportal/api/panel/get_panel_mode'

# post to the login form
r = s.post(url1, data=payload)
print(r.text)

要添加更多上下文,我会遇到以下错误

{“结果”:“ 0”,“消息”:“ system.permission_denied”,“代码”:“ 999”}

0 个答案:

没有答案