Skip to content

Commit

Permalink
Магнит (ab-bonus-magnit):
Browse files Browse the repository at this point in the history
- Первая версия провайдера.
  • Loading branch information
dukei committed Feb 24, 2020
1 parent 3905122 commit 6fd6fbd
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 0 deletions.
31 changes: 31 additions & 0 deletions providers/ab-bonus-magnit/anybalance-manifest.xml
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<provider>
<id version="1">ab-bonus-magnit</id>
<api flags="no_browser"/>
<name>Магнит</name>
<description html="true"><![CDATA[
<p>Предоставляет информацию о балансе на бонусной карте сети магазинов Магнит https://magnit.ru</p>
<p>Информация берется из личного кабинета https://moy.magnit.ru</p>
]]></description>
<instruction html="true"><![CDATA[
<p>Для использования провайдера необходимо активировать карту Магнит в личном кабинете https://moy.magnit.ru </p>
]]></instruction>
<author>AnyBalance Team &lt;info@anybalance.ru&gt;</author>
<depends>
<module id="library"/>
</depends>
<files>
<icon>icon.png</icon>
<preferences>preferences.xml</preferences>
<js>main.js</js>
<history>history.xml</history>
</files>
<counters>
<counter id="balance" name="Баланс" units=" Б"/>
<counter id="fio" name="ФИО" type="text"/>
<counter id="status" name="Статус" type="text"/>
</counters>
<keywords></keywords>
<type>bonus</type>
<country>ru</country>
</provider>
11 changes: 11 additions & 0 deletions providers/ab-bonus-magnit/history.xml
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<history>
<change version="1" date="2020-02-24">
- Первая версия провайдера.
</change>
<!--
<change version="2" date="2014-02-01">
-
</change>
-->
</history>
Binary file added providers/ab-bonus-magnit/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
131 changes: 131 additions & 0 deletions providers/ab-bonus-magnit/main.js
@@ -0,0 +1,131 @@

/**
Провайдер AnyBalance (http://any-balance-providers.googlecode.com)
*/

var g_headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'windows-1251,utf-8;q=0.7,*;q=0.3',
'Accept-Language': 'ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4',
'Connection': 'keep-alive',
// Mobile
//'User-Agent':'Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en-US) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.0.0.187 Mobile Safari/534.11+',
// Desktop
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36',
};

var g_apiHeaders = {
'Connection': 'Keep-Alive',
'User-Agent': 'okhttp/3.12.0'
};

function callApi(verb, postParams, addH){
var method = 'GET';
var h = g_apiHeaders;
if(isset(postParams)){
method = 'POST';
}

if(addH)
h = addHeaders(addH, h);

var html = AnyBalance.requestPost('https://moy.magnit.ru/b2c/' + verb, postParams, h, {HTTP_METHOD: method});

if(!html)
return {__empty: true};

var json = getJson(html);
if(json.error && json.error !== 'OTP_REQUIRED'){
AnyBalance.trace(html);
throw new AnyBalance.Error(json.message, null, /парол|password/i.test(json.error.description));
}

return json;
}

function login(){
var prefs = AnyBalance.getPreferences();
delete g_apiHeaders.Authorization;

var json = callApi('login', {
username: '7' + prefs.login,
password: prefs.password,
channel: 'O',
mobileSystem: 'A'
});

if(json.error === 'OTP_REQUIRED'){
AnyBalance.trace(json.message);
var code = AnyBalance.retrieveCode('Пожалуйста, введите код из SMS для входа в ЛК Магнит', null, {inputType: 'number', timeout: 180000});
json = callApi('login', {
username: '7' + prefs.login,
password: prefs.password,
channel: 'O',
mobileSystem: 'A'
}, {
'X-CLM-OTP-Token': code
});
}

if(!json.access_token){
AnyBalance.trace(JSON.stringify(json));
throw AnyBalance.Error('Не удалось зайти в личный кабинет. Сайт изменен?');
}

AnyBalance.setData('login', prefs.login);
AnyBalance.setData('access_token', json.access_token);
AnyBalance.setData('refresh_token', json.refresh_token);
AnyBalance.saveData();
}

function setAuthHeaders(){
var at = AnyBalance.getData('access_token');
g_apiHeaders.Authorization = 'Bearer ' + at;
//g_apiHeaders.queryMode = '2';
}

function main() {
var prefs = AnyBalance.getPreferences();
AnyBalance.setDefaultCharset('utf-8');

AB.checkEmpty(prefs.login, 'Введите номер телефона!');
AB.checkEmpty(/^\d{10}$/.test(prefs.login), 'Введите 10 цифр номера телефона без пробелов и разделителей, например, 9261234567 !');
AB.checkEmpty(prefs.password, 'Введите пароль!');

var _login = AnyBalance.getData('login');
if(_login !== prefs.login){
login();
}

setAuthHeaders();
var json;

try{
json = callApi('me');
if(json.anonymized)
throw new AnyBalance.Error('Not logged in');
}catch(e){
//Надо бы по рефреш токену восстановить здесь доступ
//но пока просто логин
login();
setAuthHeaders();

json = callApi('me');
if(json.anonymized){
AnyBalance.trace(JSON.stringify(json));
throw new AnyBalance.Error('Не удалось зайти в личный кабинет после попытки авторизации. Сайт изменен?');
}
}


var result = {
success: true
};

AB.getParam(json.mainPointsBalance/100, result, 'balance');
AB.getParam(json.firstName, result, 'fio');
AB.getParam(json.mainIdentifier, result, '__tariff');
AB.getParam(json.statusName, result, 'status');

AnyBalance.setResult(result);
}
25 changes: 25 additions & 0 deletions providers/ab-bonus-magnit/preferences.xml
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen>
<EditTextPreference
title="Номер телефона"
key="login"
summary="||{@s}"
obligatory="true"
inputType="number"
dialogTitle="Номер телефона"
positiveButtonText="ОК"
negativeButtonText="Отмена"
dialogMessage="Введите 10 цифр номера телефона без пробелов и разделителей. Например, 9261234567">
</EditTextPreference>
<EditTextPreference
title="Пароль"
key="password"
summary="||***********"
obligatory="true"
dialogTitle="Пароль"
positiveButtonText="ОК"
negativeButtonText="Отмена"
dialogMessage="Введите пароль, используемый для входа в личный кабинет"
inputType="textPassword">
</EditTextPreference>
</PreferenceScreen>

0 comments on commit 6fd6fbd

Please sign in to comment.