Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const config: Config = {
testPathIgnorePatterns: ["/node_modules/", "/dist/"],
testMatch: [
"**/__tests__/**/*.[jt]s?(x)",
"**/?(*.)+(spec|test|test.e2e).[jt]s?(x)",
"**/?(*.)+(spec|test).[jt]s?(x)",
],
};

Expand Down
15,309 changes: 15,214 additions & 95 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"doc": "jsdoc -c ./jsdoc.config.json",
"style": "npx prettier ./src --write",
"test": "npx jest -c jest.config.ts",
"e2eTest": "npx jest -c jest.config.e2e.ts",
"test:e2e": "npx jest -c jest.config.e2e.ts",
"unitTest": "npx jest -c jest.config.unit.ts"
},
"author": "keivan",
Expand Down
1 change: 1 addition & 0 deletions src/lib/InfoScraper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,5 @@ export const getPodcastInfo = async (url: string): Promise<PodcastInfo> => {
return infoAndUrl;
};

export {setRequestHeaders} from "./utils"
export * from "./interfaces";
3 changes: 3 additions & 0 deletions src/lib/InfoScraper/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ export interface PodcastInfo extends MusicAndVideoCommonInfo {
relatedTracks: Track[];
}

export type headersInputType = {[key in string]:string}


/**
* Music details
*
Expand Down
25 changes: 25 additions & 0 deletions src/lib/InfoScraper/utils/__test__/setRequestHeaders.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { setRequestHeaders } from '../index';
import { getRequestHeaders } from '../index';
import { headersInputType } from '../../interfaces';
describe("setRequestHeaders()", () => {


it("should use default headers when not called setRequestHeaders()", () => {
expect(getRequestHeaders())
.toEqual({
"user-agent":
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
})
})

it("should set custom headers", () => {
const headers:headersInputType = {
"user-agent": "xxxx xxx xxx",
"user-jwt":"xxxxxx.xxxxxxxx.xxxx"
}
setRequestHeaders(headers)
expect(getRequestHeaders()).toEqual(headers)
})


})
33 changes: 29 additions & 4 deletions src/lib/InfoScraper/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
import axios from "axios";
import { JSDOM } from "jsdom";
import { headersInputType } from "../interfaces";
export * from "./PageScraper";

let requestHeaders:unknown = {
"user-agent":
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
}


export function getRequestHeaders() {
return requestHeaders
}

/**
* Used to set custom headers for requests
*
* @example
* setRequestHeaders({
* "user-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
* "Authorization":"token"
* })
* @param {headersInputType} headers
*/
export function setRequestHeaders(headers:headersInputType) {
requestHeaders = headers
}


export const getSourceCodeDOMDocument = async (url: string) => {
const headers = getRequestHeaders()
const sourceCode = (
await axios.get(url, {
method: "GET",
headers: {
"user-agent":
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
},
headers: headers,
})
).data;
const dom = new JSDOM(sourceCode);
Expand Down
3 changes: 3 additions & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
AlbumInfo,
} from "./InfoScraper";
import { Music, Podcast, Video } from "./interfaces";
import { setRequestHeaders } from './InfoScraper/utils/index';

export {
formatURL,
Expand Down Expand Up @@ -145,3 +146,5 @@ export const getAlbum = async (url: string): Promise<AlbumInfo> =>
*/
export const getPlaylist = async (url: string): Promise<PlaylistInfo> =>
getPlaylistInfo(url);

export {setRequestHeaders} from "./InfoScraper"