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

Commit

Permalink
fix(scrape): scrape by kind property of the data, not id
Browse files Browse the repository at this point in the history
the ids seem to change randomly
  • Loading branch information
vaaski committed Mar 11, 2021
1 parent 841e2ce commit 096787c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/playlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ClientIDv2, URLorID } from "../types"
import type { Playlistv2 } from "../types/playlist"

import got from "got"
import { APIv2, getClientIDv2, scrapeData, SCRAPE_ID, urlify } from "./util"
import { APIv2, getClientIDv2, scrapeData, SCRAPE_FIND, urlify } from "./util"

/**
* Get a playlist by ID using the APIv2.
Expand All @@ -22,7 +22,7 @@ const byID = async (id: number, client_id: ClientIDv2) => {
*/
const byURL = async (url: string) => {
const scraped = await scrapeData(urlify(url))
const playlistData = scraped.find(({ id }) => id === SCRAPE_ID.playlist)
const playlistData = scraped.find(SCRAPE_FIND.playlist)
if (!playlistData) throw new Error("No playlist data found.")

const [data] = playlistData.data
Expand Down
4 changes: 2 additions & 2 deletions src/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Trackv2 } from "../types/track"
import type { ClientIDv2, URLorID } from "../types"

import got from "got"
import { APIv2, getClientIDv2, scrapeData, SCRAPE_ID, urlify } from "./util"
import { APIv2, getClientIDv2, scrapeData, SCRAPE_FIND, urlify } from "./util"

/**
* Get a track by ID using the APIv2
Expand All @@ -22,7 +22,7 @@ const getByID = async (id: number, client_id: ClientIDv2): Promise<Trackv2> => {
*/
const getByURL = async (url: string) => {
const scraped = await scrapeData(urlify(url))
const trackData = scraped.find(({ id }) => id === SCRAPE_ID.trackWithTranscodings)
const trackData = scraped.find(SCRAPE_FIND.trackWithTranscodings)
if (!trackData) throw new Error("No track data found.")

const [data] = trackData.data
Expand Down
4 changes: 2 additions & 2 deletions src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
PaginatedResponse,
paginateNext,
scrapeData,
SCRAPE_ID,
SCRAPE_FIND,
urlify,
} from "./util"

Expand All @@ -38,7 +38,7 @@ const getByID = async (id: number, client_id: ClientIDv2) => {
*/
const getByURL = async (url: string) => {
const scraped = await scrapeData(urlify(url))
const userData = scraped.find(({ id }) => id === SCRAPE_ID.user)
const userData = scraped.find(SCRAPE_FIND.user)
if (!userData) throw new Error("No user data found.")

const [data] = userData.data
Expand Down
11 changes: 6 additions & 5 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ScrapedData } from "../types/scrape"
import type { ScrapedData, ScrapedDataPart } from "../types/scrape"

import got from "got"

Expand All @@ -24,10 +24,11 @@ export const scrapeData = async (url: string): Promise<ScrapedData> => {
return JSON.parse(match) as ScrapedData
}

export const SCRAPE_ID = {
user: 73324,
playlist: 62261,
trackWithTranscodings: 70925,
export const SCRAPE_FIND = {
playlist: ({ data: [data] }: ScrapedDataPart): boolean => data.kind === "playlist",
user: ({ data: [data] }: ScrapedDataPart): boolean => data.kind === "user",
trackWithTranscodings: ({ data: [data] }: ScrapedDataPart): boolean =>
data.kind === "track" && data.media && data.media.transcodings,
}

const scriptReg = /<script(?: crossorigin)? src="(https:\/\/a-v2\.sndcdn\.com\/assets\/.+\.js)"/gm
Expand Down
6 changes: 4 additions & 2 deletions types/scrape.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export type ScrapedData = Array<{
export interface ScrapedDataPart {
id: number
chunks: number[]
data: Array<Record<string, any>>
}>
}

export type ScrapedData = Array<ScrapedDataPart>

0 comments on commit 096787c

Please sign in to comment.