def write_bills_pages():
    logger_html.info("Generating bills html pages.")
    per_bill_template = templates.get_template("bill_D_S_template.html")
    billcur = db.cursor()
    billcur.execute("""SELECT * FROM bills""")
    count = 0
    for name, sig, date, original_url in billcur:
        print count
        count += 1
        chroncur = db.cursor()
        chroncur.execute(
            """SELECT event, event_date FROM bill_history
                            WHERE bill_signature = %s""",
            (sig,),
        )
        authcur = db.cursor()
        authcur.execute(
            """SELECT bill_author FROM bill_authors
                           WHERE bill_signature = %s""",
            (sig,),
        )
        authors = [(a, "mp_%s.html" % unicode2urlsafe(a)) for (a,) in authcur]
        authcur.execute(
            """SELECT COUNT(*) FROM bills_by_government
                           WHERE bill_signature = %s""",
            (sig,),
        )
        if authcur.fetchone()[0]:
            authors.append((u"Mинистерски съвет", "http://www.government.bg/"))
        with open("generated_html/bill_%s_%s.html" % (date.strftime("%Y%m%d"), sig), "w") as html_file:
            html_file.write(
                per_bill_template.render(
                    name=name, chronology=chroncur.fetchall(), authors=authors, original_url=original_url
                )
            )
            sitemap.add("bill_%s_%s.html" % (date.strftime("%Y%m%d"), sig), 0.7)
def write_MPs_overview_page():
    load_votes_regs_data()

    ############
    # Summary. #
    ############
    logger_html.info("Generating summary html page with MP details.")

    # Plots
    alltime_regs(*np.sum(mps_all_reg, 0))
    alltime_votes(*np.sum(all_sessions_vote, 0))

    # HTML
    mps_template = templates.get_template("mps_template.html")
    with open("generated_html/mps.html", "w") as html_file:
        html_file.write(
            mps_template.render(
                mps=mps,
                mps_all_reg=mps_all_reg,
                mps_all_vote=mps_all_vote,
                mps_all_with_against_party=mps_all_with_against_party,
                mps_all_with_against_all=mps_all_with_against_all,
            )
        )
        sitemap.add(
            "mps.html",
            0.8,
            [
                ("alltimeregs.png", u"Oтсъствия на депутати по време на регистрация."),
                ("alltimevotes.png", u"Гласове и отсъствия на депутати по време на гласувания."),
            ],
        )

    #################
    # Per MP pages. #
    #################
    per_mp_template = templates.get_template("mp_N_template.html")
    only_dates = [d[0] for d in session_dates]
    for mp_i, (name, party, party_now, original_url) in enumerate(mps):
        logger_html.info("Generating page for MP %d of %d." % (mp_i, len(mps)))
        asciiname = unicode2urlsafe(name)
        alltime_regs_singleMP(mps_all_reg[mp_i, :], name, asciiname)
        alltime_votes_singleMP_compare_all(mps_all_with_against_all[mp_i, :], mps_all_vote[mp_i, 2:], name, asciiname)
        alltime_votes_singleMP_compare_party(
            mps_all_with_against_party[mp_i, :], mps_all_vote[mp_i, 2:], name, asciiname
        )
        evolution_of_votes_singleMP(
            only_dates,
            mps_dates_vote[mp_i, :, :],
            mps_dates_with_against_all[mp_i, :, :],
            mps_dates_with_against_party[mp_i, :, :],
            name,
            asciiname,
        )
        with open("generated_html/mp_%s.html" % asciiname, "w") as html_file:
            html_file.write(
                per_mp_template.render(
                    name=name,
                    asciiname=asciiname,
                    party=party,
                    party_now=party_now,
                    vote=mps_all_vote[mp_i, :],
                    with_against_p=mps_all_with_against_party[mp_i, :],
                    with_against_a=mps_all_with_against_all[mp_i, :],
                    reg=mps_all_reg[mp_i, :],
                    original_url=original_url,
                )
            )
            sitemap.add(
                "mp_%s.html" % asciiname,
                0.7,
                [
                    ("vote_evol_%s.png" % asciiname, u"Гласовете и отсъствията на %s през годините." % name),
                    ("alltimeregs_%s.png" % asciiname, u"Oтсъствия на %s по време на регистрация." % name),
                    (
                        "alltimevotes_compare_all_%s.png" % asciiname,
                        u"Гласове и отсъствия на %s по време на гласувания (сравнение с мнозинството)." % name,
                    ),
                    (
                        "alltimevotes_compare_party_%s.png" % asciiname,
                        u"Гласове и отсъствия на %s по време на гласувания (сравнение с позицията на партията)." % name,
                    ),
                ],
            )