<commit_before>#!/usr/bin/env python3

# Given a Backlist post file, collect ASINs for books as a
# comma-separated list to use as seed content in Amazon aStore categories.

import os
import sys

import yaml


def grab_yaml_frontmatter(f):
    '''Given a file, return YAML frontmatter as string, if present'''
    yaml_result = ''

    if f.readline() == '---\n':
        yaml_active = True
        for line in f.readlines():
            if yaml_active:
                if line != '---\n':
                    yaml_result += line
                else:
                    yaml_active = False

    return yaml_result


def book_ids_from_frontmatter(frontmatter):
    '''Return a list of book id hashes from frontmatter of list file.'''
    sections = yaml.load(frontmatter)['sections']

    books = []
    for section in sections:
        for source in section['listings']:
            if source['type'] == 'book':
                books.append(source['id'])

    return books


def get_book_data_paths(list_file_path, books):
    '''Given root book data directory, return list of paths to files that \
    match book id hashes in given list of hashes.'''
    book_data_dir = os.path.abspath(list_file_path) + '/../../../../../_books'
    book_data_dir = os.path.abspath(book_data_dir)

    book_data_paths = []
    for path, visit, arg in os.walk(book_data_dir):
        for filename in arg:
            if os.path.splitext(filename)[1] == '.bib':
                for book in books:
                    if filename.find(book) >= 0:
                        book_data_paths.append(path + '/' + filename)

    return book_data_paths


def get_asins_from_files(book_data_paths):
    '''Given list of file paths, return list of ASIN strings in YAML \
    frontmatter in specified files.'''
    asins = []

    for path in book_data_paths:
        book_file = open(path)
        book_yaml = grab_yaml_frontmatter(book_file)
        asins.append(str(yaml.load(book_yaml)['amzn']))
        book_file.close()

    return asins


if __name__ == '__main__':
    f = open(sys.argv[1])

    yaml_frontmatter = grab_yaml_frontmatter(f)

    books = book_ids_from_frontmatter(yaml_frontmatter)
    book_data_paths = get_book_data_paths(sys.argv[1], books)
    asins = get_asins_from_files(book_data_paths)

    asin_string = ''
    for asin in asins:
        asin_string += asin + ', '
    asin_string = asin_string[:-2]

    print("ASINs for aStore:")
    print(asin_string)

    f.close()<commit_msg>Make ASIN script work on new backlist module<commit_after>#!/usr/bin/env python3

# Given a Backlist post file, collect ASINs for books as a
# comma-separated list to use as seed content in Amazon aStore categories.

import sys

import backlist


if __name__ == '__main__':
    f = open(sys.argv[1])

    yaml_frontmatter = backlist.grab_yaml_frontmatter(f)

    books = backlist.book_ids_from_frontmatter(yaml_frontmatter)
    book_data_paths = backlist.get_book_data_paths(sys.argv[1], books)
    asins = backlist.get_asins_from_files(book_data_paths)

    asin_string = ''
    for asin in asins:
        asin_string += asin + ', '
    asin_string = asin_string[:-2]

    print("ASINs for aStore:")
    print(asin_string)

    f.close()
{'fraction_non_alphanumeric': 0.10483870967741936, 'fraction_numerical': 0.003101736972704715, 'mean_word_length': 2.7154377880184333, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, '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': '757169', 'n_tokens_mistral': 1094, 'n_tokens_neox': 1020, 'n_words': 270}