diff options
| author | Ab30657 <amar_ronaldo30657@yahoo.com> | 2021-04-11 09:29:00 +0545 |
|---|---|---|
| committer | Ab30657 <amar_ronaldo30657@yahoo.com> | 2021-04-11 09:29:00 +0545 |
| commit | 8bb9b435244d68297b900e5a14cf90d5b5088b0d (patch) | |
| tree | a6dee4abcdcbbedb3f0a24cb1441b87b857adea7 /src/scraper/scrape.js | |
| parent | 2927b2cbe0e623c0a8c11b92a8dd987e1a03e3e4 (diff) | |
| parent | 0007b7e4fb5b48334b35e1fd77c2bcf814089f7a (diff) | |
| download | StreamFinder-8bb9b435244d68297b900e5a14cf90d5b5088b0d.tar.xz StreamFinder-8bb9b435244d68297b900e5a14cf90d5b5088b0d.zip | |
Merge branch 'main' of https://github.com/FivePixels/StreamFinder into amar
Diffstat (limited to 'src/scraper/scrape.js')
| -rw-r--r-- | src/scraper/scrape.js | 73 |
1 files changed, 47 insertions, 26 deletions
diff --git a/src/scraper/scrape.js b/src/scraper/scrape.js index 131ae40..605c234 100644 --- a/src/scraper/scrape.js +++ b/src/scraper/scrape.js @@ -1,33 +1,54 @@ const puppeteer = require('puppeteer'); -const mongodb = require('mongodb') +const MongoClient = require('mongodb').MongoClient; // Specifically for scraping // Stores in database once finished scraping -// query is the search string (title of movie or show) and the type (music, show, movie) -// will be used +// query is the movie or show name +// type is the media type ("tv" for tv show or "movie") -async function performSearch(query, type) { - const browser = await puppeteer.launch({ - headless: true - }); - const pages = await browser.pages(); - const page = pages[0]; - await page.goto('https://google.com'); - const searchBox = await page.$x("/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input"); - await searchBox[0].type(`${query} streaming`); - await page.keyboard.press('Enter'); - await page.waitForNavigation(); - const resultsContainer = await page.$$('.r0VsPb') - var streamingPlatforms; - var streamingPrices; - for (let i = 0; i < resultsContainer.length; i++) { - streamingPlatforms = await resultsContainer[i].$$eval('.i3LlFf', nodes => nodes.map(n => n.innerText)); - streamingPrices = await resultsContainer[i].$$eval('.V8xno', nodes => nodes.map(n => n.innerText)) +exports.performSearch = async function (id, query, type) { + try { + const uri = "mongodb+srv://user0:8HL0NBINt6B8mIYF@cluster0.kfyrm.mongodb.net/StreamFinder?retryWrites=true&w=majority"; + const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true }); + await client.connect(); + const database = client.db("db"); + const tv = database.collection("tv"); + const movie = database.collection('movie') + const browser = await puppeteer.launch({ + headless: true + }); + const pages = await browser.pages(); + const page = pages[0]; + await page.goto('https://google.com'); + const searchBox = await page.$x("/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input"); + await searchBox[0].type(`${query} ${type} streaming`); + await page.keyboard.press('Enter'); + await page.waitForNavigation(); + const resultsContainer = await page.$$('.r0VsPb') + var streamingPlatforms; + var streamingPrices; + for (let i = 0; i < resultsContainer.length; i++) { + streamingPlatforms = await resultsContainer[i].$$eval('.i3LlFf', nodes => nodes.map(n => n.innerText)); + streamingPrices = await resultsContainer[i].$$eval('.V8xno', nodes => nodes.map(n => n.innerText)) + } + if (type == "movie") { + await movie.insertOne({ + title: query, + id: id, + service: streamingPlatforms, + price: streamingPrices + }) + } else if (type == "tv") { + await tv.insertOne({ + title: query, + id: id, + service: streamingPlatforms, + price: streamingPrices + }) + } + await client.close() + await browser.close(); + } catch (error) { + console.log(error) } - console.log(streamingPlatforms); - console.log(streamingPrices); - // do the database storage - await browser.close(); } - -// performSearch('spongebob', 'tv');
\ No newline at end of file |
