メインコンテンツに移動
mattz.xii.jp

Main navigation

  • Home
User account menu
  • ログイン

パンくず

  1. ホーム

2ログビューア(6)-Class化の続き

By mattz, 2008/05/23

続き

poptotype3.pyの中身

#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
read.cgi
'''
import sys
sys.path.append('/home/mattz/lib/python')
from read2ch import read2ch

import codecs
fout = codecs.getwriter('shift_jisx0213')(sys.stdout)

thread = read2ch( 'handygame', '1211373877' )
title  = thread.getThreadTitle()
count  = thread.getResCount()

print 'Content-Type: text/html;charset;Shift_JIS'
print
fout.write('''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content='text/html; charset="Shift_JIS"'>
<title>%s</title>
</head>
<body><h1>%s</h1><dl>
%s
</dl></body></html>
''' % (title, title, thread.getFormatedReses( range(count))))

んでもって、read2ch.pyの中身

#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
2ちゃんねるの.datファイルを扱うクラス
  0.0.1 prototype版
'''
import codecs,re
line_end_br = re.compile( '((<br>)?\s*)+$' )
normal_br   = re.compile( '\s*<br>' )
'''
1行分のログとレス番号からdt要素とdd要素を生成する
  TODO:
    本文中のURLと思われる文字列をリンクする処理
    アンカーと思われる文字列に対する処理
    IDの有無/強制/任意などの対応
    日付関係の処理
'''
def formatRes( rstr, rnum ):
        ar = re.split( '<>', rstr )
        resFormat  = u"<dt id='t-%d'>%d : 名前:%s %s</dt><dd id='d-%d'>%s<br><br></dd>\n"
        name = '<b>%s</b>' % ( ar[0] )
        if ar[1]:
                name = '<a href="mailto:%s">%s</a>' % ( ar[1], name )

        return resFormat % ( rnum, rnum, name, ar[2], rnum, normal_br.sub( '<br>', line_end_br.sub( "", ar[3])) )

class read2ch:
        base_dir = '/home/mattz/data/2channel/'
        resAll   = []

        def __init__( self, board, thread ):
                self.board  = board
                self.thread = thread
                self.dat    = "%s%s/%s.dat" % ( self.base_dir, board, thread )
                self.idx    = "%s%s/%s.idx" % ( self.base_dir, board, thread )
                fres = codecs.open( self.dat, 'r', 'shift_jisx0213', 'strict' )
                for line in fres:
                        self.resAll.append( line )
                fres.close()
                fres = codecs.open( self.idx, 'r', 'shift_jisx0213', 'strict' )
                lines = re.split( '>', fres.readline() );
                self.title     = lines[0]
                self.standDate = lines[4]
                self.resCount  = lines[5]
                fres.close()
        def getDataFilename( self ):
                return self.dat

        def getThreadTitle( self ):
                return self.title

        def getFormatedReses( self, num ):
                ret = ''
                for n in num:
                        ret += formatRes( self.resAll[n], n + 1 )
                return ret

        def getResCount( self ):
                return int( self.resCount )

細かい中身のことは書かない。

タグ

  • Python
前のエントリー次のエントリー

最近のエントリー

  • 2025シーズン セ・パ12球団の総移動距離
  • 2023年度 パシフィック・リーグ主催試合ヒーローインタビューランキング
  • 2024シーズン セ・パ12球団の総移動距離
  • 当サイトのDrupalが10になりました
  • E-Vinoを買ってみました
  • 2023シーズン セ・パ12球団の総移動距離
  • 俺は野球殿堂博物館を甘く見ていた
  • 2022シーズン セ・パ12球団の総移動距離
  • Drupalを9にあげてみました
  • 2021シーズン セ・パ12球団の総移動距離
RSS feed