blog/content/post/2010/01/02/2010-01-02-00001301.md

120 lines
13 KiB
Markdown
Raw Normal View History

2019-03-31 11:00:21 +00:00
---
title: Twitterのfavoriteをhowmファイルにするスクリプト
author: kazu634
date: 2010-01-02
url: /2010/01/02/_1436/
wordtwit_post_info:
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:5035;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- Emacs
- Perl
---
<div class="section">
<p>
Twitter の favorite を howm ファイルにするスクリプトを作成しました。
</p>
<p>
設定を変更したい場合には、ソース中の下記を変更してください:
</p>
<pre class="syntax-highlight">
<span class="synComment"># howm のデータを格納するディレクトリ</span>
<span class="synStatement">my</span> <span class="synIdentifier">$howm_direcotory</span> = <span class="synConstant">&#34;/Users/kazu634/Documents/howm&#34;</span>;
<span class="synComment"># favorite を取得したいユーザー名</span>
<span class="synStatement">my</span> <span class="synIdentifier">$user</span> = <span class="synConstant">&#34;kazu634&#34;</span>;
</pre>
<h4>
ソース
</h4>
<pre class="syntax-highlight">
<span class="synComment"># === Libraries ===</span>
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synStatement">use </span>Perl6::Say;
<span class="synStatement">use utf8</span>;
<span class="synStatement">use </span>Encode;
<span class="synStatement">use </span>WebService::Simple;
<span class="synStatement">use </span>XML::Simple;
<span class="synStatement">use </span>YAML::Syck;
<span class="synStatement">use </span>Config::Auto;
<span class="synComment"># === Initial Setting ===</span>
<span class="synStatement">my</span> <span class="synIdentifier">$howm_direcotory</span> = <span class="synConstant">&#34;/Users/kazu634/Documents/howm&#34;</span>;
<span class="synStatement">my</span> <span class="synIdentifier">$user</span> = <span class="synConstant">&#34;kazu634&#34;</span>;
<span class="synStatement">my</span> <span class="synIdentifier">$page</span> = <span class="synConstant">1</span>; <span class="synComment"># page counter</span>
<span class="synStatement">my</span> <span class="synIdentifier">$new_id</span> = <span class="synConstant"></span>; <span class="synComment"># the newest id when obtaining the favorites.</span>
<span class="synStatement">my</span> <span class="synIdentifier">$howm_file</span> = get_howm_filename(); <span class="synComment"># filename for the howm file</span>
<span class="synStatement">my</span> <span class="synIdentifier">$howm_header</span> = get_howm_header(); <span class="synComment"># header string for the howm file</span>
<span class="synStatement">my</span> <span class="synIdentifier">$counter</span> = <span class="synConstant"></span>; <span class="synComment"># counter for how many times this script outputs the favorites.</span>
<span class="synComment"># === Until what ID number do I get from my favorites ===</span>
<span class="synStatement">my</span> <span class="synIdentifier">$recent_id</span> = <span class="synConstant"></span>; <span class="synComment"># variant for storing the recent-obtained ID number</span>
<span class="synComment"># Read the ID number from the configuration file, .get_favorite,</span>
<span class="synComment"># if it exists.</span>
<span class="synStatement">if</span> ( <span class="synStatement">-f</span> <span class="synConstant">&#34;.get_favorite&#34;</span> ) {
<span class="synStatement">my</span> <span class="synIdentifier">$config</span> = Config::Auto::parse(<span class="synConstant">&#34;.get_favorite&#34;</span>);
<span class="synIdentifier">$recent_id</span> = <span class="synIdentifier">$config</span>-&#62;{recent_id};
}
<span class="synComment"># === Get the favorites ===</span>
<span class="synStatement">my</span> <span class="synIdentifier">$twitter</span> =
WebService::Simple-&#62;<span class="synStatement">new</span>( <span class="synConstant">base_url </span>=&#62; <span class="synConstant">&#34;http://twitter.com/favorites.xml&#34;</span>, );
LOOP: <span class="synStatement">while</span> (<span class="synConstant">1</span>) {
<span class="synStatement">my</span> <span class="synIdentifier">$response</span> = <span class="synIdentifier">$twitter</span>-&#62;get( { <span class="synConstant">id </span>=&#62; <span class="synIdentifier">$user</span>, <span class="synConstant">page </span>=&#62; <span class="synIdentifier">$page</span> } );
<span class="synComment"># If the page you request is empty, get out of the while-loop.</span>
<span class="synStatement">last</span> <span class="synStatement">unless</span> ( <span class="synStatement">defined</span> %{ XMLin( <span class="synIdentifier">$response</span>-&#62;content )-&#62;{status} } );
<span class="synStatement">my</span> <span class="synIdentifier">@ids</span> = <span class="synStatement">reverse</span> <span class="synStatement">sort</span> <span class="synStatement">keys</span> %{ XMLin( <span class="synIdentifier">$response</span>-&#62;content )-&#62;{status} };
<span class="synComment"># Store the newest id</span>
<span class="synComment"># (after obtaining the favorites,</span>
<span class="synComment"># this script outputs this id to the .get_favorite file).</span>
<span class="synIdentifier">$new_id</span> = <span class="synIdentifier">$ids</span>[<span class="synConstant"></span>] <span class="synStatement">if</span> ( <span class="synIdentifier">$page</span> == <span class="synConstant">1</span> );
<span class="synStatement">foreach</span> <span class="synStatement">my</span> <span class="synIdentifier">$id</span> (<span class="synIdentifier">@ids</span>) {
<span class="synComment"># If the favorite just getting is already obtained,</span>
<span class="synComment"># get out of the while-loop.</span>
<span class="synStatement">last</span> LOOP <span class="synStatement">if</span> ( <span class="synIdentifier">$recent_id</span> == <span class="synIdentifier">$id</span> );
<span class="synStatement">if</span> ( <span class="synIdentifier">$recent_id</span> &#60; <span class="synIdentifier">$id</span> ) {
<span class="synStatement">my</span> <span class="synIdentifier">$text</span> =
encode( <span class="synConstant">&#34;utf8&#34;</span>,
XMLin( <span class="synIdentifier">$response</span>-&#62;content )-&#62;{status}-&#62;{<span class="synIdentifier">$id</span>}-&#62;{text} );
<span class="synStatement">open</span>( <span class="synIdentifier">HOWM</span>, <span class="synConstant">&#34;&#62;&#62; </span><span class="synIdentifier">$howm_file</span><span class="synConstant">&#34;</span> ) <span class="synStatement">or</span> <span class="synStatement">die</span> <span class="synConstant">&#34;</span><span class="synIdentifier">$!</span><span class="synConstant">&#34;</span>;
say HOWM <span class="synIdentifier">$howm_header</span> <span class="synStatement">if</span> ( <span class="synIdentifier">$counter</span> == <span class="synConstant"></span> );
say HOWM <span class="synConstant">&#34;[memo][twitter] </span><span class="synIdentifier">$text</span><span class="synConstant">&#34;</span>;
<span class="synStatement">close</span>(<span class="synIdentifier">HOWM</span>);
<span class="synIdentifier">$counter</span>++;
}
}
<span class="synComment"># Increment the page number.</span>
<span class="synIdentifier">$page</span>++;
}
<span class="synComment"># === update the recent_id ===</span>
<span class="synStatement">open</span>( <span class="synIdentifier">FILE</span>, <span class="synConstant">'&#62; .get_favorite'</span> ) <span class="synStatement">or</span> <span class="synStatement">die</span> <span class="synConstant">&#34;</span><span class="synIdentifier">$!</span><span class="synConstant">&#34;</span>;
say FILE <span class="synConstant">&#34;recent_id=</span><span class="synIdentifier">$new_id</span><span class="synConstant">&#34;</span>;
<span class="synStatement">close</span>(<span class="synIdentifier">FILE</span>);
<span class="synComment"># === Sub-routines ===</span>
<span class="synStatement">sub</span><span class="synIdentifier"> get_howm_filename </span>{
<span class="synStatement">my</span> ( <span class="synIdentifier">$sec</span>, <span class="synIdentifier">$min</span>, <span class="synIdentifier">$hour</span>, <span class="synIdentifier">$mday</span>, <span class="synIdentifier">$mon</span>, <span class="synIdentifier">$year</span> ) = <span class="synStatement">localtime</span>(<span class="synStatement">time</span>);
<span class="synIdentifier">$year</span> += <span class="synConstant">1900</span>;
<span class="synIdentifier">$mon</span> += <span class="synConstant">1</span>;
<span class="synIdentifier">$mon</span> = <span class="synConstant">&#34;0</span><span class="synIdentifier">$mon</span><span class="synConstant">&#34;</span> <span class="synStatement">if</span> ( <span class="synIdentifier">$mon</span> &#60; <span class="synConstant">10</span> );
<span class="synIdentifier">$mday</span> = <span class="synConstant">&#34;0</span><span class="synIdentifier">$mday</span><span class="synConstant">&#34;</span> <span class="synStatement">if</span> ( <span class="synIdentifier">$mday</span> &#60; <span class="synConstant">10</span> );
<span class="synIdentifier">$hour</span> = <span class="synConstant">&#34;0</span><span class="synIdentifier">$hour</span><span class="synConstant">&#34;</span> <span class="synStatement">if</span> ( <span class="synIdentifier">$hour</span> &#60; <span class="synConstant">10</span> );
<span class="synIdentifier">$min</span> = <span class="synConstant">&#34;0</span><span class="synIdentifier">$min</span><span class="synConstant">&#34;</span> <span class="synStatement">if</span> ( <span class="synIdentifier">$min</span> &#60; <span class="synConstant">10</span> );
<span class="synIdentifier">$sec</span> = <span class="synConstant">&#34;0</span><span class="synIdentifier">$sec</span><span class="synConstant">&#34;</span> <span class="synStatement">if</span> ( <span class="synIdentifier">$sec</span> &#60; <span class="synConstant">10</span> );
<span class="synStatement">return</span> encode( <span class="synConstant">&#34;utf8&#34;</span>,
<span class="synConstant">&#34;</span><span class="synIdentifier">$howm_direcotory</span><span class="synConstant">/</span><span class="synIdentifier">$year</span><span class="synConstant">/</span><span class="synIdentifier">$mon</span><span class="synConstant">/</span><span class="synIdentifier">$year</span><span class="synConstant">-</span><span class="synIdentifier">$mon</span><span class="synConstant">-</span><span class="synIdentifier">$mday</span><span class="synConstant">-</span><span class="synIdentifier">$hour$min$sec</span><span class="synConstant">.txt&#34;</span> );
}
<span class="synStatement">sub</span><span class="synIdentifier"> get_howm_header </span>{
<span class="synStatement">my</span> ( <span class="synIdentifier">$sec</span>, <span class="synIdentifier">$min</span>, <span class="synIdentifier">$hour</span>, <span class="synIdentifier">$mday</span>, <span class="synIdentifier">$mon</span>, <span class="synIdentifier">$year</span> ) = <span class="synStatement">localtime</span>(<span class="synStatement">time</span>);
<span class="synIdentifier">$year</span> += <span class="synConstant">1900</span>;
<span class="synIdentifier">$mon</span> += <span class="synConstant">1</span>;
<span class="synIdentifier">$mon</span> = <span class="synConstant">&#34;0</span><span class="synIdentifier">$mon</span><span class="synConstant">&#34;</span> <span class="synStatement">if</span> ( <span class="synIdentifier">$mon</span> &#60; <span class="synConstant">10</span> );
<span class="synIdentifier">$mday</span> = <span class="synConstant">&#34;0</span><span class="synIdentifier">$mday</span><span class="synConstant">&#34;</span> <span class="synStatement">if</span> ( <span class="synIdentifier">$mday</span> &#60; <span class="synConstant">10</span> );
<span class="synIdentifier">$hour</span> = <span class="synConstant">&#34;0</span><span class="synIdentifier">$hour</span><span class="synConstant">&#34;</span> <span class="synStatement">if</span> ( <span class="synIdentifier">$hour</span> &#60; <span class="synConstant">10</span> );
<span class="synIdentifier">$min</span> = <span class="synConstant">&#34;0</span><span class="synIdentifier">$min</span><span class="synConstant">&#34;</span> <span class="synStatement">if</span> ( <span class="synIdentifier">$min</span> &#60; <span class="synConstant">10</span> );
<span class="synIdentifier">$sec</span> = <span class="synConstant">&#34;0</span><span class="synIdentifier">$sec</span><span class="synConstant">&#34;</span> <span class="synStatement">if</span> ( <span class="synIdentifier">$sec</span> &#60; <span class="synConstant">10</span> );
<span class="synStatement">return</span> encode( <span class="synConstant">&#34;utf8&#34;</span>,
<span class="synConstant">&#34;= favorites from twitter.</span><span class="synSpecial">\n\[</span><span class="synIdentifier">$year</span><span class="synConstant">-</span><span class="synIdentifier">$mon</span><span class="synConstant">-</span><span class="synIdentifier">$mday</span><span class="synConstant"> </span><span class="synIdentifier">$hour</span><span class="synConstant">:</span><span class="synIdentifier">$min</span><span class="synSpecial">\]\n</span><span class="synConstant">&#34;</span> );
}
</pre>
</div>