nazib61 commited on
Commit
e574575
·
verified ·
1 Parent(s): 120d8f4

Update scraper.js

Browse files
Files changed (1) hide show
  1. scraper.js +32 -23
scraper.js CHANGED
@@ -1,6 +1,6 @@
1
  const puppeteer = require('puppeteer-core');
2
 
3
- async function scrapeAll(query) {
4
  let browser;
5
  try {
6
  browser = await puppeteer.launch({
@@ -8,52 +8,61 @@ async function scrapeAll(query) {
8
  headless: 'new',
9
  args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage']
10
  });
11
-
12
  const page = await browser.newPage();
13
  await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36');
14
 
15
- // --- PHASE 1: PINTEREST ---
16
- const pinUrl = `https://www.pinterest.com/search/pins/?q=${encodeURIComponent(query)}&rs=typed`;
17
- try {
18
- await page.goto(pinUrl, { waitUntil: 'networkidle2', timeout: 15000 });
19
- const pinLinks = await page.evaluate(() => {
 
 
 
20
  return Array.from(document.querySelectorAll('a[href*="/pin/"]')).map(a => a.href);
21
  });
22
- const uniquePins = [...new Set(pinLinks)].slice(0, 15);
23
 
24
- for (const url of uniquePins) {
25
  const pPage = await browser.newPage();
26
  try {
27
- await pPage.goto(url, { waitUntil: 'domcontentloaded', timeout: 10000 });
28
  const content = await pPage.content();
29
  const match = content.match(/https:\/\/v1\.pinimg\.com\/videos\/mc\/[^\s"']+/);
30
  if (match) {
31
  const hashMatch = match[0].match(/([a-f0-9]{32})/);
32
  if (hashMatch) {
33
  const h = hashMatch[1];
34
- process.stdout.write(`SOURCE_PINT:https://v1.pinimg.com/videos/mc/720p/${h.substring(0,2)}/${h.substring(2,4)}/${h.substring(4,6)}/${h}.mp4\n`);
35
  }
36
  }
37
  } catch (e) {}
38
  await pPage.close();
39
  }
40
- } catch (e) { process.stderr.write("Pinterest fail, moving to Pexels\n"); }
 
 
 
 
 
41
 
42
- // --- PHASE 2: PEXELS ---
43
- const pexUrl = `https://www.pexels.com/search/videos/${encodeURIComponent(query)}/`;
44
- try {
45
- await page.goto(pexUrl, { waitUntil: 'networkidle2', timeout: 15000 });
46
- const pexVideos = await page.evaluate(() => {
47
- return Array.from(document.querySelectorAll('video source')).map(s => s.src);
48
  });
49
- pexVideos.forEach(v => process.stdout.write(`SOURCE_PEXE:${v}\n`));
50
- } catch (e) { process.stderr.write("Pexels fail\n"); }
 
51
 
52
- } catch (err) {
53
- process.stderr.write(err.message);
54
  } finally {
55
  if (browser) await browser.close();
56
  }
57
  }
58
 
59
- scrapeAll(process.argv.slice(2).join(' '));
 
 
 
1
  const puppeteer = require('puppeteer-core');
2
 
3
+ async function scrape(query, mode, orientation) {
4
  let browser;
5
  try {
6
  browser = await puppeteer.launch({
 
8
  headless: 'new',
9
  args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage']
10
  });
 
11
  const page = await browser.newPage();
12
  await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36');
13
 
14
+ if (mode === "pinterest") {
15
+ const searchUrl = `https://www.pinterest.com/search/pins/?q=${encodeURIComponent(query)}&rs=typed`;
16
+ await page.goto(searchUrl, { waitUntil: 'networkidle2', timeout: 30000 });
17
+ await page.evaluate(async () => {
18
+ window.scrollBy(0, 1500);
19
+ await new Promise(r => setTimeout(r, 1000));
20
+ });
21
+ const pinUrls = await page.evaluate(() => {
22
  return Array.from(document.querySelectorAll('a[href*="/pin/"]')).map(a => a.href);
23
  });
24
+ const uniquePins = [...new Set(pinUrls)].slice(0, 30);
25
 
26
+ for (const pinUrl of uniquePins) {
27
  const pPage = await browser.newPage();
28
  try {
29
+ await pPage.goto(pinUrl, { waitUntil: 'domcontentloaded', timeout: 10000 });
30
  const content = await pPage.content();
31
  const match = content.match(/https:\/\/v1\.pinimg\.com\/videos\/mc\/[^\s"']+/);
32
  if (match) {
33
  const hashMatch = match[0].match(/([a-f0-9]{32})/);
34
  if (hashMatch) {
35
  const h = hashMatch[1];
36
+ console.log(`https://v1.pinimg.com/videos/mc/720p/${h.substring(0,2)}/${h.substring(2,4)}/${h.substring(4,6)}/${h}.mp4`);
37
  }
38
  }
39
  } catch (e) {}
40
  await pPage.close();
41
  }
42
+ }
43
+ else if (mode === "pexels") {
44
+ // Pexels supports orientation filter directly in the URL!
45
+ const pexelsOri = orientation.toLowerCase() === "any" ? "" : orientation.toLowerCase();
46
+ const searchUrl = `https://www.pexels.com/search/videos/${encodeURIComponent(query)}/?orientation=${pexelsOri}`;
47
+ await page.goto(searchUrl, { waitUntil: 'networkidle2', timeout: 30000 });
48
 
49
+ const videoUrls = await page.evaluate(() => {
50
+ // Find all video source tags on Pexels search page
51
+ return Array.from(document.querySelectorAll('video source'))
52
+ .map(s => s.src)
53
+ .filter(src => src.includes('.mp4') || src.includes('video-preview'));
 
54
  });
55
+
56
+ videoUrls.forEach(url => console.log(url));
57
+ }
58
 
59
+ } catch (error) {
60
+ process.stderr.write(error.message);
61
  } finally {
62
  if (browser) await browser.close();
63
  }
64
  }
65
 
66
+ const args = process.argv.slice(2);
67
+ // args[0] = query, args[1] = mode, args[2] = orientation
68
+ scrape(args[0], args[1] || "pinterest", args[2] || "any");