This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
162 additions
and
15 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"cSpell.words": [ | ||
"Playlistv", | ||
"Userv", | ||
"lcov", | ||
"npmrc", | ||
"sndcdn", | ||
|
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import resolve from "./resolve" | ||
import playlist from "./playlist" | ||
import user from "./user" | ||
|
||
export default { resolve, playlist } | ||
export { resolve, playlist } | ||
export default { resolve, playlist, user } | ||
export { resolve, playlist, user } |
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,38 @@ | ||
import type { ClientIDv2, URLorID } from "../types" | ||
import type { Userv2 } from "../types/user" | ||
|
||
import ky from "ky-universal" | ||
import { APIv2, getClientIDv2, scrapeData, ScrapeIDs, urlify } from "./util" | ||
|
||
const byID = async (id: number, client_id: ClientIDv2) => { | ||
const url = urlify(`users/${id}`, APIv2) | ||
const searchParams = { client_id } | ||
|
||
const data = await ky(url, { searchParams }).json() | ||
return data as Userv2 | ||
} | ||
|
||
const byURL = async (url: string) => { | ||
const scraped = await scrapeData(urlify(url)) | ||
const userData = scraped.find(({ id }) => id === ScrapeIDs.user) | ||
if (!userData) throw new Error("No user data found.") | ||
|
||
const [data] = userData.data | ||
return data as Userv2 | ||
} | ||
|
||
const user = async (identifier: URLorID, client_id?: ClientIDv2): Promise<any> => { | ||
if (typeof identifier === "string") return await byURL(identifier) | ||
|
||
if (typeof identifier === "number") { | ||
if (!client_id) client_id = await getClientIDv2() | ||
return await byID(identifier, client_id) | ||
} | ||
|
||
throw new Error("Source must be a string (URL) or a number (ID)") | ||
} | ||
|
||
user.id = byID | ||
user.url = byURL | ||
|
||
export default user |
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,44 @@ | ||
import test from "ava" | ||
|
||
import { user } from "../src" | ||
import { getClientIDv2 } from "../src/util" | ||
|
||
const exampleUserURL = process.env.EXAMPLE_USER_URL || "" | ||
const exampleUserID = Number(process.env.EXAMPLE_USER_ID) || 0 | ||
|
||
let client_id2: string | ||
|
||
test.before(async () => { | ||
client_id2 = await getClientIDv2() | ||
}) | ||
|
||
test("get user using URL", async t => { | ||
const data = await user(exampleUserURL) | ||
t.is(data.kind, "user") | ||
t.truthy(data.id) | ||
}) | ||
|
||
test("get user using ID", async t => { | ||
const data = await user(exampleUserID) | ||
t.is(data.kind, "user") | ||
t.truthy(data.id) | ||
}) | ||
|
||
test("get user using ID and client_id", async t => { | ||
const data = await user(exampleUserID, client_id2) | ||
t.is(data.kind, "user") | ||
t.truthy(data.id) | ||
}) | ||
|
||
test("get user throws using wrong input", async t => { | ||
// @ts-expect-error intentionally wrong input | ||
await t.throwsAsync(user([])) | ||
}) | ||
|
||
test("get user by URL throws when not found", async t => { | ||
await t.throwsAsync(user("")) | ||
}) | ||
|
||
test("get user by ID throws when not found", async t => { | ||
await t.throwsAsync(user(0, client_id2)) | ||
}) |
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,60 @@ | ||
export interface Userv2 { | ||
avatar_url: string | ||
city: string | ||
comments_count: number | ||
country_code: null | string | ||
created_at: Date | ||
creator_subscriptions: CreatorSubscription[] | ||
creator_subscription: CreatorSubscription | ||
description: string | ||
followers_count: number | ||
followings_count: number | ||
first_name: string | ||
full_name: string | ||
groups_count: number | ||
id: number | ||
kind: string | ||
last_modified: Date | ||
last_name: string | ||
likes_count: number | ||
playlist_likes_count: number | ||
permalink: string | ||
permalink_url: string | ||
playlist_count: number | ||
reposts_count: null | ||
track_count: number | ||
uri: string | ||
urn: string | ||
username: string | ||
verified: boolean | ||
visuals: Visuals | ||
badges: Badges | ||
url?: string | ||
} | ||
|
||
export interface Badges { | ||
pro: boolean | ||
pro_unlimited: boolean | ||
verified: boolean | ||
} | ||
|
||
export interface CreatorSubscription { | ||
product: Product | ||
} | ||
|
||
export interface Product { | ||
id: string | ||
} | ||
|
||
export interface Visuals { | ||
urn: string | ||
enabled: boolean | ||
visuals: Visual[] | ||
tracking: null | ||
} | ||
|
||
export interface Visual { | ||
urn: string | ||
entry_time: number | ||
visual_url: string | ||
} |