Skip to content

Instantly share code, notes, and snippets.

@pastak
Last active August 29, 2015 14:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pastak/10bae1ee5c897c012713 to your computer and use it in GitHub Desktop.
Save pastak/10bae1ee5c897c012713 to your computer and use it in GitHub Desktop.
var list = require('./list.json');
var fs = require('fs');
var page = require('webpage').create();
var result = require('./result.json') || {};
var finalize = function(){
fs.write('result.json', JSON.stringify(result),'w');
phantom.exit()
}
var failSeach = function(text) {
return !(text.indexOf('画像が大きすぎるか、') === -1 &&
text.indexOf('似の画像画像を報告ご報告いただきありがとうございます。') === -1 &&
text.indexOf('特徴を表す語句') === -1)
}
var getPage = function(i, retry) {
if(result[list[i].image_id]){
getPage(i+1);
return false;
}
if(list.length <= i){
finalize();
}
page.settings.userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36';
page.open('https://images.google.com/searchbyimage?image_url='+list[i].url, function(status) {
console.log('try: '+list[i].url)
console.log(status);
if(!status){
return false;
}
var text = page.evaluate(function(){
if(document.querySelector('.srg li.g')){
return Array.prototype.slice.apply(document.querySelectorAll('.srg .g:first-child')).map(function(item){return item.textContent})
}else{
return false;
}
})
if(!retry && !text && page.plainText.indexOf('画像が大きすぎるか、') > -1){
setTimeout(function(){
getPage(i, true)
}, 5000)
return false
}else if(failSeach(page.plainText)){
getPage(i+1);
return false;
}
console.log('result'+text+"\n === \n");
if(text){
result[list[i].image_id] = {
text: text,
imageData: list[i]
}
fs.write('result.json', JSON.stringify(result),'w');
}
setTimeout(function(){
getPage(i + 1)
},1000)
});
}
getPage(0)
[
{
"image_id": "dc107262a37882ded68d0d1861a13709",
"created_at": "2015-02-09T16:31:24.682Z",
"alias_id": "_f3ced6d80d939089a9500ce6585c3db9",
"deletable": true,
"drawable": true,
"star": false,
"has_mp4": false,
"url": "https://i.gyazo.com/dc107262a37882ded68d0d1861a13709.png",
"thumb_url": "https://i.gyazo.com/thumb/200/_f3ced6d80d939089a9500ce6585c3db9.png",
"crop_thumb_url": "https://i.gyazo.com/thumb/22_crop/_f3ced6d80d939089a9500ce6585c3db9.png",
"large_thumb_url": "https://i.gyazo.com/thumb/360/_f3ced6d80d939089a9500ce6585c3db9.png",
"cover_url": "https://i.gyazo.com/cover/dc107262a37882ded68d0d1861a13709.png",
"permalink_url": "http://gyazo.com/dc107262a37882ded68d0d1861a13709"
},
{
"image_id": "65148f4c0f21169cc7cca954a630c47b",
"created_at": "2015-02-09T16:31:20.944Z",
"alias_id": "_4c1b3e8612f6bbcbbf8dd573b1701d28",
"deletable": true,
"drawable": true,
"star": false,
"has_mp4": false,
"url": "https://i.gyazo.com/65148f4c0f21169cc7cca954a630c47b.png",
"thumb_url": "https://i.gyazo.com/thumb/200/_4c1b3e8612f6bbcbbf8dd573b1701d28.png",
"crop_thumb_url": "https://i.gyazo.com/thumb/22_crop/_4c1b3e8612f6bbcbbf8dd573b1701d28.png",
"large_thumb_url": "https://i.gyazo.com/thumb/360/_4c1b3e8612f6bbcbbf8dd573b1701d28.png",
"cover_url": "https://i.gyazo.com/cover/65148f4c0f21169cc7cca954a630c47b.png",
"permalink_url": "http://gyazo.com/65148f4c0f21169cc7cca954a630c47b"
},
{
"image_id": "bac70ef6862635fba8eaf1b608efedd3",
"created_at": "2015-02-09T16:31:15.361Z",
"alias_id": "_e99a7a0384d76a06472e858087a3cb64",
"deletable": true,
"drawable": true,
"star": false,
"has_mp4": false,
"url": "https://i.gyazo.com/bac70ef6862635fba8eaf1b608efedd3.png",
"thumb_url": "https://i.gyazo.com/thumb/200/_e99a7a0384d76a06472e858087a3cb64.png",
"crop_thumb_url": "https://i.gyazo.com/thumb/22_crop/_e99a7a0384d76a06472e858087a3cb64.png",
"large_thumb_url": "https://i.gyazo.com/thumb/360/_e99a7a0384d76a06472e858087a3cb64.png",
"cover_url": "https://i.gyazo.com/cover/bac70ef6862635fba8eaf1b608efedd3.png",
"permalink_url": "http://gyazo.com/bac70ef6862635fba8eaf1b608efedd3"
},
{
"image_id": "7f1b9f614d650e75ed26ce3d240f1aa1",
"created_at": "2015-02-09T16:31:09.579Z",
"alias_id": "_d66d73159eab859a2ca32891d87f4d7c",
"deletable": true,
"drawable": true,
"star": false,
"has_mp4": false,
"url": "https://i.gyazo.com/7f1b9f614d650e75ed26ce3d240f1aa1.png",
"thumb_url": "https://i.gyazo.com/thumb/200/_d66d73159eab859a2ca32891d87f4d7c.png",
"crop_thumb_url": "https://i.gyazo.com/thumb/22_crop/_d66d73159eab859a2ca32891d87f4d7c.png",
"large_thumb_url": "https://i.gyazo.com/thumb/360/_d66d73159eab859a2ca32891d87f4d7c.png",
"cover_url": "https://i.gyazo.com/cover/7f1b9f614d650e75ed26ce3d240f1aa1.png",
"permalink_url": "http://gyazo.com/7f1b9f614d650e75ed26ce3d240f1aa1"
},
{
"image_id": "76cb3f489bf22f85a158ad7aa421dea4",
"created_at": "2015-02-09T16:31:05.284Z",
"alias_id": "_16a9d5166c9602d62075574f017e4b8c",
"deletable": true,
"drawable": true,
"star": false,
"has_mp4": false,
"url": "https://i.gyazo.com/76cb3f489bf22f85a158ad7aa421dea4.png",
"thumb_url": "https://i.gyazo.com/thumb/200/_16a9d5166c9602d62075574f017e4b8c.png",
"crop_thumb_url": "https://i.gyazo.com/thumb/22_crop/_16a9d5166c9602d62075574f017e4b8c.png",
"large_thumb_url": "https://i.gyazo.com/thumb/360/_16a9d5166c9602d62075574f017e4b8c.png",
"cover_url": "https://i.gyazo.com/cover/76cb3f489bf22f85a158ad7aa421dea4.png",
"permalink_url": "http://gyazo.com/76cb3f489bf22f85a158ad7aa421dea4"
},
{
"image_id": "8b83f37db9497334dce48215dadd5a45",
"created_at": "2015-02-09T16:31:00.056Z",
"alias_id": "_216299692d501ab91eecf1f49ce7e040",
"deletable": true,
"drawable": true,
"star": false,
"has_mp4": false,
"url": "https://i.gyazo.com/8b83f37db9497334dce48215dadd5a45.png",
"thumb_url": "https://i.gyazo.com/thumb/200/_216299692d501ab91eecf1f49ce7e040.png",
"crop_thumb_url": "https://i.gyazo.com/thumb/22_crop/_216299692d501ab91eecf1f49ce7e040.png",
"large_thumb_url": "https://i.gyazo.com/thumb/360/_216299692d501ab91eecf1f49ce7e040.png",
"cover_url": "https://i.gyazo.com/cover/8b83f37db9497334dce48215dadd5a45.png",
"permalink_url": "http://gyazo.com/8b83f37db9497334dce48215dadd5a45"
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment