-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathebay.service.ts
90 lines (75 loc) · 2.71 KB
/
ebay.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import * as fs from 'fs';
import { Injectable, OnModuleDestroy } from '@nestjs/common';
import { Browser } from 'puppeteer';
const APIFY_TOKEN = process.env.APIFY_TOKEN;
@Injectable()
export class EbayService implements OnModuleDestroy {
constructor(private readonly browser: Browser) {}
async onModuleDestroy() {
await this.browser.close();
}
async getEbayItem(itemUrl: string) {
const response = await fetch(
`https://api.apify.com/v2/acts/dtrungtin~ebay-items-scraper/run-sync-get-dataset-items?token=${APIFY_TOKEN}`,
{
method: 'POST',
body: JSON.stringify({
maxItems: 1,
proxyConfig: {
useApifyProxy: true,
},
startUrls: [
{
url: itemUrl,
},
],
}),
headers: {
'content-type': 'application/json',
},
},
);
return response.json();
}
async getEbayMerchantData(merchantId: string) {
const merchantUrl = `https://www.ebay.com/usr/${merchantId}`;
const page = await this.browser.newPage();
await page.setDefaultNavigationTimeout(60000);
await page.goto(merchantUrl);
await page.solveRecaptchas();
const html = await page.content();
//save html to file
fs.writeFileSync('response.html', html);
//save screenshot to file
await page.screenshot({ path: 'response.png', fullPage: true });
await page.waitForSelector("[class='str-seller-card__stats-content']", { timeout: 60000 });
const element1 = await page.$("[class='str-seller-card__stats-content']");
const merchantDataRaw = await page.evaluate((el) => el.textContent, element1);
const element2 = await page.$("[class='str-about-description__seller-info']");
const memberSinceRaw = await page.evaluate((el) => el.textContent, element2);
// 100% Positive feedback3.3K Items sold278 Followers
// 100% Positive feedback (1)
console.log(merchantDataRaw);
console.log(memberSinceRaw);
const match1 = merchantDataRaw.match(/^(\d+%)\s/);
const positiveFeedback = parseInt((match1 ? match1[1] : '0%').replace('%', ''));
const match2 = merchantDataRaw.match(/.*feedback(.*)\sItems/);
const itemsSoldStr = match2 ? match2[1] : '0';
let itemsSold = 0;
if (itemsSoldStr.includes('K')) {
itemsSold = parseFloat(itemsSoldStr.replace('K', '')) * 1000;
} else {
itemsSold = parseInt(itemsSoldStr);
}
const match3 = memberSinceRaw.match(/since:(.*\d)/);
const memberSinceDate = match3 ? new Date(match3[1]) : 'N/A';
console.log(memberSinceDate);
console.log(positiveFeedback);
console.log(itemsSold);
return {
itemsSold,
positiveFeedback,
memberSinceDate,
};
}
}