Created
August 11, 2017 16:12
-
-
Save chimame/2563d85ab7aa193bedcb84653f0db264 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const aws = require('aws-sdk') | |
const LineStream = require('byline').LineStream | |
const client = require('cheerio-httpcli') | |
const s3 = new aws.S3({ apiVersion: '2006-03-01' }) | |
const ls = new LineStream() | |
exports.handler = (event, context, callback) => { | |
// Get the object from the event and show its content type | |
const bucket = event.Records[0].s3.bucket.name | |
const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' ')) | |
const params = { | |
Bucket: bucket, | |
Key: key, | |
} | |
const s3s = s3.getObject(params).createReadStream() | |
s3s.setEncoding('utf-8') | |
let result = '' | |
s3s.pipe(ls).on('data', function(line) { | |
let response = client.fetchSync('http://www.google.co.jp/search', { q: 'site:'+line, safe: 'high' }) | |
if(response.$('body').html().indexOf('一致する情報は見つかりませんでした') > -1) { | |
result = result + line + ":adult\n" | |
} else { | |
result = result + line + ":normal\n" | |
} | |
}) | |
s3s.on('end', function() { | |
const putBucket = bucket | |
const putKey = key+'.out' | |
//const contentType = "Content-Type Header" | |
const put = {Bucket: putBucket, Key: putKey, Body: result} | |
s3.putObject(put, function(err, data) { | |
if (err) { | |
console.error(err) | |
const message = `Error putting object ${putKey} from bucket ${putBucket}.` | |
console.error(message) | |
callback(message) | |
} else { | |
//or callback(null) | |
context.done() | |
} | |
}) | |
}) | |
/* | |
s3.getObject(params, (err, data) => { | |
if (err) { | |
console.log(err) | |
const message = `Error getting object ${key} from bucket ${bucket}. Make sure they exist and your bucket is in the same region as this function.` | |
console.log(message) | |
callback(message) | |
} else { | |
const putBucket = bucket | |
const putKey = key+'.out' | |
//const contentType = "Content-Typeヘッダ" | |
//console.log(data.Body.toString()) | |
let result = '' | |
data.Body.toString().split(/\r\n|\r|\n/).forEach(function(site) { | |
const response = client.fetchSync('http://www.google.co.jp/search', { q: 'site:'+site, safe: 'high' }) | |
if(response.$('body').html().indexOf('一致する情報は見つかりませんでした') > -1) { | |
result = result + site + ":adult\n" | |
} else { | |
result = result + site + ":normal\n" | |
} | |
}) | |
const put = {Bucket: putBucket, Key: putKey, Body: result} | |
s3.putObject(put, function(err, data) { | |
if (err) { | |
console.error(err) | |
const message = `Error putting object ${putKey} from bucket ${putBucket}.` | |
console.error(message) | |
callback(message) | |
} else { | |
//callback(null) | |
context.done() | |
} | |
}) | |
} | |
}) | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment