Created
January 1, 2019 14:33
-
-
Save tanshio/7d15486f44960a738805ce60122308e4 to your computer and use it in GitHub Desktop.
Recall bot
This file contains 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
var moment = Moment.load(); | |
var domain = 'http://www.recall.go.jp'; | |
function myFunction() { | |
var URL = "http://www.recall.go.jp/new/"; | |
var html = UrlFetchApp.fetch(URL).getContentText(); | |
var doc = Parser.data(html).from('<div id="wrapper">').to('</div><!--one_colum end-->').build(); | |
var table = Parser.data(doc).from('<table id="importance_info">').to('</table>').build(); | |
var list = Parser.data(table).from('<tr>').to('</tr>').iterate(); | |
list.shift(); | |
var _count = 0; | |
var arr = []; | |
list.forEach(function(e){ | |
console.log(e); | |
var date = moment(Parser.data(e).from('<td class="im_date">').to('</td>').build()); | |
var today = moment(); | |
var diff = today.diff(date, 'days'); | |
if(diff === 0) { | |
_count++; | |
var _arr = []; | |
//日付、タイトル、レベル、カテゴリ、リンク、画像URL | |
var cat = Parser.data(e).from('<td class="im_cate">').to('</td>').build(); | |
var detail = Parser.data(e).from('<td class="im_detail">').to('</td>').iterate()[0]; | |
var _title = Parser.data(detail).from('">').to('</a>').build().replace(/\n|\s/g,""); | |
var sepTitle = _title.match(/(.*):(.*$)/); | |
var level = sepTitle[2] | |
var title = sepTitle[1]; | |
var link = Parser.data(detail).from('href="').to('"').build().replace('..',domain); | |
_arr.push(date.format("YYYY/MM/DD")); | |
_arr.push(title); | |
_arr.push(level); | |
_arr.push(cat); | |
_arr.push(link); | |
if(hasImg(Parser.data(e).from('<div class="img_box clearfix">').to('</div>').build())) { | |
var _wrapper = Parser.data(e).from('<div class="img_box clearfix">').to('</div>').iterate()[0]; | |
var src = Parser.data(_wrapper).from('<img src="').to('"').build(); | |
_arr.push(domain + src); | |
} else { | |
_arr.push(''); | |
} | |
arr.push(_arr); | |
} | |
}) | |
// var cat = Parser.data(list[0]).from('<td class="im_cate">').to('</td>').build(); | |
Logger.log(arr); | |
if(arr.length > 0) { | |
insertRecords(arr); | |
sendTweetwithImage(arr); | |
}else { | |
MailApp.sendEmail('rakuburomail@gmail.com', 'なし', '確認'); | |
} | |
Logger.log(_count); | |
if(_count === 10) { | |
MailApp.sendEmail('rakuburomail@gmail.com', '2P目に行ってそう', '確認'); | |
} | |
} | |
function hasImg($elem) { | |
return /<img/.test($elem); | |
} | |
function insertRecords(arr){ | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("test"); | |
if (sheet != null) { | |
Logger.log(sheet.getIndex()); | |
} | |
var rows = arr.length; | |
var cols = arr[0].length; | |
sheet.getRange(sheet.getLastRow()+1,1,rows,cols).setValues(arr); | |
} | |
function sendTweetwithImage(arr) { | |
var twitterKeys= { | |
TWITTER_CONSUMER_KEY: "TWITTER_CONSUMER_KEY", | |
TWITTER_CONSUMER_SECRET: "TWITTER_CONSUMER_SECRET", | |
TWITTER_ACCESS_TOKEN: "TWITTER_ACCESS_TOKEN", | |
TWITTER_ACCESS_SECRET: "TWITTER_ACCESS_SECRET" | |
}; | |
var props = PropertiesService.getUserProperties(); | |
props.setProperties(twitterKeys); | |
var twit = new Twitterlib.OAuth(props); | |
if ( twit.hasAccess() ) { | |
try { | |
// DriveApp.getFileById(id).getBlob() | |
arr.forEach(function(e){ | |
if(e[5].length > 3) { | |
var imageUrl = e[5]; | |
var imageBlob = twit.grabImage(imageUrl, "image/jpeg"); | |
var uploadImg = twit.uploadMedia(imageBlob); | |
if (uploadImg) { | |
var status = '【'+e[2]+'】'+'【'+e[3]+'】'+e[1]+' '+e[4] + ' #リコール'; | |
var response = twit.sendTweet(status, { | |
media_ids: uploadImg.media_id_string | |
}); | |
if (response) { | |
Logger.log("Tweet Sent " + response.id_str); | |
} else { | |
// Tweet could not be sent | |
// Go to View -> Logs to see the error message | |
} | |
} | |
}else { | |
var imageUrl = e[5]; | |
var imageBlob = twit.grabImage(imageUrl, "image/jpeg"); | |
var uploadImg = twit.uploadMedia(imageBlob); | |
if (uploadImg) { | |
var status = '【'+e[2]+'】'+'【'+e[3]+'】'+e[1]+' '+e[4]+ ' #リコール'; | |
var response = twit.sendTweet(status, { | |
media_ids: uploadImg.media_id_string | |
}); | |
if (response) { | |
Logger.log("Tweet Sent " + response.id_str); | |
} else { | |
// Tweet could not be sent | |
// Go to View -> Logs to see the error message | |
} | |
} | |
} | |
}) | |
} catch (f) { | |
Logger.log(f.toString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment