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

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2011  Laurent Peuch  <cortex@worlddomination.be>

import sys
import urllib2
from BeautifulSoup import BeautifulSoup
from re import compile
from urllib2 import urlopen


WHITESPACES = compile(r'\s+')

def get_title(url):
    html = urlopen(url).read()
    soup = BeautifulSoup(html)
    title = soup.title.string if soup.title and soup.title.string else u''
    # Normalize whitespaces
    title = title.strip()
    title = WHITESPACES.sub(' ', title)

    url = u'%s %s\n' % (url[:-1], title)
    return url.encode('utf-8')

def run():
    url = sys.stdin.readline()
    while url:
        try:
            title = get_title(url)
            sys.stdout.write(title)
        except urllib2.HTTPError:
            sys.stderr.write("%s" % url)
        sys.stdout.flush()
        url = sys.stdin.readline()

if __name__ == "__main__":
    run()

# vim:set shiftwidth=4 tabstop=4 expandtab:
