Skip to content

Commit

Permalink
V.14
Browse files Browse the repository at this point in the history
  • Loading branch information
rit3zh committed Aug 7, 2024
1 parent 94d7f2b commit 375971e
Show file tree
Hide file tree
Showing 14 changed files with 857 additions and 14 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
- **Added "limit" option in the "searchPins" function**
- **Resolved all the errors**
- **Added `getAutoCompletion` function that takes a query parameter**
- **Addedd `getComments` function that takes `IOptions` as a parameter**
- **Fixed video property**

## Key Features 🚀

Expand Down Expand Up @@ -52,6 +54,11 @@ const Pinterest = require("pinterest.js");

- Fixed the `video` property in the `suggestions` function.

### [1.0.14] - 2024-08-06

- Added `getComments` function that taked a `IOptions` as a parameter.
- The old function `getPin` has been officially deprecated though, it is still usable `getPinV3`

## Projects Using Pinterest.js 🌟

Here are some amazing projects built using Pinterest.js:
Expand All @@ -68,4 +75,4 @@ Pinterest.js is licensed under the [MIT License](https://github.com/TrishCX/Pint

---

_Note: Replace 'Ritesh CX' and [LICENSE](https://github.com/TrishCX/Pinterest.js/blob/main/LICENSE) with your GitHub username and the link to the license file, respectively, when setting up your repository._
_Note: Replace 'rit3zh' and [LICENSE](https://github.com/TrishCX/Pinterest.js/blob/main/LICENSE) with your GitHub username and the link to the license file, respectively, when setting up your repository._
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
"form",
"json",
"images",
"google-serp"
"google-serp",
"wallpaper-anime",
"wallpapers"
],
"author": "rit3zh",
"license": "MIT",
Expand Down
21 changes: 21 additions & 0 deletions src/externals/formatNumbers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export function formatNumberWithLabel(number: number): string {
if (number < 1_000) {
return number.toString();
}

const units = ["K", "M", "B"];
const threshold = 1_000;

let unitIndex = -1;
let scaledNumber = number;

while (scaledNumber >= threshold && unitIndex < units.length - 1) {
scaledNumber /= threshold;
unitIndex++;
}

const formattedNumber =
scaledNumber % 1 === 0 ? scaledNumber.toFixed(0) : scaledNumber.toFixed(1);

return `${formattedNumber}${units[unitIndex]}`;
}
1 change: 1 addition & 0 deletions src/externals/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./formatDate";
export * from "./formatNumbers";
35 changes: 35 additions & 0 deletions src/functions/getComments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import request from "../fetch/request";
import { CommentsResultResponse } from "../interfaces";
import { parseCommentsResponse } from "../parser/parse.comments";

const AGGREGATED_PIN_ID: string = `5345354990309401472`;

interface IOptions {
id: string;
pageSize?: number;
}

export async function getComments(
options: IOptions
): Promise<CommentsResultResponse[]> {
const params = {
source_url: `/pin/${options.id}/`,
data: {
options: {
aggregated_pin_id: AGGREGATED_PIN_ID,
comment_featured_ids: [],
page_size: options?.pageSize || 20,
redux_normalize_feed: true,
is_reversed: false,
},
context: {},
},
};

const URL: string = `https://in.pinterest.com/resource/UnifiedCommentsResource/get/?source_url=${encodeURIComponent(
params.source_url
)}&data=${encodeURIComponent(JSON.stringify(params.data))}`;

const data = await request.get(URL);
return parseCommentsResponse(data);
}
34 changes: 22 additions & 12 deletions src/functions/getPin.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import { Api } from "../api/api";
import request from "../fetch/request";
import { parseSpecificScriptTags } from "../parser/parser.tags";
import { parsePinData } from "../parser/parese.pin";
import type { ParsedPinData } from "../interfaces";
import { PinV4Response } from "../interfaces";
import { parsePinV4 } from "../parser/parse.pin";

export async function getPin(id: string): Promise<ParsedPinData> {
const requestCall = await request.getText(Api.baseURL + `/pin/${id}`);
const response = requestCall;
const parseScriptAllScriptTags = parseSpecificScriptTags(response);
const scriptFormValidation = parseScriptAllScriptTags[0];
const JSONConversion = JSON.parse(scriptFormValidation);
const data = parsePinData(JSONConversion);
return { ...data };
export async function getPin<T extends string>(id: T): Promise<PinV4Response> {
const params = {
source_url: `/pin/${id}/`,
data: {
options: {
id: `${id}`,
field_set_key: "auth_web_main_pin",
noCache: true,
fetch_visual_search_objects: true,
},
context: {},
},
};

const URL: string = `https://in.pinterest.com/resource/PinResource/get/?source_url=${encodeURIComponent(
params.source_url
)}&data=${encodeURIComponent(JSON.stringify(params.data))}`;

const data = await request.get(URL);
return parsePinV4(data);
}
18 changes: 18 additions & 0 deletions src/functions/getPinV3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Api } from "../api/api";
import request from "../fetch/request";
import { parseSpecificScriptTags } from "../parser/parser.tags";
import { parsePinData } from "../parser/parse.pin_v3";
import type { ParsedPinData } from "../interfaces";

/**
* @deprecated
*/
export async function getPinV3(id: string): Promise<ParsedPinData> {
const requestCall = await request.getText(Api.baseURL + `/pin/${id}`);
const response = requestCall;
const parseScriptAllScriptTags = parseSpecificScriptTags(response);
const scriptFormValidation = parseScriptAllScriptTags[0];
const JSONConversion = JSON.parse(scriptFormValidation);
const data = parsePinData(JSONConversion);
return { ...data };
}
2 changes: 2 additions & 0 deletions src/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export * from "./searchBoards";
export * from "./getBoards";
export * from "./getPin";
export * from "./autocomplete";
export * from "./getPinV3";
export * from "./getComments";
16 changes: 16 additions & 0 deletions src/interfaces/Comments.Response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export interface CommentUser {
username?: string;
displayName?: string;
image?: string;
id?: string;
}

export interface CommentsResultResponse {
text?: string;
createdAt?: string;
user?: CommentUser;
details?: string;
likes?: number;
image?: string[];
tags?: string[];
}
63 changes: 63 additions & 0 deletions src/interfaces/Pin.ResponseV4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
export interface PinV4Images {
lg?: string;
md?: string;
xl?: string;
og?: string;
}

export interface PinV4Reactions {
label?: string;
numbers?: number;
}

export interface PinV4BoardOwner {
id?: string;
username?: string;
fullName?: string;
imageMediumUrl?: string;
isVerifiedMerchant?: boolean;
}

export interface PinV4Board {
name?: string;
id?: string;
imageThumbnail?: string;
url?: string;
description?: string;
privacy?: string;
isCollaborative?: boolean;
layout?: string;
pinThumbnailUrls?: string[];
owner?: PinV4BoardOwner;
}

export interface PinV4Creator {
fullName?: string;
username?: string;
image?: string;
}

export interface PinV4CarouselSlot {
image?: string;
title?: string;
details?: string;
id?: string;
}

export interface PinV4Response {
title?: string;
images?: PinV4Images;
id?: string;
video?: string;
reactions?: PinV4Reactions;
commentCount?: number;
category?: string;
board?: PinV4Board;
creator?: PinV4Creator;
description?: string;
createdAt?: string;
shareCount?: number;
repinCount?: number;
favorites?: number;
carousel?: PinV4CarouselSlot[];
}
2 changes: 2 additions & 0 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export type * from "./SearchOptions";
export type * from "./Pin.Results";
export type * from "./Pin.Response";
export type * from "./Search.AutoCompletion";
export type * from "./Pin.ResponseV4";
export type * from "./Comments.Response";
Loading

0 comments on commit 375971e

Please sign in to comment.