「2008年下半期ライトノベルサイト杯」心友チェッカー

2008年下半期ライトノベルサイト杯への投票から自分の投票との類似度とユニークさを基準に一覧してみる Python スクリプトです。

で作ったやつの改良版。

これ使ってあなたと同じものに投票した心の友へのリンクをあなたのブログに貼り付けよう!
(Python 2.5, 2.6 用)

from __future__ import division
import urllib

URL = 'http://d.hatena.ne.jp/kadotanimitsuru/20090117'
# ↑これを自分の投票記事の URL に書き換える

baseurl = 'http://ippo.dip.jp/lightnovel/lnsite2008last/vote/'

url = baseurl + 'data_book_infos'
print 'read:', url
f = urllib.urlopen(url)
books = {}
for i in unicode(f.read(), 'utf').splitlines():
    d = dict(zip(
        ['code', 'isbn', 'title', 'author', 'illustrator',
         'label', 'type', 'date', 'dummy1', 'price', 'img',
         'amazon', 'width', 'high', 'url', 'dummy2', 'vote_count']
        ,i.split('\t') + [0]))
    books[d['code']] = d
f.close()
url = baseurl + 'data_votes'
print 'read:', url
f = urllib.urlopen(url)
votes = {}
for i in unicode(f.read(), 'utf').splitlines():
    section, name, url, code, disable, dummy = i.split('\t')
    if disable:
        continue
    books[code]['vote_count'] += 1
    site = name, url
    if site in votes:
        votes[site].append(code)
    else:
        votes[site] = [code]
f.close()
for name, url in votes:
    if url == URL:
        my_site = name, url
        break
else:
    raise KeyError(URL + ' not found.')
my_codes = votes[my_site]
data = []
for (name, url), codes in votes.items():
    codes.sort()
    c = []
    others = []
    point = 0
    for i in codes:
        if i in my_codes:
            c.append(i)
            point += 1 / books[i]['vote_count']
        else:
            others.append(i)
    if c:
        data.append((point, len(c), c, name, url, others))
data.sort(cmp=lambda x,y:cmp(y[:2], x[:2]) or cmp(x[2:], y[2:]))
d = []
for point, n, c, name, url, others in data:
    d.append(u'''<dt>%.2f %%, %s作品: <a href="%s">%s</a></dt>
<dd><small>%s</small></dd>''' % (
    point * 20, n, url, name, ', '.join(
        (', '.join([(
    u'<a href="%sranks/%s"><strong>%s</strong></a> %d人' % (
        baseurl, books[x]['code'], books[x]['title'], books[x]['vote_count'])) for x in c]),
         ', '.join([(
    u'<a href="%sranks/%s">%s</a> %d人' % (
        baseurl, books[x]['code'], books[x]['title'], books[x]['vote_count'])) for x in others])))))
         
html = u'''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>同じ作品へ投票した人</title>
</head>
<body>
<p>投票が重なっている各作品について「1/投票者数」を合計した値の順。</p>
<p>理論上最高のマッチング(その投票者達2人のみが投票しているもので10作全部が占められている)を「100%%」として表記。</p>

<dl>%s</dl>

</body>''' % '\n'.join(d)
filename = 'love.htm'
print 'write:', filename
f = open(filename, 'w')
f.write(html.encode('utf'))
f.close()
# 好きに流用してください。

参考: 「得票数と読書メーター登録数」で読むライトノベルサイト杯結果 - 平和の温故知新@はてな