Skip to content

Commit

Permalink
fixed unavailability of curl in android shell
Browse files Browse the repository at this point in the history
  • Loading branch information
joep committed Feb 19, 2022
1 parent dbf7833 commit 8559d97
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 31 deletions.
2 changes: 1 addition & 1 deletion dist/index.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 8 additions & 10 deletions src/Tasker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ import fs from 'fs';
export { default as fetch } from 'node-fetch';

export const shell = (command, root, timeoutSeconds) => {
if(process.env.NODE_ENV !== "production"){
command = command.replace(
/\/storage\/emulated\/0\/?/g,
'./'
);
}
command = command.replace(/\/storage\/emulated\/0\/?/g, './');

try {
return execSync(command, {
timeout: timeoutSeconds * 1000,
Expand Down Expand Up @@ -107,8 +103,13 @@ export const flashLong = flash;
export const setWallpaper = (filePath) =>
console.info('Wallpaper set', filePath);

export const performTask = (taskName, priority, parameterOne, parameterTwo) =>
export const performTask = (taskName, priority, parameterOne, parameterTwo) => {
console.info('Perform task', taskName, parameterOne, parameterTwo);
return 'true';
};

export const setClip = (text, appendFlag) =>
console.info('Set clipboard', text, appendFlag);

// v1 tv5.12.22
export const alarmVol = (a1, a2, a3) => {
Expand Down Expand Up @@ -345,9 +346,6 @@ export const sendIntent = (a1, a2, a3, a4, a5, a6, a7, a8) => {
export const sendSMS = (a1, a2, a3) => {
throw new Error('Function sendSMS is not yet implemented');
};
export const setClip = (a1, a2) => {
throw new Error('Function setClip is not yet implemented');
};
export const settings = (a1) => {
throw new Error('Function settings is not yet implemented');
};
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = Object.freeze(
this.TASKER_PATH = 'Tasker';
this.MAX_WALLPAPERS = 1000;
this.PREVIOUS_FILEPATH = this.TASKER_PATH + '/wallpaper/previous.json';
this.IMAGE_PATH = this.TASKER_PATH + '/wallpaper/images/';
this.IMAGE_PATH = this.TASKER_PATH + '/wallpaper/images';
this.REDDIT_CLIENT_BASE_URL = 'https://reddit.premii.com/#';
this.REDDIT_WALLPAPER_SOURCE_URL = 'https://reddit.com/r/art.json';
this.MIN_WIDTH = 1080;
Expand Down
13 changes: 9 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ const cleanCached = (previous) =>

// Check if phone is connected to Wifi and run either off or online
const [_, group] = global('%WIFII').match(/>>> (.+) <<</);

const newPrevious = await (process && process.argv.includes("--online")? online: process && process.argv.includes("--offline")? offline:group === 'CONNECTION' ? online : offline)(
previous
);

const newPrevious = await (!global('sdk') &&
process.argv.includes('--online')
? online
: !global('sdk') && process.argv.includes('--offline')
? offline
: group === 'CONNECTION'
? online
: offline)(previous);

// Remove wallpapers from cache
cleanCached(newPrevious);
Expand Down
65 changes: 50 additions & 15 deletions src/tasks/online.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import {
REDDIT_CLIENT_BASE_URL,
REDDIT_WALLPAPER_SOURCE_URL,
} from '../helpers/constants.js';
import { fetch, global, setGlobal, setWallpaper, shell } from '../Tasker.js';
import {
fetch,
global,
setGlobal,
setWallpaper,
shell,
} from '../Tasker.js';
import {
isImage,
sendNotification,
Expand Down Expand Up @@ -53,22 +59,51 @@ const getWallpaperPosts = async (after) => {
return [result.data.after, posts.filter(isImage).filter(isLargeEnough)];
};

const downloadImage = (url, filePath, timeoutSec = 30) =>
new Promise((resolve, reject) => {
const r = shell(
`cd /storage/emulated/0 && curl -L -f -o "${filePath}" "${url}" && echo d`,
false,
timeoutSec
const blobToBase64 = (blob) =>
new Promise((resolve, _) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.readAsDataURL(blob);
});

const getWallpaperBase64 = async (url, filePath) => {
const response = await fetch(url);
const blob = await response.blob();
const base64DataUrl = await blobToBase64(blob);
const base64 = base64DataUrl.match(
/^data:image\/[a-z]+;base64,(?<base>.+)$/
).groups.base;
const command = `echo '${base64}' | base64 -d > /storage/emulated/0/${filePath} && echo done`;
const r = shell(command, false, 45);
if (!r) {
throw new Error(
`shell command failed Tasker JS does not include error, ${command}`
);
if (r) {
return resolve();
}
return reject(
Error(
`Could not download ${url} to /storage/emulated/0/${filePath}`
)
}
};

const getWallpaperCurl = (url, filePath) => {
const command = `cd /storage/emulated/0 && curl -L -f -o "${filePath}" "${url}" && echo done`;
const r = shell(command, false, 45);
if (!r) {
throw new Error(
`Curl wallpaper download errored with commmand: ${command}`
);
});
}
};

const downloadImage = async (url, filePath) => {
// Test shell commands needed for downloading methods
if (shell('curl --help', false, 10)) {
getWallpaperCurl(url, filePath);
} else if (shell('base64 --help', false, 10)) {
await getWallpaperBase64(url, filePath);
} else {
throw new Error(
'Neither curl or base64 commands precent in shell, no methods available to download image'
);
}
};

const online = async (
previous = [],
Expand Down

0 comments on commit 8559d97

Please sign in to comment.