<commit_before>#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Interpreter version: python 2.7
#
#= Imports ====================================================================
import sys
import os.path
import getpass
import argparse

import abclinuxu  # https://github.com/Bystroushaak/abclinuxuapi
import dhtmlparser as d


#= Variables ==================================================================
#= Functions & objects ========================================================
def upload_file(args):
    print args


#= Main program ===============================================================
if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="abclinuxu.cz blog uploader.")

    parser.add_argument(
        "FN",
        type=str,
        help="Filename of the HTML document, which will be parsed and uploaded."
    )
    parser.add_argument(
        "-u",
        '--username',
        metavar="USER",
        required=True,
        type=str,
        help="Your username."
    )
    parser.add_argument(
        "-p",
        '--password',
        metavar="PASS",
        type=str,
        default=None,
        help="Your password. If not set, you will be asked interactively."
    )
    parser.add_argument(
        "-t",
        "--title",
        type=str,
        default=None,
        help="Title of your blogpost. If not set, <title> tag from your\
              document is used."
    )
    args = parser.parse_args()

    if not os.path.exists(args.FN):
        sys.stderr.write("File '%s' doesn't exists!\n" % args.FN)
        sys.exit(1)

    dom = None
    with open(args.FN) as f:
        data = f.read()
        dom = d.parseString(data)

    if args.title is None:
        title = dom.find("title")

        if not title:
            sys.stderr.write("Can't find <title> in your document.")
            sys.stderr.write("Use --title switch.\n")
            sys.exit(1)

        args.title = title[0].getContent().strip()

    if args.password is None:
        args.password = getpass.getpass("Password for '%s': " % args.username)

    upload_file(args)<commit_msg>Uploader should now work.<commit_after>#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Interpreter version: python 2.7
#
#= Imports ====================================================================
import os
import sys
import os.path
import getpass
import argparse

import abclinuxu  # https://github.com/Bystroushaak/abclinuxuapi
import dhtmlparser as d


#= Variables ==================================================================
ALLOWED_IMAGES = [
    "jpg",
    "jpeg",
    "gif",
    "png"
]


#= Functions & objects ========================================================
def get_body(dom):
    body = dom.find("body")
    if not body:
        return str(dom)
    else:
        return body[0].getContent()


def upload_image(concept, image_path):
    # remote images
    if not os.path.exists(image_path):
        return image_path

    concept.add_pic(open(image_path))

    return concept.list_pics()[-1]


def upload_html(dom, args):
    user = abclinuxu.User(args.username, args.password)

    try:
        user.add_concept(get_body(dom), args.title)
    except ValueError, e:
        sys.stderr.write("Fail: " + e.message + "\n")
        sys.exit(1)

    concept = user.get_concepts()[-1]

    print "Uploading your concept '%s'" % concept.title
    print "Uploading inlined images:"

    # upload inlined images
    for img in dom.find("img"):
        if "src" not in img.params:
            continue

        print "\tUploading '%s'" % os.path.basename(img.params["src"])

        img.params["src"] = upload_image(concept, img.params["src"])

    print
    print "Uploading linked images:"

    # upload linked images
    for a in dom.find("a"):
        if "href" not in a.params or "." not in a.params["href"]:
            continue

        if a.params["href"].rsplit(".", 1)[1].lower() not in ALLOWED_IMAGES:
            continue

        print "\tUploading '%s'" % os.path.basename(a.params["href"])

        a.params["href"] = upload_image(concept, a.params["href"])

    print
    print "Uploading concept .."

    try:
        concept.edit(get_body(dom))
    except ValueError, e:
        sys.stderr.write("Fail: " + e.message + "\n")
        sys.exit(1)

    print "Done: " + concept.link


#= Main program ===============================================================
if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="abclinuxu.cz blog uploader.")

    parser.add_argument(
        "FN",
        type=str,
        help="Filename of the HTML document, which will be parsed and uploaded."
    )
    parser.add_argument(
        "-u",
        '--username',
        metavar="USER",
        required=True,
        type=str,
        help="Your username."
    )
    parser.add_argument(
        "-p",
        '--password',
        metavar="PASS",
        type=str,
        default=None,
        help="Your password. If not set, you will be asked interactively."
    )
    parser.add_argument(
        "-t",
        "--title",
        type=str,
        default=None,
        help="Title of your blogpost. If not set, <title> tag from your\
              document is used."
    )
    args = parser.parse_args()

    if not os.path.exists(args.FN):
        sys.stderr.write("File '%s' doesn't exists!\n" % args.FN)
        sys.exit(1)

    dom = None
    with open(args.FN) as f:
        data = f.read()
        dom = d.parseString(data)

    if args.title is None:
        title = dom.find("title")

        if not title:
            sys.stderr.write("Can't find <title> in your document.")
            sys.stderr.write("Use --title switch.\n")
            sys.exit(1)

        args.title = title[0].getContent().strip()

    if args.password is None:
        args.password = <PASSWORD>("Password for '%s': " % args.username)

    os.chdir(os.path.dirname(args.FN))
    upload_html(dom, args)
{'fraction_non_alphanumeric': 0.2095828446976043, 'fraction_numerical': 0.0031831127492042218, 'mean_word_length': 2.464886825304701, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1519452', 'n_tokens_mistral': 1827, 'n_tokens_neox': 1738, 'n_words': 432}