bartman081523 commited on
Commit
973aec5
·
1 Parent(s): 3f409fa

add cli version

Browse files
Files changed (1) hide show
  1. cli_clock.py +91 -0
cli_clock.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import logging
3
+ import datetime
4
+ import time
5
+ import requests
6
+ import pytz
7
+ from deep_translator import GoogleTranslator
8
+ from deep_translator.exceptions import NotValidLength, RequestError
9
+ from utils import process_json_files, flatten_text_with_line_breaks, build_word_index
10
+
11
+ # Set up logging
12
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
13
+
14
+ # Load Tanach text
15
+ TANACH_DATA = process_json_files(1, 39)
16
+ WORD_INDEX = build_word_index(TANACH_DATA)
17
+
18
+ # --- Translation Cache ---
19
+ translation_cache = {}
20
+
21
+ def translate_chapter(hebrew_chapter):
22
+ """Translates a Hebrew chapter to English, caching the result."""
23
+ if hebrew_chapter in translation_cache:
24
+ return translation_cache[hebrew_chapter]
25
+
26
+ try:
27
+ translator = GoogleTranslator(source='iw', target='en')
28
+ max_length = 2000 # Slightly below the limit to be safe
29
+ translated_text = ""
30
+
31
+ # Split the chapter into chunks smaller than the max length
32
+ chunks = [hebrew_chapter[i:i + max_length] for i in range(0, len(hebrew_chapter), max_length)]
33
+
34
+ for chunk in chunks:
35
+ translated_text += translator.translate(chunk)
36
+
37
+ translation_cache[hebrew_chapter] = translated_text.split('\n') # Store as list of lines
38
+ return translation_cache[hebrew_chapter]
39
+
40
+ except RequestError as e:
41
+ logging.warning(f"Translation failed: Request Error - {e}")
42
+ return ["Translation unavailable: Request Error"]
43
+
44
+ def display_current_verse():
45
+ """Displays the verse corresponding to the current time."""
46
+ while True:
47
+ now = datetime.datetime.now()
48
+ current_time_str = now.strftime("%H:%M:%S")
49
+
50
+ word_data, _ = get_current_word_data(current_time_str)
51
+ if word_data is None:
52
+ logging.error("Word data not found for current time.")
53
+ time.sleep(1)
54
+ continue
55
+
56
+ book_id = word_data["book_id"]
57
+ chapter_id = word_data["chapter_id"]
58
+ verse_id = word_data["verse_id"]
59
+
60
+ hebrew_chapter = flatten_text_with_line_breaks(TANACH_DATA[book_id]["text"][chapter_id])
61
+ english_chapter = translate_chapter('\n'.join(hebrew_chapter))
62
+
63
+ print("\033c", end="") # Clear the terminal
64
+ print(f"Time: {current_time_str}")
65
+ print(f"{TANACH_DATA[book_id]['title']}, Chapter {chapter_id + 1}, Verse {verse_id}")
66
+ print("-" * 30)
67
+ print(hebrew_chapter[verse_id - 1])
68
+ print(english_chapter[verse_id - 1]) # Display corresponding English line
69
+ print("-" * 30)
70
+ time.sleep(1)
71
+
72
+ # --- Utility Functions --- (Same as before)
73
+
74
+ def get_current_word_data(client_time_str):
75
+ """Gets data about the current word based on the client's time."""
76
+ try:
77
+ client_time = datetime.datetime.strptime(client_time_str, "%H:%M:%S")
78
+ total_seconds = int(client_time.strftime("%H")) * 3600 + \
79
+ int(client_time.strftime("%M")) * 60 + \
80
+ int(client_time.strftime("%S"))
81
+
82
+ # Find the closest key in WORD_INDEX
83
+ word_position = min(WORD_INDEX.keys(), key=lambda k: abs(k - total_seconds))
84
+
85
+ return WORD_INDEX[word_position], word_position
86
+ except Exception as e:
87
+ logging.error(f"Error processing client time: {e}")
88
+ return None, None
89
+
90
+ if __name__ == "__main__":
91
+ display_current_verse()