4. Rss.py는 다음과 같습니다.
mod_python으로 연결해 놓으시면 됩니다.

# -*- coding: utf-8 -*-

from mod_python import apache
import pickle, re
import os.path, time

url_head = "http://asialadders.battle.net/war3/ladder/W3XP-player-profile.aspx?Gateway=Kalimdor&&PlayerName="

def conv( date ):
    ds = date.split(',')
    ds1 = ds[1].split(' ')
    ds2 = ds[2].split(' ')
    date = ds[0][:3]+', '+ds1[2]+' '+ds1[1][:3]+' '+ds2[1]+' '+ds2[2]+' '+ds2[3]
    return date
def getInfo( ):
    f = open('/var/www/war3/info', 'rb')
    info = pickle.load( f )
    f.close()
    return info

def handler(req):
    req.content_type="Text/xml"
    req.send_http_header()

    t = os.path.getctime('/var/www/war3/info')
    pubdate = time.asctime( time.gmtime(t) )

    body ="""<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
    <channel>
        <title>Frozen Throne - Kalimdor Tracer</title>
        <link>http://cybershin.x-y.net/tt/</link>
        <description>누가누가 달렸나 모니터링 시스템</description>
        <language>ko</language>
        <pubDate>%s</pubDate>
        <generator>dsp generator</generator>""" %pubdate
    footer = """
    </channel>
</rss>"""
    info = getInfo()
    for i in info:
        user, level, date = i
        date = conv(date)
        level = level.replace('l','l ')
        link = url_head + user
        body += """
        <item>
            <title>%s</title>
            <link>%s</link>
            <description>%s, Last Ladder Game : %s</description>
            <author>(%s)</author>
            <guid>%s#%s</guid>
            <pubDate>%s</pubDate>
        </item>"""%(user, link, level, date, user, link, re.sub(' ','',date), date )
        # Sat, 18 Apr 2009 00:15:00 +0900
    body += footer
    req.write( body )
    return apache.OK


5. 성공적으로 실행되면 브라우저상에서 다음과 같이 잘 출력되는 것을 볼 수 있으며,
hanrss, outlook 등으로 연결해서도 잘 되는 것을 확인할 수 있습니다.

Frozen Throne - Kalimdor Tracer
누가누가 달렸나 모니터링 시스템
     
alpakook
Level 12, Last Ladder Game : Sat, 09 May 2009 1:54 AM
dspshin
Level 6, Last Ladder Game : Fri, 08 May 2009 12:40 AM
soudz
Level 22, Last Ladder Game : Thu, 23 Apr 2009 12:58 AM
milkelf
Level 9, Last Ladder Game : Sun, 03 May 2009 8:20 PM
sacrea
Level 2, Last Ladder Game : Sun, 12 Apr 2009 8:31 PM
again4you
Level 1, Last Ladder Game : Tue, 05 May 2009 6:49 PM

AND

이번 예제는 친구들이 워3를 했나안했나 체크해서,
rss로 제공해 주는 rss feed generator입니다. ㅎㅎ

1.
저는 Python으로 할 것이므로 mod_python을 설치/설정합니다.
당연히 PHP등 다른 언어 사용해도 무방.
> 방법 : http://cybershin.x-y.net/tt/188 참고.

2.
이 예제는 특정 사이트를 모니터링하며 해당 정보가 업데이트되면 RSS로 알려주는 예제입니다.
고로 Request가 올때마다 특정 사이트를 읽어서 답하면 너무 늦으므로 프로세스를 2개로 나눠서 실행합니다.
즉, 정보 수집을 맡는 Crawl.py 와 RSS결과를 반환해 주는 rss.py로 분리.

3. Crawl.py는 다음과 같습니다.
crontab 등으로 하루에 몇번만 실행시키면 됩니다.


#!/usr/bin/python
# -*- coding: utf-8 -*-

import urllib2, re, pickle, sys, time
from BeautifulSoup import BeautifulSoup

users = [
'alpakook', 'dspshin', 'soudz', 'milkelf', 'sacrea','again4you'
]

url_head = "http://asialadders.battle.net/war3/ladder/W3XP-player-profile.aspx?Gateway=Kalimdor&PlayerName="

def getInfo( user ):
    url = url_head + user
    contents = urllib2.urlopen(url).read()
    soup = BeautifulSoup( contents )
    B = soup('b', {'class':'small'})
    date = ''
    for b in B:
        if str(b).find(':')>-1:
            date = b.contents[0].strip().encode('ascii')

    level = ''
    lv = soup('div', {'style':"Z-INDEX: 200; LEFT: 75px; POSITION: relative; TOP: -25px"})
    if len(lv)>0:
        body = str(lv[0])
        sp = body.find('Level')
        ep = body[sp:].find('<')
        level = body[sp:sp+ep].strip()
        level = re.sub('\s', '', level)

    return level, date

if __name__=="__main__":
        print sys.version
        print 'run crawl.py : '+ time.strftime("%B %dth %A %I:%M ",time.localtime())
        info = []
        for user in users:
                try:
                        level, date = getInfo(user)
                except:
                        print sys.exc_info()
                else:
                        print user, level, date
                        info.append( (user, level, date) )

        #print info
        f = open('/var/www/war3/info', 'wb')
        pickle.dump( info, f )
        f.close()



>>> 나머지는 다음 글에...


AND