Skip to content

Commit

Permalink
Merge pull request #2759 from tkalir/2742-telegram-images-are-blurry-…
Browse files Browse the repository at this point in the history
…and-out-of-order

2742 telegram images are blurry and out of order
  • Loading branch information
atalyaalon authored Jan 12, 2025
2 parents e9dfa95 + 0061316 commit cc33c10
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 27 deletions.
1 change: 1 addition & 0 deletions anyway/infographic_image_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def create_chrome_browser_session(newsflash_id):
"profile.content_settings.exceptions.automatic_downloads.*.setting": 1,
}
options.add_experimental_option("prefs", prefs)
options.add_argument("--incognito")

browser = webdriver.Remote(
command_executor=selenium_hub_url,
Expand Down
5 changes: 5 additions & 0 deletions anyway/infographics_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,8 @@ def get_infographics_mock_data():
mock_data[WIDGETS].append(widget)
mock_data[WIDGETS] = sorted(mock_data[WIDGETS], key=lambda widget: widget[META]["rank"])
return mock_data


def get_infographics_data_by_newsflash(newsflash_id):
request_params = get_request_params_from_request_values({"news_flash_id": newsflash_id})
return get_infographics_data_for_location(request_params)
29 changes: 3 additions & 26 deletions anyway/telegram_accident_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from anyway.models import TelegramForwardedMessages
from anyway.utilities import trigger_airflow_dag
from anyway.app_and_db import db
from anyway.infographics_utils import get_infographics_data_by_newsflash
import telebot
import boto3
import time
Expand Down Expand Up @@ -77,33 +78,9 @@ 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)
widgets = get_infographics_data_by_newsflash(newsflash_id)["widgets"]
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:
Expand Down Expand Up @@ -157,4 +134,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)
4 changes: 3 additions & 1 deletion anyway/views/news_flash/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,10 @@ def update_news_flash_qualifying(id):
new_location=new_location,
new_qualification=new_location_qualifiction,
)
VERIFIED_QUALIFICATIONS = [NewsflashLocationQualification.MANUAL.value,
NewsflashLocationQualification.VERIFIED.value]
if os.environ.get("FLASK_ENV") == "production" and \
new_location_qualifiction == NewsflashLocationQualification.MANUAL.value and \
new_location_qualifiction in VERIFIED_QUALIFICATIONS and \
old_location_qualifiction != NewsflashLocationQualification.MANUAL.value:
trigger_generate_infographics_and_send_to_telegram(id, False)
return Response(status=HTTPStatus.OK)
Expand Down

0 comments on commit cc33c10

Please sign in to comment.