コード例 #1
0
ファイル: build_site.py プロジェクト: filiph/mobfriendly
def build_for_country(country):
    csv_filename = get_output_file_from_country(country,
                                                consolidated=True)
    with open(csv_filename, "r") as f:
        sites = get_sites_from_csv(f)

    sites.sort(key=lambda site: site.country_rank)

    directory = os.path.join("build", country)
    if not os.path.exists(directory):
        os.makedirs(directory)

    # Build index.html.
    filename = os.path.join(directory, "index.html")
    with open(filename, "w") as f:
        def write(s):
            f.write(s.encode(UTF8))

        write(html_head.format(
            title=u"Mobilní web: " + country,
            css=u"../screencaps.css"
        ))

        write(u"<div class=\"header\">")
        write(u"<h1>.{tld} sites</h1>".format(tld=country))
        write(u"<p><small><a href=\"https://stav-mobilniho-webu.appspot.com/\">"
              u"Back to main site</a></small></p>")
        write(u"</div>")

        for site in sorted(sites, key=lambda site: site.country_rank):
            assert isinstance(site, SiteInfo)
            site_directory = os.path.join(directory, site.url)
            if not os.path.exists(site_directory):
                os.makedirs(site_directory)

            write(u"<div class='site {class_f} {class_b}'>".format(
                class_f=u"friendly-yes" if site.friendly else u"friendly-no",
                class_b=u"broken-yes" if site.broken else u""
            ))
            write(u"<a href=\"{subdir}/\">".format(
                subdir=site.url
            ))
            copy_and_resize_image(site, site_directory)
            write(u"<img class='screencap' src=\"{url}/{url}.jpg\" />".format(
                url=site.url))
            write(u"<p class='url'>{url}</p>".format(url=site.url))
            if site.friendly is not None:
                write(u"<p class='friendly {class_s}'>{friendly}</p>".format(
                    friendly=u"Friendly" if site.friendly else u"Unfriendly",
                    class_s=u"friendly-yes" if site.friendly else u"friendly-no"
                ))
            if site.broken is not False:
                write(u"<p class='broken'>Broken</p>")
            write(u"</a>")
            write(u"</div>")

            # Build example.cz/index.html
            build_site_index_html(site, site_directory)

        write(html_tail)