Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moved all Api URLs to the beginning so that they can be modified more easily #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 48 additions & 13 deletions NiuDataWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
var username = "";
var password = "";
var sn = ""
var countryCode = '86'
var debug_size = "medium"; // which size should the widget try to run as when run through Scriptable. (small, medium, large)
var is_dark_mode_working = true; // always use white theme when setting to false
}
Expand Down Expand Up @@ -47,6 +48,25 @@ var colors = {
}
}

// [SN] will be replaced with the scooter serial no
const scooterDetailApi = 'https://app-api.niu.com/v5/scooter/detail/[SN]';
// const scooterDetailApi = 'https://app-api-fk.niu.com/v5/scooter/detail/[SN]';
const scooterInfolApi = 'https://app-api.niu.com/v3/motor_data/index_info?&sn=[SN]';
// const scooterInfolApi = 'https://app-api-fk.niu.com/v3/motor_data/index_info?&sn=[SN]';
const lastTrackDataApi = 'https://app-api.niu.com/v5/track/list/v2';
// const lastTrackDataApi = 'https://app-api-fk.niu.com/v5/track/list/v2';
const loadTokenApi = 'https://account.niu.com/v3/api/oauth2/token';
// const loadTokenApi = 'https://account-fk.niu.com/v3/api/oauth2/token';
const actionApi = 'https://app-api.niu.com/v5/cmd/creat';
// const actionApi = 'https://app-api-fk.niu.com/v5/cmd/creat';

const headerUserAgent = 'manager/4.6.20 (iPhone; iOS 14.5.1; Scale/3.00);timezone=Asia/Shanghai;model=iPhone13,2;lang=zh-CN;ostype=iOS;clientIdentifier=Domestic';
// const headerUserAgent = 'manager/4.6.48 (iPhone; iOS 15.1.1; Scale/3.00);timezone=Europe/Berlin;model=iPhone13,2;lang=de-DE;ostype=iOS;clientIdentifier=Domestic';
const headerAcceptLanguage = 'zh-CN';
const useScooterDetailCache = false;
const niu_app_id = 'niu_03cn0n7v';


function isDarkMode()
{
return Device.isUsingDarkAppearance() && is_dark_mode_working;
Expand Down Expand Up @@ -732,26 +752,38 @@ async function progressCircle(
}

async function fetchScooterDetail(token, sn) {
var req = new Request('https://app-api.niu.com/v5/scooter/detail/' + sn);
let fileManager = FileManager.iCloud();
let file = fileManager.joinPath(fileManager.documentsDirectory(), 'niu_data/niu_detail_' + sn + '.dat');
if (useScooterDetailCache){
if (fileManager.fileExists(file)){
fileManager.downloadFileFromiCloud(file);
return [true, JSON.parse(fileManager.readString(file))];
}
}
var req = new Request(scooterDetailApi.replace("[SN]", sn));
req.method = 'GET';
req.headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'manager/4.6.48 (iPhone; iOS 15.1.1; Scale/3.00);timezone=Asia/Shanghai;model=iPhone13,2;lang=zh-CN;ostype=iOS;clientIdentifier=Domestic',
'User-Agent': headerUserAgent,
'Accept-Language': headerAcceptLanguage,
'token': token
};
var json = await req.loadJSON();
if (json.status == 0)
if (json.status == 0){
fileManager.writeString(file, JSON.stringify(json));
return [true, json];
else
} else {
return [false, json];
}
}

async function fetchInfoData(token) {
var req = new Request('https://app-api.niu.com/v3/motor_data/index_info?&sn=' + sn);
var req = new Request(scooterInfolApi.replace("[SN]", sn));
req.method = 'GET';
req.headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148',
'User-Agent': headerUserAgent,
'Accept-Language': headerAcceptLanguage,
'token': token
};
var json= await req.loadJSON();
Expand All @@ -771,11 +803,12 @@ async function loadLastTrackData(token, sn, from_local = true) {
}
else
{
var req = await new Request('https://app-api.niu.com/v5/track/list/v2');
var req = await new Request(lastTrackDataApi.replace("[SN]", sn));
req.method = 'POST';
req.headers = {
'Content-Type': 'application/json',
'User-Agent': 'manager/4.6.20 (iPhone; iOS 14.5.1; Scale/3.00);timezone=Asia/Shanghai;model=iPhone13,2;lang=zh-CN;ostype=iOS;clientIdentifier=Domestic',
'User-Agent': headerUserAgent,
'Accept-Language': headerAcceptLanguage,
'token': token
};
req.body = '{"sn":"' + sn + '","index":"0","token":"' + token + '","pagesize":1}';
Expand All @@ -794,13 +827,14 @@ async function loadToken(force = false) {
token_file = tokenManager.joinPath(tokenManager.documentsDirectory(), "niu_data/token_" + username + ".dat");

if (force || !tokenManager.fileExists(token_file)) {
var request_ = await new Request('https://account.niu.com/v3/api/oauth2/token');
var request_ = await new Request(loadTokenApi.replace("[SN]", sn));
request_.method = "POST";
request_.headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'manager/4.6.48 (iPhone; iOS 15.1.1; Scale/3.00);timezone=Asia/Shanghai;model=iPhone13,2;lang=zh-CN;ostype=iOS;clientIdentifier=Domestic'
'User-Agent': headerUserAgent,
'Accept-Language': headerAcceptLanguage,
};
request_.body = 'account=' + username + '&password=' + password + '&app_id=niu_03cn0n7v&scope=base&countryCode=86&grant_type=password';
request_.body = 'account=' + username + '&password=' + password + '&app_id=' + niu_app_id + '&scope=base&countryCode=' + countryCode + '&grant_type=password';
var json = await request_.loadJSON();
if (json.status == 0) {
var token = json.data.token.access_token;
Expand All @@ -818,11 +852,12 @@ async function loadToken(force = false) {
}

async function action(type, token) {
var req = new Request('https://app-api.niu.com/v5/cmd/creat');
var req = new Request(actionApi.replace("[SN]", sn));
req.method = 'POST';
req.headers = {
'Content-Type': 'application/json',
'User-Agent': 'manager/4.6.20 (iPhone; iOS 14.5.1; Scale/3.00);timezone=Asia/Shanghai;model=iPhone13,2;lang=zh-CN;ostype=iOS;clientIdentifier=Domestic',
'User-Agent': headerUserAgent,
'Accept-Language': headerAcceptLanguage,
'token': token
};
req.body = '{"token":"' + token + '","sn":"' + sn + '", "type":"' + type + '"}';
Expand Down