Skip to content

Commit

Permalink
Add square format for social media
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloarosado committed Nov 26, 2024
1 parent 3e7755b commit 2aa5ee4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
28 changes: 23 additions & 5 deletions apps/chart_animation/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def get_query_parameters_in_chart(chart_url, all_years):
return params


def modify_chart_url(chart_url, year, year_range_open, tab):
def modify_chart_url(chart_url, year, year_range_open, tab, social_media_square):
# Take a chart URL, modify its parameters, and create a new URL for the PNG download.
parsed_url = urlparse(chart_url)
path = parsed_url.path
Expand All @@ -135,6 +135,11 @@ def modify_chart_url(chart_url, year, year_range_open, tab):
query_params["time"] = [f"earliest..{year}"]
else:
query_params["time"] = [str(year)]

if social_media_square:
query_params["imType"] = ["social-media-square"]
query_params["imSquareSize"] = ["1080"]

query_params["tab"] = [tab]
query_params["download"] = ["png"]
updated_query = urlencode(query_params, doseq=True)
Expand Down Expand Up @@ -172,14 +177,17 @@ def get_chart_slug(chart_url):
return urlparse(chart_url).path.split("/")[-1]


def create_image_file_name(year, year_range_open, tab):
return f"{year}_{'open' if year_range_open else 'close'}_{tab}.png"
def create_image_file_name(year, year_range_open, tab, social_media_square):
return (
f"{year}_{'open' if year_range_open else 'close'}_{tab}_{'square' if social_media_square else 'nonsquare'}.png"
)


def get_images_from_chart_url(
chart_url,
png_folder,
tab=None,
social_media_square=False,
years=None,
year_range_open=True,
max_workers=None,
Expand Down Expand Up @@ -221,8 +229,11 @@ def get_images_from_chart_url(
futures = {
executor.submit(
download_chart_png,
modify_chart_url(chart_url, year, year_range_open, tab),
Path(png_folder) / create_image_file_name(year=year, year_range_open=year_range_open, tab=tab),
modify_chart_url(chart_url, year, year_range_open, tab, social_media_square),
Path(png_folder)
/ create_image_file_name(
year=year, year_range_open=year_range_open, tab=tab, social_media_square=social_media_square
),
): year
for year in years
}
Expand Down Expand Up @@ -342,6 +353,11 @@ def create_mp4_from_images(
default=None,
help="Chart tab view (either map or chart). If not specified, it is inferred from URL, and otherwise defaults to map.",
)
@click.option(
"--social-media-square",
is_flag=True,
help="Create a square image for social media.",
)
@click.option(
"--years",
type=str,
Expand Down Expand Up @@ -405,6 +421,7 @@ def cli(
output_format,
output_file,
tab,
social_media_square,
years,
year_range_open,
duration,
Expand Down Expand Up @@ -442,6 +459,7 @@ def cli(
chart_url=chart_url,
png_folder=png_folder,
tab=tab,
social_media_square=social_media_square,
years=years,
year_range_open=year_range_open,
max_workers=max_workers,
Expand Down
6 changes: 5 additions & 1 deletion apps/wizard/app_pages/chart_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def add_icons_to_tabs(tab_name):
".gif" if output_type == "GIF" else ".mp4"
)
remove_duplicates = st.toggle("Remove duplicate frames", value=True)
social_media_square = st.toggle("Square format for social media", value=False)
with st_horizontal():
repetitions_last_frame = st.number_input("Repetitions of Last Frame", value=0, step=1)
if output_type == "GIF":
Expand All @@ -159,6 +160,7 @@ def add_icons_to_tabs(tab_name):
chart_url=chart_url,
png_folder=st.session_state.chart_animation_images_folder,
tab=tab,
social_media_square=social_media_square,
years=years,
year_range_open=year_range_open,
max_workers=None,
Expand All @@ -169,7 +171,9 @@ def add_icons_to_tabs(tab_name):
# Select only images that match the required parameters.
image_paths_selected = [
st.session_state.chart_animation_images_folder
/ create_image_file_name(year=year, year_range_open=year_range_open, tab=tab)
/ create_image_file_name(
year=year, year_range_open=year_range_open, tab=tab, social_media_square=social_media_square
)
for year in years
]

Expand Down

0 comments on commit 2aa5ee4

Please sign in to comment.