Esempio n. 1
0
def reformat(prepared_format, unformatted):
    post_id = prepared_format.post_id
    media_id = prepared_format.media_id
    date = prepared_format.date
    text = prepared_format.text
    value = "Free"
    maximum_length = prepared_format.maximum_length
    text_length = prepared_format.text_length
    post_id = "" if post_id is None else str(post_id)
    media_id = "" if media_id is None else str(media_id)
    extra_count = 0
    if type(date) is str:
        format_variables2 = format_variables()
        if date != format_variables2.date and date != "":
            date = datetime.strptime(date, "%d-%m-%Y %H:%M:%S")
            date = date.strftime(prepared_format.date_format)
    else:
        if date != None:
            date = date.strftime(prepared_format.date_format)
    has_text = False
    if "{text}" in unformatted:
        has_text = True
        text = clean_text(text)
        extra_count = len("{text}")
    if "{value}" in unformatted:
        if prepared_format.price:
            if not prepared_format.preview:
                value = "Paid"
    directory = prepared_format.directory
    path = unformatted.replace("{site_name}", prepared_format.site_name)
    path = path.replace("{first_letter}",
                        prepared_format.username[0].capitalize())
    path = path.replace("{post_id}", post_id)
    path = path.replace("{media_id}", media_id)
    path = path.replace("{username}", prepared_format.username)
    path = path.replace("{api_type}", prepared_format.api_type)
    path = path.replace("{media_type}", prepared_format.media_type)
    path = path.replace("{filename}", prepared_format.filename)
    path = path.replace("{ext}", prepared_format.ext)
    path = path.replace("{value}", value)
    path = path.replace("{date}", date)
    directory_count = len(directory)
    path_count = len(path)
    maximum_length = maximum_length - (directory_count + path_count -
                                       extra_count)
    text_length = text_length if text_length < maximum_length else maximum_length
    if has_text:
        filtered_text = text[:text_length]
        path = path.replace("{text}", filtered_text)
    else:
        path = path.replace("{text}", "")
    directory2 = os.path.join(directory, path)
    directory3 = os.path.abspath(directory2)
    return directory3
Esempio n. 2
0
def reformat(prepared_format, unformatted):
    post_id = prepared_format.post_id
    media_id = prepared_format.media_id
    date = prepared_format.date
    text = prepared_format.text
    value = "Free"
    maximum_length = prepared_format.maximum_length
    text_length = prepared_format.text_length
    post_id = "" if post_id is None else str(post_id)
    media_id = "" if media_id is None else str(media_id)
    extra_count = 0
    if type(date) is str:
        format_variables2 = format_variables()
        if date != format_variables2.date and date != "":
            date = datetime.strptime(date, "%d-%m-%Y %H:%M:%S")
            date = date.strftime(prepared_format.date_format)
    else:
        if date != None:
            date = date.strftime(prepared_format.date_format)
    has_text = False
    if "{text}" in unformatted:
        has_text = True
        text = clean_text(text)
        extra_count = len("{text}")
    if "{value}" in unformatted:
        if prepared_format.price:
            if not prepared_format.preview:
                value = "Paid"
    directory = prepared_format.directory
    path = unformatted.replace("{site_name}", prepared_format.site_name)
    path = path.replace("{first_letter}",
                        prepared_format.username[0].capitalize())
    path = path.replace("{post_id}", post_id)
    path = path.replace("{media_id}", media_id)
    path = path.replace("{username}", prepared_format.username)
    path = path.replace("{api_type}", prepared_format.api_type)
    path = path.replace("{media_type}", prepared_format.media_type)
    path = path.replace("{filename}", prepared_format.filename)
    path = path.replace("{ext}", prepared_format.ext)
    path = path.replace("{value}", value)
    path = path.replace("{date}", date)
    directory_count = len(directory)
    path_count = len(path)
    maximum_length = maximum_length - (directory_count + path_count -
                                       extra_count)
    text_length = text_length if text_length < maximum_length else maximum_length
    if has_text:
        # https://stackoverflow.com/a/43848928
        def utf8_lead_byte(b):
            '''A UTF-8 intermediate byte starts with the bits 10xxxxxx.'''
            return (b & 0xC0) != 0x80

        def utf8_byte_truncate(text, max_bytes):
            '''If text[max_bytes] is not a lead byte, back up until a lead byte is
            found and truncate before that character.'''
            utf8 = text.encode('utf8')
            if len(utf8) <= max_bytes:
                return utf8
            i = max_bytes
            while i > 0 and not utf8_lead_byte(utf8[i]):
                i -= 1
            return utf8[:i]

        filtered_text = utf8_byte_truncate(text, text_length).decode('utf8')
        path = path.replace("{text}", filtered_text)
    else:
        path = path.replace("{text}", "")
    directory2 = os.path.join(directory, path)
    directory3 = os.path.abspath(directory2)
    return directory3