This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
try to download app images automatically
- Loading branch information
1 parent
38caffe
commit 35a9f2f
Showing
9 changed files
with
1,543 additions
and
1,399 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.