Archive@2008/02

Pythonではてなダイアリーキーワード自動リンクAPIを試してみた

はてなダイアリーキーワード自動リンクAPI
今作成中のブログに使えないか考えてたので、
試してみた。

サンプルは、
Perlしかなかったので、
Pythonで書いてみた。

#まずxmlrpclibをインポートします。
>>> import xmlrpclib
#次にurlを指定します。
>>> server = xmlrpclib.ServerProxy('http://d.hatena.ne.jp/xmlrpc')
#次は実際リンクを貼りたい内容と各属性を指定しています。
>>> result = server.hatena.setKeywordLink({'body': 'はてなでスケボーやレコードなどを検査するとどんなレスポンスが返ってくるのだろう?', 'score': '20', 'a_target': '_blank', 'a_class': 'keyword'})
#最後にレスポンスを表示してみる
>>> print result<
<a>はてな</a><a>スケボー</a><a>レコード</a>などを検査するとどんなレスポンスが返ってくるのだろう?
こんな感じで返ってきたっす。

指定するパラメーターなどははてなに記載されていました。
uri : http://d.hatena.ne.jp/xmlrpc
encoding : utf8
methodName : hatena.setKeywordLink
parameters : 以下を参照
body(string): キーワードを抽出するテキストを指定します。
score(int): 0〜50。キーワードスコアのしきい値を指定します。指定された値よりもスコアが大きいキーワードのみが抽出されます。省略可。
cname(array): キーワードのカテゴリーを指定します。指定があった場合、「一般」と指定されたカテゴリーのキーワードが抽出されます。指定が無かった場合は、全カテゴリーとなります。book,music,movie,web,elec,animal,anime,food,sports,game,comic,hatena,clubが指定可能です。省略可。
a_target(string): アンカー(a)タグのtarget属性値を指定します。省略可。例:_blank
a_class(string): アンカー(a)タグのclass属性値を指定します。省略可。例:keyword

今回指定したのは、

・body : 'はてなでスケボーやレコードなどを検査するとどんなレスポンスが返ってくるのだろう?
・score : 20
・a_target : _blank
・a_class : keyword

です。
これをブログに組み込んだら面白いかな?
と考え中です。

Posted at: 
2008/02/09 00:15:39
0 Comments
0 TrackBacks
Tags: 
Python
Trackback: 
http://kishi-r.com/2008/02/09/pythonapi/trackback/

OLIVE OIL - SPRING BREAK [2CD+DVD]予約っす

とりあえず予約しておいた。
ついでに

