# coding: utf-8

INDEX_URL = "http://narm.co.kr/diary_date04.htm"

from BeautifulSoup import BeautifulSoup
import urllib
import re
import sys; reload(sys)

from datetime import datetime
import PyRSS2Gen

sys.setdefaultencoding('utf-8')

index_page = urllib.urlopen(INDEX_URL)
soup = BeautifulSoup(index_page)

base_url = '/'.join(index_page.url.split('/')[:-1]) + '/'
rss_items = []
src = soup('table')[:2]
links = src[1].findAll("a")+src[0].findAll("a")
for link in links[-10:]:
    date_string = link.get("href").split("/")[-1]
    y = int(date_string[6:10])
    m = int(date_string[10:12])
    d = int(date_string[12:14])
    href = base_url + link.get("href")

    body_soup = BeautifulSoup(urllib.urlopen(href))
    descr = body_soup("img", {'src':re.compile('diary')})[0].prettify()

    rss_item = PyRSS2Gen.RSSItem(
            author = "narm@paran.com",
            title = date_string,
            link = href,
            description = descr.replace('src="', 'src="'+base_url),
            guid = PyRSS2Gen.Guid(link.get('href')),
            pubDate = datetime(y, m, d, 23, 59, 59))
    rss_items.append(rss_item)
rss_items.reverse()

rss = PyRSS2Gen.RSS2(
        title = "낢이 사는 이야기" or soup.title.renderContents(),
        link = "http://narm.co.kr/diary.htm" or INDEX_URL,
        description = "",
        lastBuildDate = datetime.now(),
        items = rss_items)

rss.write_xml(open("diary.xml", "w"), "UTF-8")
