<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Wei&#039;s Blog &#187; Mechanize</title>
	<atom:link href="http://Wei23.Net/tag/mechanize/feed/" rel="self" type="application/rss+xml" />
	<link>http://Wei23.Net</link>
	<description>Successfully Reborned.</description>
	<lastBuildDate>Sun, 25 Apr 2010 08:45:22 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>zh</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>用Ruby做的Gmail新邮件提醒</title>
		<link>http://Wei23.Net/my-works/%e7%94%a8ruby%e5%81%9a%e7%9a%84gmail%e6%96%b0%e9%82%ae%e4%bb%b6%e6%8f%90%e9%86%92/</link>
		<comments>http://Wei23.Net/my-works/%e7%94%a8ruby%e5%81%9a%e7%9a%84gmail%e6%96%b0%e9%82%ae%e4%bb%b6%e6%8f%90%e9%86%92/#comments</comments>
		<pubDate>Mon, 02 Nov 2009 14:14:55 +0000</pubDate>
		<dc:creator>Wei</dc:creator>
				<category><![CDATA[My Works]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[Email]]></category>
		<category><![CDATA[Gmail]]></category>
		<category><![CDATA[Mechanize]]></category>
		<category><![CDATA[rfetion]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[接口]]></category>
		<category><![CDATA[短信]]></category>
		<category><![CDATA[飞信]]></category>

		<guid isPermaLink="false">http://Wei23.Net/?p=694</guid>
		<description><![CDATA[前段时间看到一个php的飞信模拟接口，可以实现免费短信发送，所以我就将其拿来做了个自己用的API。
前一段时间由于各种事务的繁忙，我几乎没有什么时间来查邮件，现在趁着这几天难得的假，我就写了一段用Ruby的Mechanize来获取Gmail或Google Apps Mail的代码。
这种模拟登录抓取网页内容的程序写出来就是比较乱，写注释以后自己都不一定记得，呵呵。
前期写模拟抓取网页和登录的时候还算顺利，但是抓取到的信息就是死活发不出去。（我用的是一个web接口，实际发短信部分是用php实现的，不是我写的，但我忘了是从哪里拿来的了，原作者看到请联系我。）
经过调查发现原来得到的信息不是纯的UTF-8编码，必须要转成纯UTF-8的形式才能顺利在手机端被识别。代码如下：

require 'rubygems'
require 'mechanize'
require 'iconv'
&#160;
def check&#40;address,email,passwd,phone,phonepw&#41;
  agent = WWW::Mechanize.new
  agent.user_agent = 'Opera Mini'
  agent.follow_meta_refresh = true
...]]></description>
			<content:encoded><![CDATA[<p>前段时间看到一个php的飞信模拟接口，可以实现免费短信发送，所以我就将其拿来做了个<a  href="http://x.rookee.net/" target="_blank">自己用的API</a>。</p>
<p>前一段时间由于各种事务的繁忙，我几乎没有什么时间来查邮件，现在趁着这几天难得的假，我就写了一段用Ruby的Mechanize来获取Gmail或Google Apps Mail的代码。</p>
<p>这种模拟登录抓取网页内容的程序写出来就是比较乱，写注释以后自己都不一定记得，呵呵。</p>
<p>前期写模拟抓取网页和登录的时候还算顺利，但是抓取到的信息就是死活发不出去。（我用的是一个web接口，实际发短信部分是用php实现的，不是我写的，但我忘了是从哪里拿来的了，原作者看到请联系我。）</p>
<p>经过调查发现原来得到的信息不是纯的UTF-8编码，必须要转成纯UTF-8的形式才能顺利在手机端被识别。代码如下：</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'mechanize'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'iconv'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> check<span style="color:#006600; font-weight:bold;">&#40;</span>address,email,passwd,phone,phonepw<span style="color:#006600; font-weight:bold;">&#41;</span>
  agent = <span style="color:#6666ff; font-weight:bold;">WWW::Mechanize</span>.<span style="color:#9900CC;">new</span>
  agent.<span style="color:#9900CC;">user_agent</span> = <span style="color:#996600;">'Opera Mini'</span>
  agent.<span style="color:#9900CC;">follow_meta_refresh</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
  page = agent.<span style="color:#9900CC;">get</span> address
  form = page.<span style="color:#9900CC;">forms</span>.<span style="color:#9900CC;">first</span>
  form.<span style="color:#9900CC;">Email</span> = email
  form.<span style="color:#9900CC;">Passwd</span> = passwd
  page = agent.<span style="color:#9900CC;">submit</span> form <span style="color:#008000; font-style:italic;">#login</span>
  tmp = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  page.<span style="color:#9900CC;">body</span>.<span style="color:#9900CC;">scan</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/&lt;</span>b<span style="color:#006600; font-weight:bold;">&gt;</span><span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&lt;/</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>str<span style="color:#006600; font-weight:bold;">|</span> tmp.<span style="color:#9900CC;">push</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Iconv</span>.<span style="color:#9900CC;">conv</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'UTF-8//IGNORE'</span>, <span style="color:#996600;">'UTF-8'</span>, str<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>0..<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#008000; font-style:italic;">#get new email and push them in to tmp after encode it in to pure UTF-8</span>
  tmp.<span style="color:#9900CC;">delete_at</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#delete extra shit</span>
  msg = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#CC0066; font-weight:bold;">exit</span> <span style="color:#9966CC; font-weight:bold;">if</span> tmp.<span style="color:#9900CC;">size</span> ==<span style="color:#006666;">0</span> <span style="color:#008000; font-style:italic;">#exit if theres none</span>
  <span style="color:#9966CC; font-weight:bold;">for</span> i <span style="color:#9966CC; font-weight:bold;">in</span> 0..<span style="color:#9900CC;">tmp</span>.<span style="color:#9900CC;">size</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span> <span style="color:#008000; font-style:italic;">#form messages</span>
    msg.<span style="color:#9900CC;">push</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{tmp[i*2][0..-2]}--#{tmp[i*2+1]}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  page.<span style="color:#9900CC;">links</span><span style="color:#006600; font-weight:bold;">&#91;</span>0..<span style="color:#006600; font-weight:bold;">&#40;</span>msg.<span style="color:#9900CC;">size</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>l<span style="color:#006600; font-weight:bold;">|</span> l.<span style="color:#9900CC;">click</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;">#click those new links to make it as read</span>
  <span style="color:#9966CC; font-weight:bold;">for</span> t <span style="color:#9966CC; font-weight:bold;">in</span> msg <span style="color:#008000; font-style:italic;">#send each</span>
    page = agent.<span style="color:#9900CC;">get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'http://x.rookee.net'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    form = page.<span style="color:#9900CC;">forms</span>.<span style="color:#9900CC;">first</span>
    form.<span style="color:#9900CC;">pn</span> = phone
    form.<span style="color:#9900CC;">pw</span> = phonepw
    form.<span style="color:#9900CC;">to</span> = phone
    form.<span style="color:#9900CC;">msg</span> = t
    page = agent.<span style="color:#9900CC;">submit</span> form
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>用法实例：</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">check<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'http://mail.google.com/'</span>,<span style="color:#996600;">'你的邮件地址'</span>,<span style="color:#996600;">'你的密码'</span>,<span style="color:#996600;">'移动手机号'</span>,<span style="color:#996600;">'飞信密码'</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>PS:其实一开始我准备用<a  href="http://github.com/flyerhzm/rfetion" target="_blank">rfetion</a>来做（<a  href="http://github.com/flyerhzm/rfetion" target="_blank">rfetion</a>是一个Ruby的飞信接口，很不错），但是不知道为什么收到的短信总是不全。所以就改用了php的接口。</p>
]]></content:encoded>
			<wfw:commentRss>http://Wei23.Net/my-works/%e7%94%a8ruby%e5%81%9a%e7%9a%84gmail%e6%96%b0%e9%82%ae%e4%bb%b6%e6%8f%90%e9%86%92/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
