<commit_before>#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
import os
import shutil
from pprint import pprint
from subprocess import call, check_call, check_output
import xml.etree.ElementTree as ET
from mutagen.mp3 import MP3
from slacker import Slacker
import yaml
import gettext

global config
global translation
global _


def main(argv):
    config = load_config("/tmp/config/config.yml")
    if config:
        translation = gettext.translation('messages', os.path.join(os.path.dirname(__file__), "i18n"), [config["language"]])
    else:
        translation = gettext.translation('messages', os.path.join(os.path.dirname(__file__), "i18n"), ["en"])
    translation.install()
    
    fullpaths = list_files("/tmp/input")
    print(str(len(fullpaths))+translation.ngettext(" file in fileset", " files in fileset", len(fullpaths)))
    
    spine = fileset_to_spine(fullpaths)
    print(str(len(spine))+translation.ngettext(" audio file in spine", " audio files in spine", len(spine)))
    
    source_audio = pick_abstract(spine)
    print(source_audio+_(" will be used as input"))
    
    book_id = os.path.relpath(source_audio, "/tmp/input").rsplit("/")[0]
    assert book_id != None and len(book_id) > 0, "Unable to determine book ID based on path"
    
    shutil.copy(source_audio, "/tmp/source_audio.mp3")
    sys.stdout.flush()
    check_call(["mp3splt", "/tmp/source_audio.mp3", "-o", "output/"+book_id, "0.00.00", "1.15.00"], timeout=300)
    os.chmod("/tmp/output/"+book_id+".mp3", 0o666)
    
    # Send message to Slack if there's a slack token available
    if os.path.exists("/tmp/config/slack.token"):
        with open("/tmp/config/slack.token", 'r') as f:
            slack_token = f.readline().rstrip()
        book_title = None
        for path in fullpaths:
            if os.path.basename(path) == "ncc.html":
                try:
                    with open(path, 'r') as f:
                        lines = f.readlines()
                    for line in lines:
                        if "<title" in line:
                            book_title = line
                            book_title = line.split("<title",1)[1].split(">",1)[1].split("<",1)[0]
                            break
                except UnicodeDecodeError as e:
                    print(e)
        slack = Slacker(slack_token)
        if book_title == None or len(book_title) == 0:
            book_title = ""
        else:
            book_title = " ("+book_title+")"
        slack.chat.post_message(
                                '#autoprod',
                                _('Audio abstract is ready for')+' '+book_id+book_title,
                                username="abstracts-bot"
                                )
    else:
        print("(no slack token in /tmp/config/slack.token; won't post to slack)")

<commit_msg>customizable botname and channel for abstracts-bot<commit_after>#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
import os
import shutil
from pprint import pprint
from subprocess import call, check_call, check_output
import xml.etree.ElementTree as ET
from mutagen.mp3 import MP3
from slacker import Slacker
import yaml
import gettext

global config
global translation
global _


def main(argv):
    config = load_config("/tmp/config/config.yml")
    if config:
        translation = gettext.translation('messages', os.path.join(os.path.dirname(__file__), "i18n"), [config["language"]])
    else:
        translation = gettext.translation('messages', os.path.join(os.path.dirname(__file__), "i18n"), ["en"])
    translation.install()
    
    fullpaths = list_files("/tmp/input")
    print(str(len(fullpaths))+translation.ngettext(" file in fileset", " files in fileset", len(fullpaths)))
    
    spine = fileset_to_spine(fullpaths)
    print(str(len(spine))+translation.ngettext(" audio file in spine", " audio files in spine", len(spine)))
    
    source_audio = pick_abstract(spine)
    print(source_audio+_(" will be used as input"))
    
    book_id = os.path.relpath(source_audio, "/tmp/input").rsplit("/")[0]
    assert book_id != None and len(book_id) > 0, "Unable to determine book ID based on path"
    
    shutil.copy(source_audio, "/tmp/source_audio.mp3")
    sys.stdout.flush()
    check_call(["mp3splt", "/tmp/source_audio.mp3", "-o", "output/"+book_id, "0.00.00", "1.15.00"], timeout=300)
    os.chmod("/tmp/output/"+book_id+".mp3", 0o666)
    
    # Send message to Slack if there's a slack token available
    if os.path.exists("/tmp/config/slack.token"):
        with open("/tmp/config/slack.token", 'r') as f:
            slack_token = f.readline().rstrip()
        book_title = None
        for path in fullpaths:
            if os.path.basename(path) == "ncc.html":
                try:
                    with open(path, 'r') as f:
                        lines = f.readlines()
                    for line in lines:
                        if "<title" in line:
                            book_title = line
                            book_title = line.split("<title",1)[1].split(">",1)[1].split("<",1)[0]
                            break
                except UnicodeDecodeError as e:
                    print(e)
        slack = Slacker(slack_token)
        if book_title == None or len(book_title) == 0:
            book_title = ""
        else:
            book_title = " ("+book_title+")"
        botname = "abstracts-bot"
        channel = "#general"
        if config["abstracts-botname"]:
            botname = config["abstracts-botname"]
        if config["abstracts-channel"]:
            channel = config["abstracts-channel"]
        slack.chat.post_message(
                                channel,
                                _('Audio abstract is ready for')+' '+book_id+book_title,
                                botname
                                )
    else:
        print("(no slack token in /tmp/config/slack.token; won't post to slack)")


{'fraction_non_alphanumeric': 0.13187552565180824, 'fraction_numerical': 0.0127838519764508, 'mean_word_length': 2.1410459587955626, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2118776', 'n_tokens_mistral': 1846, 'n_tokens_neox': 1781, 'n_words': 421}