Skip to content

Commit

Permalink
Updated to work with latest API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
abdumu committed Oct 8, 2021
1 parent 2de52d9 commit ae60f06
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
58 changes: 30 additions & 28 deletions cli/backend/api-calls.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ const http = axios.create({
baseURL: "https://api.jarirreader.com",
httpsAgent: new https.Agent({
rejectUnauthorized: false,
KeepAlive: true,
}),
});
http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
http.defaults.headers.post["User-Agent"] = "okhttp/4.3.1";
http.defaults.headers.post["Host"] = "api.jarirreader.com";
http.defaults.headers.post["X-Request-Check"] = getRequestCheck();

const getInitialAuth = () => {
return new Promise((resolve, reject) => {
Expand All @@ -63,7 +63,6 @@ const getInitialAuth = () => {

http.post(
"/v5.0.1/login/token",
// "/oauth2.0/token.php",
querystring.stringify({
grant_type: "client_credentials",
client_secret: "cfb6113dfb4ccba4da7fd18c4dd8da6d",
Expand All @@ -72,6 +71,9 @@ const getInitialAuth = () => {
}),
{
timeout: 10000,
headers: {
"X-Request-Check": getRequestCheck(),
},
}
)
.then((response) => {
Expand All @@ -93,21 +95,15 @@ const getInitialAuth = () => {
});
};

const auth = (email, password, tries) => {
if (blank(tries)) {
currentTry = 1;
} else {
currentTry = tries;
}

const auth = (email, password) => {
return new Promise((resolve, reject) => {
var settings = getSettings();

if (!blank(settings) && settings.hasOwnProperty("auth") && !blank("auth")) {
const expires = Number(settings.expires);

if (expires > Date.now()) {
resolve(settings.auth);
resolve({ username: settings.username, auth: settings.auth, deviceName: settings.deviceName, deviceUID: settings.deviceUID });
return;
}
}
Expand Down Expand Up @@ -148,25 +144,26 @@ const auth = (email, password, tries) => {
const x_access = xAccess();

http.post(
// "/login/v1.0/login.php",
"v5.0.1/login/login",
querystring.stringify({
access_token: initialToken,
password: settings.password,
email: settings.email,
prev_access_token: x_access,
appId: "1",
deviceUID: deviceUID,
appId: "1",
email: settings.email,
deviceName: deviceName,
password: settings.password,
prev_access_token: x_access,
Platform: "Android",
})
}),
{
timeout: 10000,
headers: {
"X-Request-Check": getRequestCheck(),
},
}
)
.then((response) => {
if (blank(response) || blank(response.data)) {
if (currentTry < 3) {
resolve(auth(settings.email, settings.password, currentTry + 1));
return;
}
const err = new Error("(505) Can not login! check your info! \nerror: null data");
err.code = 505;
reject(err);
Expand All @@ -183,7 +180,7 @@ const auth = (email, password, tries) => {
deviceUID: deviceUID,
});

resolve(result.access_token);
resolve({ username: username, auth: result.access_token, deviceName: deviceName, deviceUID: deviceUID });
})
.catch((error) => {
const err = new Error("(505) Can not login! check your info! \nerror: " + (error.message || error));
Expand All @@ -201,22 +198,27 @@ const getUserBooks = () => {
return new Promise((resolve, reject) => {
bInfo.start();
return auth()
.then((accessToken) => {
.then((authResult) => {
const settings = getSettings();
if (
typeof settings === "undefined" ||
!settings.hasOwnProperty("books") ||
(settings.hasOwnProperty("books") && settings.books.cached_at < Date.now() - 1000 * 60 * 10) /** TODO: 5 */
(settings.hasOwnProperty("books") && settings.books.cached_at < Date.now() - 1000 * 60 * 10)
) {
http.post(
// "/v1/books/get-user-books",
"/v5.0.1/books/get-user-books",
querystring.stringify({
access_token: accessToken,
access_token: authResult.auth,
Platform: "Android",
deviceName: settings.deviceName,
deviceUID: settings.deviceUID,
})
deviceName: authResult.deviceName,
deviceUID: authResult.deviceUID,
}),
{
timeout: 10000,
headers: {
"X-Request-Check": getRequestCheck(),
},
}
)
.then((response) => {
var books = [];
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jarir-reader",
"icon": "./ui/images/jarir-logo.png",
"version": "1.1.5",
"version": "1.1.6",
"description": "تحميل كتب قارئ جرير وتحويلها لكتب عادية غير مشفرة",
"main": "ui/index.js",
"scripts": {
Expand Down Expand Up @@ -33,4 +33,4 @@
"@electron-forge/publisher-github": "^6.0.0-beta.61",
"electron": "14.0.0"
}
}
}
2 changes: 1 addition & 1 deletion ui/js/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ window.root = () => {
if (res === false) {
this.authenticated = false;
this.showLoading = false;
return;
}
return;
} catch (err) {
if (err.message.indexOf("(504) ") > -1 || err.message.indexOf("(502) ") > -1) {
this.authError = "بيانات الدخول خاطئة أو أنه لايوجد إتصال بالأنترنت!";
Expand Down

0 comments on commit ae60f06

Please sign in to comment.