File size: 3,367 Bytes
973aec5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import json
import logging
import datetime
import time
import requests
import pytz
from deep_translator import GoogleTranslator
from deep_translator.exceptions import NotValidLength, RequestError
from utils import process_json_files, flatten_text_with_line_breaks, build_word_index

# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

# Load Tanach text
TANACH_DATA = process_json_files(1, 39)
WORD_INDEX = build_word_index(TANACH_DATA)

# --- Translation Cache ---
translation_cache = {}

def translate_chapter(hebrew_chapter):
    """Translates a Hebrew chapter to English, caching the result."""
    if hebrew_chapter in translation_cache:
        return translation_cache[hebrew_chapter]

    try:
        translator = GoogleTranslator(source='iw', target='en')
        max_length = 2000  # Slightly below the limit to be safe
        translated_text = ""

        # Split the chapter into chunks smaller than the max length
        chunks = [hebrew_chapter[i:i + max_length] for i in range(0, len(hebrew_chapter), max_length)]

        for chunk in chunks:
            translated_text += translator.translate(chunk)

        translation_cache[hebrew_chapter] = translated_text.split('\n') # Store as list of lines
        return translation_cache[hebrew_chapter]

    except RequestError as e:
        logging.warning(f"Translation failed: Request Error - {e}")
        return ["Translation unavailable: Request Error"]

def display_current_verse():
    """Displays the verse corresponding to the current time."""
    while True:
        now = datetime.datetime.now()
        current_time_str = now.strftime("%H:%M:%S")

        word_data, _ = get_current_word_data(current_time_str)
        if word_data is None:
            logging.error("Word data not found for current time.")
            time.sleep(1)
            continue

        book_id = word_data["book_id"]
        chapter_id = word_data["chapter_id"]
        verse_id = word_data["verse_id"]

        hebrew_chapter = flatten_text_with_line_breaks(TANACH_DATA[book_id]["text"][chapter_id])
        english_chapter = translate_chapter('\n'.join(hebrew_chapter))

        print("\033c", end="")  # Clear the terminal
        print(f"Time: {current_time_str}")
        print(f"{TANACH_DATA[book_id]['title']}, Chapter {chapter_id + 1}, Verse {verse_id}")
        print("-" * 30)
        print(hebrew_chapter[verse_id - 1])
        print(english_chapter[verse_id - 1]) # Display corresponding English line
        print("-" * 30)
        time.sleep(1)

# --- Utility Functions --- (Same as before)

def get_current_word_data(client_time_str):
    """Gets data about the current word based on the client's time."""
    try:
        client_time = datetime.datetime.strptime(client_time_str, "%H:%M:%S")
        total_seconds = int(client_time.strftime("%H")) * 3600 + \
                        int(client_time.strftime("%M")) * 60 + \
                        int(client_time.strftime("%S"))

        # Find the closest key in WORD_INDEX
        word_position = min(WORD_INDEX.keys(), key=lambda k: abs(k - total_seconds))

        return WORD_INDEX[word_position], word_position
    except Exception as e:
        logging.error(f"Error processing client time: {e}")
        return None, None

if __name__ == "__main__":
    display_current_verse()