40歳からのキャリアチェンジ

20代はエンジニア・PM、30代はWeb系エンジニア向けのキャリアアドバイザー。40代の今はフリーランスで開発含めて色々やってます。技術ネタとしてはRuby/RailsとJavaScript関連あたり

017-YQL

ブログ本文はHTTPClient使えば取得できるのはわかったのですが、その中で必要な箇所だけ抜き出すとなると面倒かなと思っていました。
ただ、なんとなくYQL使えばいけそうかなーと漠然と思っていたのと、前回のエントリを読んでいただいたdonayamaさんからアドバイスもらったのでYQL試すことにしました。

YQL とは?

SQLライクな構文でインターネット上の各種リソースを取得できるUS Yahooが提供しているサービスです。
Yahoo! Query Language

TitaniumのAPIにはTitanium.Yahoo.yqlというそのものずばりなものがあるのでこちらの情報を参考にしながら以下のようなものを書いてみました

var Crawler = {
  isConnected:function(){
    if(Titanium.Network.online == false){
      return false;
    } else {
      return true;
    }
  }
}
Crawler.parse = function(url){
  this.first_url = url;
};
Crawler.parse.prototype = {
  start:function(){
    if(Crawler.isConnected()){

      Titanium.Yahoo.yql('select * from html where url="' + this.first_url + '"',function(e){
	try {
	  Titanium.API.info(e.data.body);

	  this.contents = e.data.body;
	  
	  entry_title = this.contents.div.div[1].div.div[0].h1;
	  entry_text =  this.contents.div.div[1].div.div[0].div[0].p;
	  previous_link = this.contents.div.div[1].div.div[0].p[0].a[0].href;
	  previous_title = this.contents.div.div[1].div.div[0].p[0].a[0].title;
	  next_link = this.contents.div.div[1].div.div[0].p[0].a[1].href;
	  next_title = this.contents.div.div[1].div.div[0].p[0].a[1].title;
	  Titanium.API.info(entry_title);
	  Titanium.API.info(entry_text);
	  Titanium.API.info('url:' + previous_link + 'title' + previous_title );
	  Titanium.API.info('url:' + next_link + 'title' + next_title )
	} catch(E) {
	  Titanium.API.error("Error = " + E);
	}
      });
    } 
    return this;
  }
}

よし、これでいけそうと思ったので

〜中略〜
Crawler.parse = function(url){
  this.first_url = url;
  this.html_body = null;
};
Crawler.parse.prototype = {
  start:function(){
〜中略〜
      Titanium.Yahoo.yql('select * from html where url="' + this.first_url + '"',function(e){
	try {
	  this.html_body = e.data.body;
〜中略〜
}

みたいな感じにしたのですが、this.html_bodyのthisが自分が意図したCrawler.parseではなくどうも違うものを指しているみたいで、思ったような動作してくれない。

JavaScript のスコープについての理解が正直自信がないのがここにきて壁になっている気がする