Skip to content

Commit

Permalink
Merge branch 'example'
Browse files Browse the repository at this point in the history
  • Loading branch information
fand committed Mar 28, 2017
2 parents 74f8c8f + 0c811cd commit e12742a
Show file tree
Hide file tree
Showing 4 changed files with 1,378 additions and 0 deletions.
10 changes: 10 additions & 0 deletions examples/image-downloader/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# image-downloader

Download random `cat` image from https://giphy.com/ to 'out.gif'.

## Usage

```
$ npm i
$ npm start
```
29 changes: 29 additions & 0 deletions examples/image-downloader/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var fs = require('fs');
var Nightmare = require('nightmare');
var download = require('download');

// Search 'cat' images from GIPHY
const urls = await Nightmare({ show: true, loadTimeout: 5000, executionTimeout: 5000 })
.goto('https://giphy.com/')
.type('#search-box', 'cat')
.click('#search-button')
.wait('#gif-results')
.evaluate(() => {
const images = Array.from(document.querySelectorAll('[data-gif] img'));
return images.map(i => i.src);
})
.end();

// Pick one of the images
const url = urls[Math.floor(Math.random() * urls.length)]

// Convert GIF url
const imageUrl = url.replace(/^.*\/(media\/\w*)\/.*$/, 'https://media.giphy.com/$1/giphy.gif');

// Download image
const image = await download(imageUrl);

// Save image to out.gif
fs.writeFileSync(`out.gif`, image);

console.log('>> DONE!');
18 changes: 18 additions & 0 deletions examples/image-downloader/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "image-downloader",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "$(npm bin)/async-node index.js"
},
"keywords": [],
"author": "fand <fand@gmork.in>",
"license": "MIT",
"dependencies": {
"@fand/async-node": "^0.1.0",
"download": "^5.0.3",
"nightmare": "^2.10.0"
}
}

0 comments on commit e12742a

Please sign in to comment.