diff --git a/anyway/infographic_image_generator.py b/anyway/infographic_image_generator.py index 31ce6ce0..36f4e72d 100644 --- a/anyway/infographic_image_generator.py +++ b/anyway/infographic_image_generator.py @@ -18,7 +18,7 @@ selenium_hub_url = f"https://{selenium_hub_url}/wd/hub" selenium_remote_results_url = f"https://{selenium_url}/tempdata" CHROME_PARTIALLY_DOWNLOADED_FILE_EXTENSION = "crdownload" - +SLEEP_DURATION_FOR_MAP_TO_HAVE_FOCUS = 5 def create_chrome_browser_session(newsflash_id): options = webdriver.ChromeOptions() @@ -94,7 +94,9 @@ def generate_infographics_in_selenium_container(browser, newsflash_id): logging.debug(f"found {buttons_found} buttons") if buttons_found > 0: for element in elements: - ActionChains(browser).move_to_element(element).click().perform() + ActionChains(browser).move_to_element(element).perform() + time.sleep(SLEEP_DURATION_FOR_MAP_TO_HAVE_FOCUS) #without sleep map infographic may be blurry + element.click() time.sleep(1) #prevents click arriving before the last finished is_download_done, generated_images_names = wait_for_folder_to_contain_all_files(newsflash_id, buttons_found, timeout=60) diff --git a/anyway/telegram_accident_notifications.py b/anyway/telegram_accident_notifications.py index abecd469..7376c731 100644 --- a/anyway/telegram_accident_notifications.py +++ b/anyway/telegram_accident_notifications.py @@ -50,11 +50,9 @@ def create_accident_text(newsflash_id): return f"{first_line}\n\n{newsflash['description']}" -def fetch_transcription_by_widget_name(newsflash_id): - widgets_url = f"{ANYWAY_BASE_API_URL}/infographics-data?lang=he&news_flash_id={newsflash_id}&years_ago=5" - widgets_json = requests.get(widgets_url).json() +def get_transcription_by_widget_name(widgets): transcription_by_widget_name = {widget["name"]: widget["data"]["text"]["transcription"] - for widget in widgets_json["widgets"] + for widget in widgets if "transcription" in widget["data"]["text"]} return transcription_by_widget_name @@ -65,6 +63,8 @@ def send_after_infographics_message(bot, message_id_in_group, newsflash_id, link return bot.send_message(linked_group, message, reply_to_message_id=message_id_in_group) +#this function sends the "root" message for the newsflash in telegram. +#the flow continues when the telegram server sends a request to our /api/telegram/webhook def publish_notification(newsflash_id, chat_id=TELEGRAM_CHANNEL_CHAT_ID): accident_text = create_accident_text(newsflash_id) bot = telebot.TeleBot(secrets.get("BOT_TOKEN")) @@ -77,23 +77,59 @@ def publish_notification(newsflash_id, chat_id=TELEGRAM_CHANNEL_CHAT_ID): db.session.commit() +def fetch_widgets_with_retries(newsflash_id, wait_times): + url = f"https://www.anyway.co.il/api/infographics-data?news_flash_id={newsflash_id}" + for attempt, wait_time in enumerate(wait_times): + try: + logging.debug(f"Attempt {attempt + 1}: Fetching data widgets for newsflash {newsflash_id}") + response = requests.get(url) + if response.ok: + response_json = response.json() + widgets = response_json.get("widgets", []) + if len(widgets) > 0: + return widgets + except requests.exceptions.RequestException as e: + logging.debug(e) + time.sleep(wait_time) + raise RuntimeError(f"Failed to fetch data from {url}") + + +def fetch_widgets(newsflash_id): + retry_timeouts = [10, 20, 30, 60] + widgets = fetch_widgets_with_retries(newsflash_id, retry_timeouts) + return [widget.get("name") for widget in widgets] + + +def get_items_for_send(newsflash_id): + items = [] + retry_timeouts = [10, 20, 30, 60] + widgets = fetch_widgets_with_retries(newsflash_id, retry_timeouts) + transcription_by_widget_name = get_transcription_by_widget_name(widgets) + urls_by_infographic_name = create_public_urls_for_infographics_images(str(newsflash_id)) + for widget in widgets: + name = widget.get("name") + if name in urls_by_infographic_name: + url = urls_by_infographic_name.get(name) + text = transcription_by_widget_name.get(name) + items.append((url, text)) + return items + + def send_infographics_to_telegram(root_message_id, newsflash_id, channel_of_initial_message): #every message in the channel is automatically forwarded to the linked discussion group. #to create a comment on the channel message, we need to send a reply to the #forwareded message in the discussion group. bot = telebot.TeleBot(secrets.get("BOT_TOKEN")) - transcription_by_widget_name = fetch_transcription_by_widget_name(newsflash_id) - urls_by_infographic_name = create_public_urls_for_infographics_images(str(newsflash_id)) - linked_group = telegram_linked_group_by_channel[channel_of_initial_message] - for infographic_name, url in urls_by_infographic_name.items(): - text = transcription_by_widget_name[infographic_name] \ - if infographic_name in transcription_by_widget_name else None + items_for_send = get_items_for_send(newsflash_id) + for url, text in items_for_send: bot.send_photo(linked_group, url, reply_to_message_id=root_message_id, caption=text) + send_after_infographics_message(bot, root_message_id, newsflash_id, linked_group) logging.info("notification send done") + def extract_infographic_name_from_s3_object(s3_object_name): left = s3_object_name.rindex("/") right = s3_object_name.rindex(".") @@ -121,4 +157,4 @@ def trigger_generate_infographics_and_send_to_telegram(newsflash_id, pre_verific dag_conf = {"news_flash_id": newsflash_id} dag_conf["chat_id"] = TELEGRAM_CHANNEL_CHAT_ID if pre_verification_chat \ else TELEGRAM_POST_VERIFICATION_CHANNEL_CHAT_ID - trigger_airflow_dag("generate-and-send-infographics-images", dag_conf) + trigger_airflow_dag("generate-and-send-infographics-images", dag_conf) \ No newline at end of file