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:
-
My weeknotes use 200 px height thumbnails as to not create a huge page of full sized images.
-
All my images are converted earlier to a max of 600x800 or 800x600 with their corresponding thumbnails at the time of previous posts throughout the week.
-
If I want to add extra images which haven't been used in an earlier post, I have to make sure I get them ready first in my images folder.
-
I still have to manually go through and add alt text.
-
The sorting is based on filename. I use IMGXXXX filenaming structure for photos and SSXXXXX for screenshots, so photos will always be above screenshots, even though I have them timestamped. Not a biggy.
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).
-
Script now creates both light and dark PNGs in a single run, rather than me toggling between different colour schemes by commenting/uncommenting, and running the script a second time. It uses a function now, something I need to use more often.
-
Added date stamps of the date the script is run to the file names.
-
General tidy of my junky code.
📸
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