Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
try to download app images automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
MuhammedKpln committed Mar 28, 2020
1 parent 38caffe commit 35a9f2f
Show file tree
Hide file tree
Showing 9 changed files with 1,543 additions and 1,399 deletions.
13 changes: 5 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,20 @@
"bin": {
"chob": "./build/src/cli.js"
},
"engines": {
"node": ">= 10.13 <11"
},
"devDependencies": {
"@types/jest": "^23.3.12",
"@types/node": "^10.12.18",
"@types/node": "latest",
"jest": "~23.6.0",
"pkg": "^4.4.0",
"prettier": "1.15.3",
"rimraf": "^2.6.3",
"ts-jest": "~23.10.5",
"tslib": "^1.9.3",
"tslint": "~5.12.0",
"tslint": "latest",
"tslint-config-prettier": "1.17.0",
"tslint-microsoft-contrib": "~6.0.0",
"tsutils": "~3.7.0",
"typescript": "^3.2.2",
"tslint-microsoft-contrib": "latest",
"tsutils": "latest",
"typescript": "latest",
"webpack": "^4.31.0",
"webpack-cli": "^3.3.2"
},
Expand Down
46 changes: 46 additions & 0 deletions src/GithubApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { IGithubLatestReleases, ITags } from './dataStructure';
import { ApiClient } from './apiClient';

const apiClient = new ApiClient();
export class GithubApi {
private repoUrl: string;
private userName: string;
private userRepo: string;
private githubApi: string = 'https://api.github.com';
private userAgent: string =
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36';

constructor(repoUrl: string) {
this.userName = repoUrl.split('/')[3];
this.userRepo = repoUrl.split('/')[4];
}
async getTheLatestRelease(): Promise<IGithubLatestReleases> {
const { githubApi, userName, userRepo, userAgent } = this;
this.repoUrl = `${githubApi}/repos/${userName}/${userRepo}/releases/latest`;

const options = {
url: this.repoUrl,
headers: {
'User-Agent': userAgent,
},
};
const data: string = await apiClient.get(options);

return JSON.parse(data);
}

async getTags(): Promise<ITags[]> {
const { githubApi, userName, userRepo, userAgent } = this;
this.repoUrl = `${githubApi}/repos/${userName}/${userRepo}/tags`;

const options = {
url: this.repoUrl,
headers: {
'User-Agent': userAgent,
},
};
const data = await apiClient.get(options);

return JSON.parse(data);
}
}
135 changes: 94 additions & 41 deletions src/apiClient.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,110 @@
import * as request from 'request'
import { flathubStructure, snapStrucuure, appimageStructure } from './dataStructure'
import { errorMessage, successfullMessage } from './main';
import * as request from 'request';
import {
flathubStructure,
snapStrucuure,
appimageStructure,
} from './dataStructure';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';

