Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crssnky/80178ab1d21afd56200eb53b54cda6cd to your computer and use it in GitHub Desktop.
Save crssnky/80178ab1d21afd56200eb53b54cda6cd to your computer and use it in GitHub Desktop.
var twitter = require('twitter');
var request=require('request');
var fs= require('fs');
var webshot=require('webshot');
var Q=require('q');
// Tweet module
var client = new twitter({
consumer_key: '',
consumer_secret: '',
access_token_key: '',
access_token_secret: '',
});
// http request
var Query = ["PREFIX schema: <http://schema.org/>SELECT (sample(?o) as ?date) (sample(?n) as ?name) (sample(?sub) as ?s) WHERE { ?sub schema:birthDate ?o; schema:name|schema:alternateName ?n;FILTER(regex(str(?o), \"", "\" )).}group by(?n)order by(?name)"];
var date = new Date();
var month = date.getMonth() + 1;
var day = date.getDate();
var now = (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day);
// console.log(now);
var url = 'https://sparql.crssnky.xyz/spql/imas/query?output=json&query=' + encodeURIComponent(Query[0] + now + Query[1]);
// text create
var text="本日の誕生日\n";
Q.when(url)
.then(function(url){
var d = Q.defer();
request.get(url,function(err,res,body){
// console.log(body);
var json=JSON.parse(body)["results"]["bindings"];
d.resolve(json);
});
return d.promise;
})
.then(function(json){
if(json["s"]){d.reject("no exist.");}
var d=Q.defer();
var urls=[];
// console.log("length:"+json.length);
json.forEach(function(i,index){
// console.log("i:"+index);
text+=i.name.value+":プロフィール→"+i.s.value+" , \n";
webshot(i.s.value,'birthday'+index+'.png',function(err){
var data=fs.readFileSync('birthday'+index+'.png');
client.post('media/upload',{media:data},function(err,media,response){
if(err){
// console.log(err);
d.reject("err.");
}
urls.push(media.media_id_string);
// console.log("url:"+media.media_id_string);
if(index==json.length-1){
d.resolve(urls);
}
});
});
});
return d.promise;
})
.then(function(urls){
var d=Q.defer();
// console.log("tweet:"+urls);
var stat={
status:text.substr(0,text.length-4),
media_ids:""
}
for(var i=0;i<urls.length;i++){
stat.media_ids+=urls[i]+",";
}
client.post('statuses/update', stat,function(err,tweet,response){
if(err){
// console.log("twErr:"+err);
}else{
// console.log("post:"+tweet);
}
});
return d.promise;
})
.done(function(){/* console.log("done");*/});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment