import bs4
import re

def parse_manga_page(html):
        match_obj = re.compile(r"document\['(\w*)']\s*=\s*(\S*)")
        attrs = {}
        soup = bs4.BeautifulSoup(html)
        scripts = soup.findAll('script')
        assert scripts , "No Scripts tag found"
        l = scripts[-1].string.split(';')
        for line in l:
            m = match_obj.search(line)
            if m:
                attrs[m.group(1)] = eval(m.group(2))

        page_menu = soup.find('select',id='pageMenu')
        assert page_menu, "Could not find page menu"

        page_map = {int(o.string):o['value'] for o in page_menu.findAll('option')}
        total_pages = len(page_map)
        current_page_no = int(page_menu.find('option', selected="selected").string)

        current_img_link = soup.find('div',id='imgholder').img['src']
        attrs['totalpages'] = total_pages
        attrs['pageno'] = current_page_no
        attrs['imagelink'] = current_img_link
        attrs['pagemap'] = page_map
        info_str = soup.find('div',id='mangainfo').a.string
        attrs['manganame'] = info_str[:info_str.rfind('Manga')].strip()
        return attrs

