blog/content/post/2008-07-12-00000965.md

121 lines
10 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: hatena-modeの投稿ログをあさってはてなのリストを作るスクリプト
author: kazu634
date: 2008-07-12
url: /2008/07/12/_1039/
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:4135;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- Perl
- Programming
---
<div class="section">
<blockquote>
<ul>
<li>
「perl」に関連する最近のエントリ <ul>
<li>
<a href="http://d.hatena.ne.jp/sirocco634/20080702" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20080702', '2008-07-02 &#8211; 武蔵の日記');" target="_blank">2008-07-02 &#8211; 武蔵の日記</a>
</li>
<li>
<a href="http://d.hatena.ne.jp/sirocco634/20080529" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20080529', '2008-05-29 &#8211; 武蔵の日記');" target="_blank">2008-05-29 &#8211; 武蔵の日記</a>
</li>
<li>
<a href="http://d.hatena.ne.jp/sirocco634/20080522" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20080522', '2008-05-22 &#8211; 武蔵の日記');" target="_blank">2008-05-22 &#8211; 武蔵の日記</a>
</li>
</ul>
</li>
</ul>
</blockquote>
<p>
こんなのをつくってみたくて、Perlでつくりました。
</p>
<pre class="syntax-highlight">
<span class="synPreProc">#!/usr/bin/perl</span>
<span class="synComment"># === use ===</span>
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synComment"># 引数が指定されていなければ終了</span>
<span class="synStatement">exit</span> <span class="synStatement">if</span> (<span class="synIdentifier">$#ARGV</span> == <span class="synConstant">-1</span>);
<span class="synComment"># 検索するディレクトリの指定(絶対パス)</span>
<span class="synStatement">my</span> <span class="synIdentifier">$init_dir</span> = <span class="synConstant">&#34;/Users/simoom634/Documents/hatena/&#34;</span>;
<span class="synComment"># 表示させる個数</span>
<span class="synStatement">my</span> <span class="synIdentifier">$num</span> = <span class="synConstant">3</span>;
<span class="synComment"># ファイル名を格納する配列</span>
<span class="synStatement">my</span> <span class="synIdentifier">@filenames</span>;
<span class="synComment"># 出力用文字列を格納する配列</span>
<span class="synStatement">my</span> <span class="synIdentifier">@outputs</span>;
<span class="synComment"># 検索する文字列格納用変数(正規表現可能)</span>
<span class="synStatement">my</span> <span class="synIdentifier">$expr</span> = <span class="synStatement">shift</span>();
<span class="synComment"># サブルーチンの呼び出し</span>
<span class="synComment"># --&#62;ファイル名の列挙</span>
<span class="synIdentifier">&#38;list</span>(<span class="synIdentifier">$init_dir</span>);
<span class="synComment"># サブルーチンの呼び出し</span>
<span class="synComment"># --&#62;整形して、@outputsに格納</span>
<span class="synIdentifier">&#38;prnt_titles</span>(<span class="synIdentifier">@filenames</span>);
<span class="synStatement">if</span> ((<span class="synIdentifier">$num</span> + <span class="synConstant">1</span>) &#60; <span class="synIdentifier">@outputs</span>) {
<span class="synStatement">print</span> (<span class="synIdentifier">$outputs</span>[<span class="synConstant"></span>]);
<span class="synStatement">for</span> (<span class="synStatement">my</span> <span class="synIdentifier">$i</span> = <span class="synConstant"></span>; <span class="synIdentifier">$i</span> &#60; <span class="synIdentifier">$num</span>; <span class="synIdentifier">$i</span>++) {
<span class="synStatement">print</span>(<span class="synStatement">pop</span>(<span class="synIdentifier">@outputs</span>));
}
} <span class="synStatement">else</span> {
<span class="synStatement">print</span> <span class="synIdentifier">@outputs</span>;
}
<span class="synComment"># 終了</span>
<span class="synStatement">exit</span>;
<span class="synComment"># ====================</span>
<span class="synComment"># === サブルーチン ===</span>
<span class="synComment"># ====================</span>
<span class="synComment"># ---&#62;ファイル名列挙</span>
<span class="synComment">#   引数として与えられたディレクトリの中から</span>
<span class="synComment">#   「\d+」に該当するものだけを列挙して</span>
<span class="synComment">#   @filenameに格納する</span>
<span class="synStatement">sub</span><span class="synIdentifier"> list </span>{
<span class="synComment"># 引数を$dirnameに代入</span>
<span class="synStatement">my</span> <span class="synIdentifier">$dirname</span> = <span class="synStatement">shift</span>;
<span class="synComment"># ディレクトリを開いて、一つ一つ処理</span>
<span class="synStatement">opendir</span>(<span class="synIdentifier">DIR</span>, <span class="synIdentifier">$dirname</span>) <span class="synStatement">or</span> <span class="synStatement">die</span> <span class="synConstant">&#34;</span><span class="synIdentifier">$dirname</span><span class="synConstant">: </span><span class="synIdentifier">$!</span><span class="synConstant">&#34;</span>;
<span class="synStatement">foreach</span> <span class="synStatement">my</span> <span class="synIdentifier">$entry</span> (<span class="synStatement">readdir</span>(<span class="synIdentifier">DIR</span>)) {
<span class="synComment"># 隠しファイル・ディレクトリだったらパス</span>
<span class="synStatement">next</span> <span class="synStatement">if</span> (<span class="synIdentifier">$entry</span> =~<span class="synStatement"> /</span><span class="synConstant">[</span><span class="synSpecial">\.</span><span class="synConstant">]</span><span class="synSpecial">+</span><span class="synStatement">/</span>);
<span class="synComment"># バックアップや設定用ファイルならパス</span>
<span class="synStatement">next</span> <span class="synStatement">if</span> (<span class="synIdentifier">$entry</span> =~<span class="synStatement"> /</span><span class="synSpecial">[0-9]+\~</span><span class="synStatement">/</span>);
<span class="synStatement">next</span> <span class="synStatement">if</span> (<span class="synIdentifier">$entry</span> =~<span class="synStatement"> /</span><span class="synSpecial">.*\@</span><span class="synConstant">hatena</span><span class="synStatement">/</span>);
<span class="synStatement">push</span> <span class="synIdentifier">@filenames</span>, <span class="synIdentifier">$entry</span>;
}
<span class="synStatement">closedir</span>(<span class="synIdentifier">DIR</span>);
}
<span class="synComment"># ---&#62;整形して表示</span>
<span class="synComment">#   はてなのリスト形式で検索結果を表示する。</span>
<span class="synComment">#   引数にはファイル名を格納した配列を指定する。</span>
<span class="synStatement">sub</span><span class="synIdentifier"> prnt_titles </span>{
<span class="synStatement">push</span> <span class="synIdentifier">@outputs</span>, <span class="synConstant">&#34;- 「</span><span class="synIdentifier">$expr」に関連する最近のエントリ</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>;
<span class="synStatement">while</span> (<span class="synIdentifier">@_</span>) {
<span class="synStatement">my</span> <span class="synIdentifier">$part_filename</span> = <span class="synStatement">shift</span>;
<span class="synComment"># パスの区切り文字を指定</span>
<span class="synStatement">my</span> <span class="synIdentifier">$delim</span> = <span class="synConstant">'/'</span>;
<span class="synComment"># $dirnameの最後が「/」or「\」だったら、区切り文字をnullに</span>
<span class="synStatement">if</span> (<span class="synIdentifier">$init_dir</span> =~<span class="synStatement"> /</span><span class="synConstant">[</span><span class="synSpecial">\/\\</span><span class="synConstant">]$</span><span class="synStatement">/</span>) {
<span class="synIdentifier">$delim</span> = <span class="synConstant">''</span>;
}
<span class="synComment"># 表示</span>
<span class="synStatement">my</span> <span class="synIdentifier">$full_filename</span> = <span class="synConstant">&#34;</span><span class="synIdentifier">$init_dir$delim$part_filename</span><span class="synConstant">&#34;</span>;
<span class="synStatement">open</span> (<span class="synIdentifier">FILE</span>, <span class="synIdentifier">$full_filename</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>;
<span class="synStatement">while</span> (<span class="synIdentifier">&#60;FILE&#62;</span>) {
<span class="synStatement">if</span> (<span class="synIdentifier">$_</span> =~<span class="synStatement"> /</span><span class="synIdentifier">$expr</span><span class="synStatement">/i</span>){
<span class="synStatement">my</span> <span class="synIdentifier">$tmp</span> = <span class="synConstant">&#34;-- &#34;</span>;
<span class="synIdentifier">$tmp</span> = <span class="synIdentifier">$tmp</span> . <span class="synConstant">&#34;[http://d.hatena.ne.jp/sirocco634/&#34;</span>;
<span class="synIdentifier">$tmp</span> = <span class="synIdentifier">$tmp</span> . <span class="synIdentifier">$part_filename</span>;
<span class="synIdentifier">$tmp</span> = <span class="synIdentifier">$tmp</span> . <span class="synConstant">&#34;:title]</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>;
<span class="synStatement">push</span> <span class="synIdentifier">@outputs</span>, <span class="synIdentifier">$tmp</span>;
<span class="synStatement">last</span>;
}
}
<span class="synStatement">close</span> (<span class="synIdentifier">FILE</span>);
}
}
</pre>
</div>