Skip to content

Commit

Permalink
Switch back to using Google Play.
Browse files Browse the repository at this point in the history
  • Loading branch information
tschoffelen committed May 27, 2022
1 parent d20a275 commit 8cc379b
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 270 deletions.
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
12 changes: 6 additions & 6 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ module.exports = function(api) {
api.cache(true);
const presets = [
[
'@babel/preset-env',
"@babel/preset-env",
{
modules: 'auto',
modules: "auto",
targets: {
browsers: ['defaults']
browsers: ["defaults"]
},
useBuiltIns: 'entry',
useBuiltIns: "entry",
corejs: 2
}
],
'@babel/preset-flow'
"@babel/preset-flow"
];

const plugins = [
'@babel/plugin-proposal-class-properties'
"@babel/plugin-proposal-class-properties"
];

return {
Expand Down
76 changes: 38 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
import { Platform, NativeModules } from 'react-native';
import { Platform, NativeModules } from "react-native";

import { lookupVersion } from './src/utils';
import { versionCompare } from './src/versions';
import { lookupVersion } from "./src/utils";
import { versionCompare } from "./src/versions";

const DEFAULT_COUNTRY = 'us';
const DEFAULT_COUNTRY = "us";

export const checkVersion = async(options = {}) => {
// Get options object
const platform = options.platform || Platform.OS;
const country = options.country || DEFAULT_COUNTRY;
const bundleId = options.bundleId || (NativeModules.RNDeviceInfo
? NativeModules.RNDeviceInfo.bundleId
: null);
const currentVersion = options.currentVersion || (NativeModules.RNDeviceInfo
? NativeModules.RNDeviceInfo.appVersion
: '');
// Get options object
const platform = options.platform || Platform.OS;
const country = options.country || DEFAULT_COUNTRY;
const bundleId = options.bundleId || (NativeModules.RNDeviceInfo
? NativeModules.RNDeviceInfo.bundleId
: null);
const currentVersion = options.currentVersion || (NativeModules.RNDeviceInfo
? NativeModules.RNDeviceInfo.appVersion
: "");

// Check if we have retrieved a bundle ID
if (!bundleId && !('RNDeviceInfo' in NativeModules)) {
throw Error(
'[react-native-check-version] Missing react-native-device-info dependency, ' +
'please manually specify a bundleId in the options object.'
);
}
// Check if we have retrieved a bundle ID
if (!bundleId && !("RNDeviceInfo" in NativeModules)) {
throw Error(
"[react-native-check-version] Missing react-native-device-info dependency, " +
"please manually specify a bundleId in the options object."
);
}

try {
const data = await lookupVersion(platform, bundleId, country);
const version = versionCompare(currentVersion, data.version);
return { platform, bundleId, ...data, ...version };
} catch (e) {
// On error - return default object
return {
platform,
bundleId,
version: null,
needsUpdate: false,
notes: '',
url: null,
lastChecked: (new Date()).toISOString(),
country,
error: e
};
}
try {
const data = await lookupVersion(platform, bundleId, country);
const version = versionCompare(currentVersion, data.version);
return { platform, bundleId, ...data, ...version };
} catch (e) {
// On error - return default object
return {
platform,
bundleId,
version: null,
needsUpdate: false,
notes: "",
url: null,
lastChecked: (new Date()).toISOString(),
country,
error: e
};
}
};
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
"lint": "prettier --write ."
},
"dependencies": {
"axios": "^0.27.2",
"date-fns": "^2.28.0"
"axios": "^0.27.2"
},
"devDependencies": {
"@babel/core": "^7.11.1",
Expand Down
31 changes: 31 additions & 0 deletions src/providers/android.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import axios from "axios";

export const getAndroidVersion = async(bundleId, country) => {
const url = `https://play.google.com/store/apps/details?id=${bundleId}&hl=${country}`;
let res;
try {
res = await axios.get(url, {
headers: {
"User-Agent": "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36"
}
});
} catch (e) {
console.log(e.response);
if (e.response && e.response.status && e.response.status === 404) {
throw new Error(
`App with bundle ID "${bundleId}" not found in Google Play.`
);
}
throw e;
}

const version = res.data.match(/\[\[\[['"]((\d+\.)+\d+)['"]\]\],/)[1];

return {
version: version || null,
releasedAt: (new Date()).toISOString(),
notes: "",
url: `https://play.google.com/store/apps/details?id=${bundleId}&hl=${country}`,
lastChecked: (new Date()).toISOString()
};
};
25 changes: 25 additions & 0 deletions src/providers/ios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import axios from "axios";

export const getIosVersion = async(bundleId, country) => {
// Adds a random number to the end of the URL to prevent caching
const url = `https://itunes.apple.com/lookup?lang=en&bundleId=${bundleId}&country=${country}&_=${new Date().valueOf()}`;

let res = await axios.get(url);
if (!res.data || !("results" in res.data)) {
throw new Error("Unknown error connecting to iTunes.");
}
if (!res.data.results.length) {
throw new Error("App for this bundle ID not found.");
}

res = res.data.results[0];

return {
version: res.version || null,
released: res.currentVersionReleaseDate || res.releaseDate || null,
notes: res.releaseNotes || "",
url: res.trackViewUrl || res.artistViewUrl || res.sellerUrl || null,
country,
lastChecked: (new Date()).toISOString()
};
};
112 changes: 22 additions & 90 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,94 +1,26 @@
import axios from 'axios';
import dateFns from 'date-fns';
import { getIosVersion } from "./providers/ios";
import { getAndroidVersion } from "./providers/android";

const cache = {};

const getTokenValue = (startToken, endToken, tokenSearchData) => {
const indexStart = tokenSearchData.indexOf(startToken);
let value = tokenSearchData.substring(indexStart + startToken.length);
const indexEnd = value.indexOf(endToken);
value = value
.substring(0, indexEnd)
.replace(/<[^>]+>/g, '')
.trim();

return value;
};

export const lookupVersion = async(platform, bundleId, country = 'us') => {
const key = `${platform}.${bundleId}`;
let res = cache[key];
if (res) {
return res;
}

let url;
switch (platform) {
case 'ios':
// Adds a random number to the end of the URL to prevent caching
url = `https://itunes.apple.com/lookup?lang=en&bundleId=${bundleId}&country=${country}&_=${new Date().valueOf()}`;
res = await axios.get(url);
if (!res.data || !('results' in res.data)) {
throw new Error('Unknown error connecting to iTunes.');
}
if (!res.data.results.length) {
throw new Error('App for this bundle ID not found.');
}
res = res.data.results[0];

res = {
version: res.version || null,
released: res.currentVersionReleaseDate || res.releaseDate || null,
notes: res.releaseNotes || '',
url: res.trackViewUrl || res.artistViewUrl || res.sellerUrl || null,
country,
lastChecked: (new Date()).toISOString()
};

cache[key] = res;
return res;
case 'android':
url = `https://apkcombo.com/x/${bundleId}/`;
try {
res = await axios.get(url, {
headers: {
'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36'
}
});
} catch (e) {
console.log(e.response);
if (e.response && e.response.status && e.response.status === 404) {
throw new Error(
`App with bundle ID "${bundleId}" not found in Google Play.`
);
}
throw e;
}

let resData = res.data;
resData = resData.split('class="information-table">', 2)[1] || '';

// Version
const version = (getTokenValue('Version', 'Update', resData) || '').split(' (')[0];

// Release Date
let released = null;
try {
released = dateFns.parse(getTokenValue('Update', 'Developer', resData), 'LLLL d, yyyy', new Date());
} catch (e) {
}

res = {
version: version || null,
releasedAt: (released && (released).toISOString()) || (new Date()).toISOString(),
notes: '',
url: `https://play.google.com/store/apps/details?id=${bundleId}`,
lastChecked: (new Date()).toISOString()
};

cache[key] = res;
return res;
default:
throw new Error('Unsupported platform defined.');
}
export const lookupVersion = async(platform, bundleId, country = "us") => {
const key = `${platform}.${bundleId}`;
let res = cache[key];
if (res) {
return res;
}

switch (platform) {
case "ios":
res = await getIosVersion(bundleId, country);
break;
case "android":
res = await getAndroidVersion(bundleId, country);
break;
default:
throw new Error("Unsupported platform defined.");
}

cache[key] = res;
return res;
};
Loading

0 comments on commit 8cc379b

Please sign in to comment.