zkbro

December Adventure 2024

2024-12-11 05:14

Stumbled across the #DecemberAdventure thanks to Frans who connected with me on Neocities (ahh, love my social web!). The goal is to write a little bit of code every day in December. I'm late to the game, but whatever. If I get more than 1 that's a success.

I am not an experienced coder so if anybody has any pointers for my amateur code or methods please let me know.

2024-12-19

[07:30] Working on pulling in activity stats to activity-post.

[21:40] Found stravalib.unithelper is now stravalib.unit_helper. Pulling in Distance and elevation, after logging in via the stravalib.client:

activity = client.get_activity(13139532616)
print(f"Distance: {round(unit_helper.kilometers(activity.distance),2)}s")
print(f"Elevation: {unit_helper.meters(int(activity.total_elevation_gain))}s")
print(f"Moving Time: {str(timedelta(seconds=activity.moving_time))}")
print(f"Elapsed Time: {str(timedelta(seconds=activity.elapsed_time))}")

Outputs:

Distance: 7.78 kilometers
Elevation: 550 meters
Moving Time: 1:05:49
Elapsed Time: 1:23:40

2024-12-18

nil

2024-12-17

Revisiting some old python scripts to work on an "activity post". Idea is to select a GPX from an activity and the script will prepare a markdown/html post with the gpx map, elevation profile, activity stats and a space for notes and images. An example raw formatting from this afternoon's run:

Stats

Notes

Photos

Current code for the map:

from gpxplotter import add_segment_to_map, create_folium_map, read_gpx_file

line_options = {"color": "red", "weight": 6, "opacity": 0.5}

the_map = create_folium_map(tiles="opentopomap")

for track in read_gpx_file("gpx/Afternoon_Run.gpx"):
    for i, segment in enumerate(track["segments"]):
        add_segment_to_map(the_map, segment, line_options=line_options)

the_map.save("gpx_output.html")
the_map

Current code for the elevation profile:

from gpxplotter import plot_line, read_gpx_file
from matplotlib import pyplot as plt

for track in read_gpx_file("gpx/Afternoon_Run.gpx"):
    for i, segment in enumerate(track["segments"]):
        # Plot elevation as function of distance:
        plot_line(
            track,
            segment,
            xvar="Distance / km",
            yvar="elevation",
            lw=0.75,
            color='k',
        )

plt.savefig('output.png')
plt.show()

Obviously a bit of work to do, but the project is up and running.

2024-12-16

Created a function append_latest_imgs in my blog.sh script to automate appending of the last 7 days of images when I first create the draft weeknotes post. The function searches my static/images/ folder based on modified date and adds the last 7 days worth to the weeknote template in the gallery format I'm using. Saves me a heap of time.

Some notes on my use of images and weeknotes:

I used ChatGPT to assist, but I still learnt a tonne.

Feel like I'm squeezing a lot into this bash script. Wondering when would be appropriate to move to something like python?

Here's the function:

append_latest_imgs() {
    cat <<EOF >> "$target_file"
<p style="text-align:center;">
📸
</p>
<div class="img-gallery">
EOF
    # Find images modified in the last 7 days
    find "$img_dir" -type f -mtime -7 | awk -F/ '{print $NF}' | sort | while read -r FILE; do
        if [[ "$FILE" == *.gif ]]; then
            # Get the base name without extension
            BASENAME=$(basename "$FILE" .gif)
        
            # Check for a matching .jpg or .png file
            IMAGE_FILE=$(find "$img_dir" -type f \( -name "$BASENAME.jpg" -o -name "$BASENAME.png" \))
        
            if [[ -n "$IMAGE_FILE" ]]; then
                # Create full path
                GIF=${FILE#"$img_dir"}
                IMG=${IMAGE_FILE#"$img_dir"}
            
                # Append image to gallery
                cat <<EOF >> "$target_file"
<div class="img-gallery__item">
    <a href="/images/$IMG"><img src="/images/$GIF" alt=""></a>
</div>
EOF
            fi
        fi
    done

    if [[ -s "$target_file" ]]; then
        echo "HTML gallery written to $target_file."
    else
        echo "No matching files found in the specified time range."
    fi
    echo "</div>" >>$target_file
    echo "<br>" >> $target_file
}

2024-12-15

nil

2024-12-14

Fixed horizontal srolling within codeblocks by adding overflow-x: auto; to the <pre> CSS element. This makes sure code doesn't extend beyond the codeblock and doesn't wrap, but includes a horizontal scroll bar to scroll within the codeblock. See entry 2024-12-11 below on a small screen or browser window.

2024-12-13

Updated elevation profile python script. This script creates the elevation profiles of my riding and running activities in the header of my website and profiles page, which I update weekly (they're a week-long lookback so can look quite different depending what I do that week).

📸

Screenshot of images created with Geany running in the background

2024-12-12

When I'm turning off the laptop I usually start typing "shutdown" in the search bar until it finds the function I'm looking for, except I always misspell it, realising when I've got to "shit", so rather than fight it, or improve my typing, I've just created a "shit" alias in ~/.bashrc to shutdown from the terminal..

alias shit='systemctl poweroff'

2024-12-11

Made a little bash script to combine my custom image scripts I use for my website:

	#!/bin/bash

# Prompt for desired image process

echo "Select image process:"
echo "[1] Convert all images in current dir to 600w x 800h (portrait)"
echo "[2] Convert all images in current dir to 800w x 600h (landscape)"
echo "[3] Convert all IMG_XXXX.JPG files in current dir to IMGYYYYMMDDHHMM.jpg"
read -p "Enter your choice (1/2/3): " choice

# Run selected process

case $choice in
    1)
        img_pt.sh
        ;;
    2)
        img_ls.sh
        ;;
    3)
        convert_ios_imgs.sh
        ;;
    *)
        echo "Invalid choice. Exiting"
        exit 1
        ;;
esac