Skip to content

Commit

Permalink
feat: migrate to new revanced api (#153)
Browse files Browse the repository at this point in the history
* feat: migrate to new revanced api

* feat(download): migrate to /releases endpoint
  • Loading branch information
Ushie authored Jul 31, 2023
1 parent a7c4b8f commit b9e50cf
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
RV_API_URL="https://releases.revanced.app"
RV_API_URL="https://api.revanced.app"
50 changes: 11 additions & 39 deletions src/data/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as settings from './settings';

// API Endpoints
import type { Patch, Repository, Tool } from '$lib/types';
import type { Patch, Repository, Metadata, Asset } from '$lib/types';

export type ReposData = Repository[];
export type PatchesData = { patches: Patch[]; packages: string[] };
export type ToolsData = { [repo: string]: Tool };
export type ReleaseData = { metadata: Metadata; assets: Asset[] };

async function get_json(endpoint: string) {
const url = `${settings.api_base_url()}/${endpoint}`;
Expand All @@ -16,48 +16,20 @@ async function repositories(): Promise<ReposData> {
return await get_json('contributors').then((json) => json.repositories);
}

async function tools(): Promise<ToolsData> {
const json = await get_json('tools');
// Make the data easier to work with.
let map: Map<string, Tool> = new Map();
for (const tool of json['tools']) {
const repo: string = tool.repository;

if (!map.has(repo)) {
map.set(repo, {
version: tool.version,
repository: repo,
// Just use the timestamp of the first one we find.
timestamp: tool.timestamp,
assets: []
});
}

let value = map.get(repo)!!;
value.assets.push({
name: tool.name,
size: tool.size,
url: tool.browser_download_url,
content_type: tool.content_type
});

map.set(repo, value);
}

return Object.fromEntries(map);
}

async function manager(): Promise<Tool> {
return await tools().then((data) => data['revanced/revanced-manager']);
async function manager(): Promise<ReleaseData> {
const json = await get_json('v2/revanced-manager/releases/latest');
// console.log(json.release.metadata.tag_name);
console.log(json.release.assets[0].browser_download_url);
return { metadata: json.release.metadata, assets: json.release.assets };
}

async function patches(): Promise<PatchesData> {
const json = await get_json('patches');
const json = await get_json('v2/patches/latest');
const packagesWithCount: { [key: string]: number } = {};

// gets packages and patch count
for (let i = 0; i < json.length; i++) {
json[i].compatiblePackages.forEach((pkg: Patch) => {
for (let i = 0; i < json.patches.length; i++) {
json.patches[i].compatiblePackages.forEach((pkg: Patch) => {
packagesWithCount[pkg.name] = (packagesWithCount[pkg.name] || 0) + 1;
});
}
Expand All @@ -67,7 +39,7 @@ async function patches(): Promise<PatchesData> {
.sort((a, b) => b[1] - a[1])
.map((pkg) => pkg[0]);

return { patches: json, packages };
return { patches: json.patches, packages };
}

export const staleTime = 5 * 60 * 1000;
Expand Down
64 changes: 32 additions & 32 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
export interface Contributor {
login: string;
avatar_url: string;
html_url: string;
login: string;
avatar_url: string;
html_url: string;
}

export interface Repository {
name: string;
contributors: Contributor[];
name: string;
contributors: Contributor[];
}

export interface Patch {
name: string;
description: string;
version: string;
excluded: boolean;
deprecated: boolean;
dependencies: string[];
options: PatchOption[];
compatiblePackages: CompatiblePackage[];
name: string;
description: string;
version: string;
excluded: boolean;
dependencies: string[];
options: PatchOption[];
compatiblePackages: CompatiblePackage[];
}

export interface CompatiblePackage {
name: string;
versions: string[];
name: string;
versions: string[];
}

export interface PatchOption {
key: string;
title: string;
description: string;
required: boolean;
choices: string[];
key: string;
title: string;
description: string;
required: boolean;
choices: string[];
}

export interface Asset {
name: string;
size: string|null;
url: string;
content_type: string;
};

export interface Tool {
repository: string;
version: string;
timestamp: string;
assets: Asset[];
};
name: string;
content_type: string;
browser_download_url: string;
}

export interface Metadata {
tag_name: string;
name: string;
draft: boolean;
prerelease: boolean;
created_at: string;
published_at: string;
body: string;
}
8 changes: 4 additions & 4 deletions src/routes/download/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<Query {query} let:data>
<Button
type="text"
href={data.assets[0].url}
href={data.assets[0].browser_download_url}
download
on:click={() => (warningDialogue = false)}>Okay</Button
>
Expand All @@ -68,17 +68,17 @@
<Query {query} let:data>
{#if !isAndroid || androidVersion < 8}
<Button on:click={handleClick} type="filled" icon="download">
{data.version}
{data.metadata.tag_name}
</Button>
{:else}
<Button
on:click={handleClick}
type="filled"
icon="download"
href={data.assets[0].url}
href={data.assets[0].browser_download_url}
download
>
{data.version}
{data.metadata.tag_name}
</Button>
{/if}
</Query>
Expand Down

0 comments on commit b9e50cf

Please sign in to comment.