Guest User

Untitled

a guest
Jan 23rd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. const puppeteer = require('puppeteer')
  2. const express = require('express')
  3. const bodyParser = require('body-parser')
  4. const crypto = require('crypto')
  5. const fs = require('fs')
  6.  
  7. const port = 3000
  8. var app = express()
  9. app.use(bodyParser.json())
  10.  
  11. const runJob = async (url) => {
  12. var browser
  13. var title = ''
  14. var filename = ''
  15. var html = ''
  16.  
  17. try {
  18. browser = await puppeteer.launch({headless: true})
  19. const page = await browser.newPage()
  20. page.setViewport({ width: 1280, height: 1024 })
  21.  
  22. // consider navigation to be finished when there are no more than 2 network connections for at least 500 ms
  23. await page.goto(url, {"waitUntil" : "networkidle2"})
  24.  
  25. title = await page.title()
  26. filename = crypto.createHash('md5').update(url).digest('hex')
  27. await page.screenshot({path: 'pages/' + filename + '.png'})
  28. html = await page.content();
  29. // save file synchronously
  30. fs.writeFileSync('pages/' + filename + '.html', html, function(err) {
  31. if (err) {
  32. return console.log('Error writing html: ' + err)
  33. }
  34. });
  35. } catch (err) {
  36. console.log('There was an error: ' + err.message)
  37. } finally {
  38. if (browser) {
  39. await browser.close()
  40. }
  41. }
  42.  
  43. return [filename, html]
  44. }
  45.  
  46. app.get('/get_page', async function(req, res) {
  47. var url = req.body.url || req.query.url
  48. console.log('Got URL: ' + url)
  49. var [id, body] = await runJob(url)
  50. console.log('Processed ID: ' + id + ' for URL: ' + url)
  51. await res.send(body)
  52. });
  53.  
  54. // handle errors
  55. app.use((err, req, res, next) => {
  56. if (! err) return next()
  57. res.status(500)
  58. res.send('500: Internal server error')
  59. })
  60. process.setMaxListeners(0);
  61. process.on('uncaughtException', function(error) {
  62. console.log('uncaughtException Error: ' + error)
  63. })
  64. process.on('unhandledRejection', function(error) {
  65. console.log('unhandledRejection Error: ' + error)
  66. })
  67.  
  68. app.listen(port)
  69. console.log('Server is listening on port ' + port)
Add Comment
Please, Sign In to add comment