export class ApiClient {
private flathubApi = 'https://flathub.org/api/v1/apps/';
private snapApi =
'https://raw.githubusercontent.com/MuhammedKpln/chob-snap-api/master/snapcraft.json';
private appimageApi = 'https://appimage.github.io/feed.json';

private flathubApi = 'https://flathub.org/api/v1/apps/'
private snapApi = 'https://raw.githubusercontent.com/MuhammedKpln/chob-snap-api/master/snapcraft.json'
private appimageApi = 'https://appimage.github.io/feed.json'
private flathubData;
private snapData;
private appimageData;

private flathubData;
private snapData;
private appimageData;
async get(url: string | Object): Promise<any> {
return new Promise((resolve, reject) => {
request.get(url, (error, response, body) => {
if (error && response.statusCode !== 200) {
return reject(error);
}

return resolve(body);
});
});
}

grabDataFromFlathub(): Promise<flathubStructure> {
return new Promise((resolve, reject) => {
request.get(this.flathubApi, (error, response, body) => {
if (error && response.statusCode !== 200) {
return reject(error)
}

this.flathubData = JSON.parse(body)
return resolve(this.flathubData)
})
})
async download(url: string, fileName: string): Promise<boolean> {
const homeFolder = path.resolve(os.homedir(), 'chob');
const filePath = path.resolve(
homeFolder,
`${fileName.toLowerCase()}.AppImage`,
);
if (!fs.existsSync(homeFolder)) {
fs.mkdirSync(path.resolve(os.homedir(), 'chob'));
}

if (!fs.existsSync(filePath)) {
fs.writeFileSync(filePath, '');
} else {
errorMessage(`You already have downloaded this file in ${homeFolder}`);
return false;
}
const file = fs.createWriteStream(filePath);
request
.get({
url: url,
followAllRedirects: true,
})
.pipe(file)
.on('finish', () => {
successfullMessage(
`Your download is finished, it is stored at ${filePath}`,
);
return true;
})
.on('error', e => {
errorMessage('Could not download the app image.');
console.error(e);
return false;
});

grabDataFromSnap(): Promise<snapStrucuure> {
return new Promise((resolve, reject) => {
request.get(this.snapApi, (error, response, body) => {
if (error && response.statusCode !== 200) {
return reject(error)
}
return true;
}

this.snapData = JSON.parse(body)
return resolve(this.snapData)
})
grabDataFromFlathub(): Promise<flathubStructure> {
return new Promise((resolve, reject) => {
request.get(this.flathubApi, (error, response, body) => {
if (error && response.statusCode !== 200) {
return reject(error);
}

})
}
this.flathubData = JSON.parse(body);
return resolve(this.flathubData);
});
});
}

grabDataAppImage(): Promise<appimageStructure> {
return new Promise((resolve, reject) => {
request.get(this.appimageApi, (error, response, body) => {
if (error && response.statusCode !== 200) {
return reject(error)
}
grabDataFromSnap(): Promise<snapStrucuure> {
return new Promise((resolve, reject) => {
request.get(this.snapApi, (error, response, body) => {
if (error && response.statusCode !== 200) {
return reject(error);
}

this.appimageData = JSON.parse(body)
return resolve(this.appimageData)
})
this.snapData = JSON.parse(body);
return resolve(this.snapData);
});
});
}

})
}
grabDataAppImage(): Promise<appimageStructure> {
return new Promise((resolve, reject) => {
request.get(this.appimageApi, (error, response, body) => {
if (error && response.statusCode !== 200) {
return reject(error);
}

this.appimageData = JSON.parse(body);
return resolve(this.appimageData);
});
});
}
}
40 changes: 25 additions & 15 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
import * as argparser from 'commander'
import * as colors from 'colors'
import { grabApplicationsFromApi, search } from './main'
import * as pkg from "../package.json"
import * as argparser from 'commander';
import * as colors from 'colors';
import { grabApplicationsFromApi, search } from './main';
import * as pkg from '../package.json';

const helpText = () => colors.bgCyan.white.bold('Usage: chob pkgName')
export let experimentalFeatures: boolean = false;

argparser.command('*')
.action(env => {
console.log(colors.bgGreen.white.bold(`🔎 Searching ${env} on repositories.`));
grabApplicationsFromApi().then(() => {
search(env.toLowerCase())
})
})
argparser.version(pkg.version)
const helpText = () => colors.bgCyan.white.bold('Usage: chob pkgName');
const searchApplication = env => {
console.log(
colors.bgGreen.white.bold(`🔎 Searching ${env} on repositories.`),
);
grabApplicationsFromApi().then(() => {
search(env.toLowerCase());
});
};

argparser
.option('--enableExperiementalFeatures', 'Enables experiemental features')
.action((appName, args) => {
if (args.enableExperiementalFeatures) {
experimentalFeatures = true;
}
searchApplication(appName);
});

argparser.version(pkg.version);
argparser.parse(process.argv);

if (!process.argv.slice(2).length) {
argparser.outputHelp(helpText);
}


79 changes: 40 additions & 39 deletions src/dataSerializer.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
import { flathubStructure, snapStrucuure, appimageStructure } from './dataStructure';

export function serializeFlathubData(data: flathubStructure) : Array<Object> {
let serializedData: Object[] = [];

for (const app of data) {
serializedData.push({
name: app.name,
type: 2,
src: `https://flathub.org/apps/details/${app.flatpakAppId}`
})
}

return serializedData
import {
flathubStructure,
snapStrucuure,
appimageStructure,
} from './dataStructure';

export function serializeFlathubData(data: flathubStructure): Array<Object> {
let serializedData: Object[] = [];

for (const app of data) {
serializedData.push({
name: app.name,
type: 2,
src: `https://flathub.org/apps/details/${app.flatpakAppId}`,
});
}

return serializedData;
}

export function serializeSnapData(data: snapStrucuure): Object[] {
let serializedData: Object[] = [];

export function serializeSnapData(data: snapStrucuure) : Object[] {
let serializedData: Object[] = [];

for (const app of data['_embedded']['clickindex:package']) {
serializedData.push({
name: app.title,
type: 3,
src: `https://snapcraft.io/${app.package_name}`
})
}
for (const app of data['_embedded']['clickindex:package']) {
serializedData.push({
name: app.title,
type: 3,
src: `https://snapcraft.io/${app.package_name}`,
});
}

return serializedData
return serializedData;
}



export function serializeAppImageData(data: appimageStructure) : Object[] {
let serializedData: Object[] = [];

for (const app of data.items) {
serializedData.push({
name: app.name,
type: 1,
src: `https://appimage.github.io/${app.name}`
})
}

return serializedData
export function serializeAppImageData(data: appimageStructure): Object[] {
let serializedData: Object[] = [];
for (const app of data.items) {
serializedData.push({
name: app.name,
type: 1,
src: `https://appimage.github.io/${app.name}`,
repoUrl: app.links?.[1]?.['url'],
});
}

return serializedData;
}
Loading

0 comments on commit 35a9f2f

Please sign in to comment.