2010/03/24


スクリプトを書いたりしてる人にとってはkarmaなんかの評価値は励みになったりもします。
#!/usr/bin/env perl
use strict;
use warnings;
use URI;
use LWP::UserAgent;
use Web::Scraper;

my $user_id = shift || '103';

local $Web::Scraper::UserAgent = LWP::UserAgent->new;
$Web::Scraper::UserAgent->env_proxy;

my $account_url = "http://www.vim.org/account/profile.php?user_id=$user_id";
my $s1      = scraper {
  process '//td/a[contains(@href,"/scripts/script")]', 'karmas[]' => sub {
    my $e1     = shift;
    my $script_url = URI->new_abs( $e1->attr('href'), $account_url );
    my $s2     = scraper {
      process '//title',                         'name'  => 'TEXT';
      process '//td[contains(text(),"Rating")]', 'karma' => sub {
        my $e2 = shift;
        if ( $e2->as_text =~ /Rating ([^\s]*), Downloaded by ([^\s]*)/ )
        {
          return { rating => $1, downloaded => $2 };
        }
      };
    };
    my $staff = $s2->scrape($script_url);
    $staff->{url} = $script_url;
    $staff;
  };
  result 'karmas';
};
my @r = @{ $s1->scrape( URI->new($account_url) ) };

use YAML::Syck;
warn Dump @r;

# vim:set et:

---
karma: 
  downloaded: 25932
  rating: 1355/552
name: "calendar.vim - Calendar : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=52
---
karma: 
  downloaded: 783
  rating: 9/7
name: "which.vim - This is a unix like \"Which\" function. : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=139
---
karma: 
  downloaded: 1054
  rating: 9/10
name: "XpMenu - Make vim use WinXP style menu. : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=928
---
karma: 
  downloaded: 1884
  rating: 13/13
name: "VS like Class Completion - This script can complete member of cpp or java like Visual Studio. : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=675
---
karma: 
  downloaded: 943
  rating: 16/9
name: "ftplugin for Calendar - This is a ftplugin for Calenar. : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=683
---
karma: 
  downloaded: 1871
  rating: 92/37
name: "VimTweak - The tweaking dll for GVim.exe. : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=687
---
karma: 
  downloaded: 980
  rating: -1/1
name: "VimSpeak - Speak selected text with MS Agent : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=692
---
karma: 
  downloaded: 960
  rating: 11/4
name: "FTP Completion - complete files in command line for ftp. : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=963
---
karma: 
  downloaded: 183
  rating: 2/2
name: "Pit Configuration - pit configuration library for vim : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=2404
---
karma: 
  downloaded: 1282
  rating: 81/24
name: "Gist.vim - vimscript for gist : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=2423
---
karma: 
  downloaded: 805
  rating: 82/29
name: "GoogleReader.vim - vimscript for googlereader : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=2678
---
karma: 
  downloaded: 252
  rating: 20/5
name: "FastLadder.vim - vimscript for fastladder : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=2683
---
karma: 
  downloaded: 78
  rating: 0/0
name: "GoogleSuggest Complete - complete function using google suggest API. perhaps, you should input japanese w : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=2948
---
karma: 
  downloaded: 833
  rating: 101/29
name: "ZenCoding.vim - vim plugins for HTML and CSS hi-speed coding. : vim online"
url: !!perl/scalar:URI::http http://www.vim.org/scripts/script.php?script_id=2981
ダメダメなスクリプトとか、もう消した方がいいかな...とか思った。
Posted at by



2010/03/02


タイトルは釣りです。
perlでは一般的にperldocを使って調べ物をする事が殆どですが
あれー、あのモジュールなんだっけ...IO:: 忘れた。
とか
IO:: の下辺りに似たモジュールあったよなー
なんて事が結構あったりするのですが(私だけかも)、今日もSys::SendfileのAuthorから「チミが送ってくれたpatchに対して僕が書いたtestがWindowsで通らないんだけど、何か分かるかい?」とメールが来てて
それ、以前ワテがp5pに送ったpatchの件ちゃいますやろか、確かIO:: ...えーっと...
ってなってしまった訳です。
なんか良くある事なので解決出来ないかなぁと思っていた所、見つけてしまいました。perldoc-completeを。
ap's perldoc-complete at master - GitHub

A bash completion helper for perldoc

http://github.com/ap/perldoc-complete
パスの通った所に perldoc-complete を置いて complete -C perldoc-complete -o nospace -o default perldoc
を .bashrc に書いておくと...
# perldoc IO:: ここでタブ押す
AIO            File           Pipe           Socket         Wrap
AtomicFile     Handle         Poll           Socket::       WrapTie
Capture        Handle::       Pty            String         Zlib
Capture::      InnerFile      Scalar         Stringy        
CaptureOutput  Lambda         ScalarArray    Tty            
Compress::     Lambda::       Seekable       Tty::          
Dir            Lines          Select         Uncompress::  
# perldoc IO::
きたーーー!
常用確定しました。
Posted at by



2010/02/17


TwitterのBasic認証APIは6月で廃止される予定なのですが、OAuthという認証方法はブラウザを起動してユーザに認証して貰わなければなりません。一見flickrアプリケーションの様な認証方法を想定しますが、OAuthはflickr認証の様にサーバから貰ったトークンをブラウザから渡して認証させる様な物ではありません。
今回OAuthの問題を解決すべくOAuthを拡張した認証方式であるxAuthが取り入れられました。
詳しくはAPIドキュメントか以下のサイトが分かりやすいかと思います。
s-take Blog.: Twitterによる簡易版OAuth: "xAuth"