OLIVE OIL - PIANITY [12inch"]

も予約しておいた。
最近買ってなかったので、
届くのが待ち通しい。

いつも日本人関連はwenodが素敵。
恵比寿にあるのに行った事がないけども・・・

http://www.wenod.com/

Posted at: 
2008/02/08 02:20:42
0 Comments
0 TrackBacks
Tags: 
Music
Trackback: 
http://kishi-r.com/2008/02/08/olive_oil_spring_break_2cddvd/trackback/

みんな転け過ぎw

まだ道が凍っていて、
朝からみんな転けてるなw

そんなに急ぐからだよ。
俺なんかすげぇ〜慎重にチャリを漕いでる。
だって久々のチャリだもんw

特に坂道で転けてる人なんかは、
すげぇ〜痛そうだった。
可哀想に。。。

まぁ〜そんなこんなで、
Djangoをsvn最新版にしたら動かなかったが、
原因がださすぎたw

Django-svn/django/template/loader_tags.py

が変更されてました。

http://code.djangoproject.com/browser/django/trunk/django/template/loader_tags.py?rev=7084

r7083からr7084になった時に
テンプレートの

{% extends hoge.html %}

などの前に

{% load i18n %}

などを書いているせいだったw

追って行ったら、
原因が解ったのとnobuさんにも指摘されたっすorz

今後気をつけます。

Posted at: 
2008/02/05 22:52:23
0 Comments
0 TrackBacks
Tags: 
sk8
Trackback: 
http://kishi-r.com/2008/02/05/w_1/trackback/

Social Graph API をPythonでやってみた

気になったのでやってみた。
自分のtwitterのページでFollowされているのを取得する。

まずはベースとなるURLが

http://socialgraph.apis.google.com/lookup

こちらは、
GoogleCodeに記載されている。
http://code.google.com/apis/socialgraph/

で引数を決める。
今回使用したのは、

・pretty(インデント・改行される。)
・callback(コールバック関数を指定)
・edo(FollowしているURLを表示させる)

In [1]: base_url = 'http://socialgraph.apis.google.com/lookup?q=http://twitter.com/kishir&pretty=1&callback=foo&
edo=1'

In [2]: html = opener.open(base_url).read()

In [3]: print html
foo({
 "canonical_mapping": {
  "http://twitter.com/kishir": "http://twitter.com/kishir"
 },
 "nodes": {
  "http://twitter.com/kishir": {
   "attributes": {
    "url": "http://twitter.com/kishir",
    "profile": "http://twitter.com/kishir",
    "rss": "http://twitter.com/statuses/user_timeline/kishir.rss",
    "atom": "http://twitter.com/statuses/user_timeline/kishir.atom"
   },
   "nodes_referenced": {
    "http://kishi-r.com/": {
     "types": [
      "me"
     ]
   },
    "http://twitter.com/kano_e": {
     "types": [
      "contact"
     ]
    },
    "http://twitter.com/soundkitchen": {
     "types": [
      "contact"
     ]
    },
    "http://twitter.com/tasukuchan": {
     "types": [
      "contact"
     ]
    },
    "http://twitter.com/yoshuki": {
     "types": [
      "contact"
     ]
    }
   }
  }
 }
});

なんでだろう?
Followされているユーザーが全て表示されない。
引数「q」に他のURLを加えると増えるのだが、
それも良くわからないなぁ~。

もうちょっと試してみるかな。

Posted at: 
2008/02/04 18:24:01
0 Comments
0 TrackBacks
Tags: 
Python
Trackback: 
http://kishi-r.com/2008/02/04/social_graph_api_python/trackback/

PythonでBeautiful Soupを試してみる。

PythonでHTMLの解析を行うのによさそうだったので、
試してみることにした。

まず必要なのが、

BeautifulSoup

まずインストール(ってかファイルをPythonが読める場所にBeautifulSoup.pyを置くだけ。)

ここからダウンロードする。
それを今回は「site-packages」の下に置いてみた。
清水川Webを参考にさせてもらったためです。

やってみます。

>>> import urllib2
>>> opener = urllib2.build_opener()
>>> html = opener.open('http://kishi-r.com/2008/02/ubuntu_1.html').read()
>>> print html
HTML内の情報が表示されました。
ここでBeautifulSoupを使って「title」のみを解析してみます。
>>> from BeautifulSoup import BeautifulSoup
>>> soup = BeautifulSoup(html)
>>> soup.find('title',)
<title>kishi-r.com: Ubuntuをインストールしました。</title>
取れたっすね。

他にも「td」を検索してみる。
>>> soup.findAll('td',)
[<td>
</td>, <td>
</td>, <td>
</td>, <td>
</td>, <td>
</td>, <td><span class="today">
<a href="http://kishi-r.com/2008/02/01/">1</a>
</span></td>, <td><span class="calendar">
2</span></td>, <td><span class="calendar">
3</span></td>, <td><span class="calendar">
4</span></td>, <td><span class="calendar">
5</span></td>, <td><span class="calendar">
6</span></td>, <td><span class="calendar">
7</span></td>, <td><span class="calendar">
8</span></td>, <td><span class="calendar">
9</span></td>, <td><span class="calendar">
10</span></td>, <td><span class="calendar">
11</span></td>, <td><span class="calendar">
12</span></td>, <td><span class="calendar">
13</span></td>, <td><span class="calendar">
14</span></td>, <td><span class="calendar">
15</span></td>, <td><span class="calendar">
16</span></td>, <td><span class="calendar">
17</span></td>, <td><span class="calendar">
18</span></td>, <td><span class="calendar">
19</span></td>, <td><span class="calendar">
20</span></td>, <td><span class="calendar">
21</span></td>, <td><span class="calendar">
22</span></td>, <td><span class="calendar">
23</span></td>, <td><span class="calendar">
24</span></td>, <td><span class="calendar">
25</span></td>, <td><span class="calendar">
26</span></td>, <td><span class="calendar">
27</span></td>, <td><span class="calendar">
28</span></td>, <td><span class="calendar">
29</span></td>, <td>
</td>]
で取れました。

このBeautifulSoupを使った記事でおもしろそうだったのが、
Googleニュースで指定した単語を検索して、ヒット数を返すサンプルなどがありました。
とりあえず試してみようっと。

Posted at: 
2008/02/04 11:51:27
0 Comments
0 TrackBacks
Tags: 
Python
Trackback: 
http://kishi-r.com/2008/02/04/pythonbeautiful_soup/trackback/

Ubuntuをインストールしました。

以前購入したサーバにUbuntuをインストールしました。

バージョンは、

Ubuntu 7.10(コード名:Gusty Gibbon)

です。
CentOSの時とちょこちょこ違う箇所がありましたが、
そこらへんをWikiにメモしました。

http://project.pod-head.net/wiki/environment2

あ、
Djangoが動くか試してないや。。。
とりあえずTracが動いてMySQlも動いて、
Subversionも動いてたしこれから色々と
やってみようっと。
とりあえず必要なものはほぼ入ったしね。

Posted at: 
2008/02/01 18:54:55
0 Comments
0 TrackBacks
Tags: 
Ubuntu
Trackback: 
http://kishi-r.com/2008/02/01/ubuntu_1/trackback/


kishirをフォローしましょう



あわせて読みたい 人気ブログランキング - kishi-r.com track feed

Categories