従来のOAuth認証ではまずアプリケーション(OAuthコンシューマ)がTwitterに接続してRequest Tokenを取得し、認証画面を開いてRequest Tokenを承認させ、承認されたRequest Tokenを使ってAccess TokenとToken Secretを取得することによって各APIにアクセスできるようになります。しかしこれはアプリケーション側の実装が複雑になる上、デスクトップアプリケーションの場合はわざわざWebブラウザへ切り替えなければならず(ブラウザを内包するものもありますが)、ユーザにとっても面倒なものです。

http://s-take.blogspot.com/2010/02/twitteroauth-xauth.html
the.hackerConundrum: Sneak peek at Twitter's browserless OAuth credentials exchange method

Over the past couple of months the Twitter API Google Group has been overflowing with more and more disgruntled developers complaining about lack of bug fixes, slow rollout of promised features, no mobile interface for OAuth, etc. (The list goes on and on) Well I'm happy to say Twitter appears to be almost done with one much requested feature: browserless OAuth credentials exchange. It was hinted that Seesmic Look was using said exchange so today I took a peek at how Look worked behind the scenes.

http://the.hackerconundrum.com/2010/02/sneak-peek-at-twitters-browserless.html
実装だと
Maraigue風。:[Ruby][Twitter] OAuthのアクセストークンを、ブラウザなしで、Twitterのユーザ名およびパスワードのみを用いて取得する(通称:xAuth)ためのRubyのコード
http://blog.livedoor.jp/maraigue/archives/1109122.html
とか
pastebin - 誰か - post number 1796209
http://ja.pastebin.ca/1796209
あとOAuthな話ですが
あまやどり: OAuthで認証してTwitterでつぶやいてみた
http://petitbanca.blogspot.com/2009/11/oauthtwitter.html
あたりが参考になります。今日はpythonを使ってxAuthするサンプルを書いてみました。pythonにも元々oauthライブラリはあるのですが、今回は分かりやすく使わず書いてみました。
以下ソース。
from pit import Pit
from random import getrandbits
from time import time
import hmac, hashlib
import sys
import urllib
import urllib2
import urlparse

consumer_key = 'YOUR-CONSUMER-KEY'
consumer_secret = 'YOUR-CONSUMER-SECRET'
user = Pit.get('twitter.com', {'require' : {
  'username' : 'your username in twitter.com',
  'password' : 'your password in twitter.com',
}})
message = sys.argv[1]
access_url = 'https://api.twitter.com/oauth/access_token'
post_url = 'http://twitter.com/statuses/update.json'

# build parameter to get access token
params = {
  'oauth_consumer_key' : consumer_key,
  'oauth_signature_method' : 'HMAC-SHA1',
  'oauth_timestamp' : str(int(time())),
  'oauth_nonce' : str(getrandbits(64)),
  'oauth_version' : '1.0',
  'x_auth_mode' : 'client_auth',
  'x_auth_username' : user['username'],
  'x_auth_password' : user['password'],
}
params['oauth_signature'] = hmac.new(
  '%s&%s' % (consumer_secret, ''),
  '&'.join([
      'POST',
      urllib.quote(access_url, ''),
      urllib.quote('&'.join(['%s=%s' % (x, params[x])
          for x in sorted(params)]), '')
  ]),
  hashlib.sha1).digest().encode('base64').strip()

# get access token
req = urllib2.Request(access_url, data = urllib.urlencode(params))
res = urllib2.urlopen(req)
token = urlparse.parse_qs(res.read())
token_key = token['oauth_token'][0]
token_secret = token['oauth_token_secret'][0]

# build parameters to post
params = {
  'oauth_consumer_key' : consumer_key,
  'oauth_signature_method' : 'HMAC-SHA1',
  'oauth_timestamp' : str(int(time())),
  'oauth_nonce' : str(getrandbits(64)),
  'oauth_version' : '1.0',
  'oauth_token' : token_key,
}
params['status'] = urllib.quote(message, '')
params['oauth_signature'] = hmac.new(
  '%s&%s' % (consumer_secret, token_secret),
  '&'.join([
      'POST',
      urllib.quote(post_url, ''),
      urllib.quote('&'.join(['%s=%s' % (x, params[x])
          for x in sorted(params)]), '')
  ]),
  hashlib.sha1).digest().encode('base64').strip()
del params['status']

# post with oauth token
req = urllib2.Request(post_url, data = urllib.urlencode(params))
req.add_data(urllib.urlencode({'status' : message}))
req.add_header('Authorization', 'OAuth %s' % ', '.join(
  ['%s="%s"' % (x, urllib.quote(params[x], '')) for x in params]))

# done!
print urllib2.urlopen(req).read()
生の処理で書いてあるので、oauthライブラリに依存させたくない様な移植には参考になるかもしれません。
ところで今回GtkTwitterというC言語で書いたTwitterクライアントのBasic認証を止めようと思っていてこの件を調べ始めたのですが, どうやらTwitterに登録するOAuthアプリケーションには「Twitter」という文言を使ってはいけない事が今日分かりました。まぁGtkTwitterはクライアントアプリが名前登録出来た頃に書いた物なので、あの頃はOKだったのかも知れません。
しかしまぁ...どうせぃっちゅうねん!

どうしましょ。Gtkほにゃらら...何がいいやろ。困った。
Posted at by