Weekly Changing URL to download Video

Hi, I download a file weekly (I download it 1 week before it's shown at my church) using a shell script, but wondered how one might use Keyboard Maestro do this natively instead of a shell script?

Here is the script I use:

#!/bin/bash

Adjust the current date by adding 7 days

AdjustedDate=$(date -j -v+7d "+%Y-%m-%d")

Extract the year, month, and day from the adjusted date

Year=$(date -j -f "%Y-%m-%d" "$AdjustedDate" "+%y")
Month=$(date -j -f "%Y-%m-%d" "$AdjustedDate" "+%m")
DayOfYear=$(date -j -f "%Y-%m-%d" "$AdjustedDate" "+%j")

Calculate the quarter based on the month

Quarter=$(( (10#$Month - 1) / 3 + 1 ))

Calculate the start day of the current quarter

StartOfQuarter=$(( ($Quarter - 1) * 91 + 1 ))

Calculate the week of the quarter

WeekOfQuarter=$(( ($DayOfYear - $StartOfQuarter) / 7 + 1 ))

Ensure the week is valid

if [ $WeekOfQuarter -lt 1 ]; then
WeekOfQuarter=1
fi

Construct the URL

URL="https://cdns.adventistmission.org/MS/MS-${Year}${Quarter}-${WeekOfQuarter}-WEEKLY-EN.mp4"

Download location

DownloadLocation="$HOME/Downloads/MS-${Year}${Quarter}-${WeekOfQuarter}-WEEKLY-EN.mp4"

Download the file using curl

curl -o "$DownloadLocation" "$URL"

A typical url is: https://cdns.adventistmission.org/MS/MS-251-1-WEEKLY-EN.mp4

25 is the yr
1 (after the 25) is the quarter in the year
-1- is the week inside that quarter

Thought the use of variables might work ok, but wasn't sure if there was an easier method natively inside of Keyboard Maestro.

Thanks!

I think there would be something slightly easier in KM, because KM has the functions "DAY(), MONTH(), YEAR()" which accept UNIXTIME as an optional parameter. This means you can pass the value "TIME()+7*24*60" to each of those functions and it will return the DAY, MONTH and YEAR of the UNIXTIME that's 7 days (7*24*60 seconds) past the current time.

While that slightly simplifies your first four actions, I don't think it would simplify the logic rest of your shell script commands. If your goal is simplicity, I think you've achieved it. But if your goal is conversion to KM native commands, you can still do that.

1 Like

You could use the %ICUDateTime% token. But you'll still have to do some calculations to get the "week of the quarter".

%ICUDateTimePlus%7%Days%yy,Q,D%

...will get you the comma-separated two-digit year (yy), the quarter (Q), and day of year (D) for 7 days from now, which you can then go to work on.

You'll have to check the maths because I don't get your "251-1" for another 9 days. But this should set you on your way:

Weekly Changing URL.kmmacros (6.5 KB)

Image

Once you're happy with proposed URL and download location displayed you can delete the "Display Text" action and enable "Get URL".

Thanks @Nige_S!

The "251-1" is for the first video of next yr! It isn't for this coming week. New videos are primed for download each Saturday.

The url for this week's video is: https://cdns.adventistmission.org/MS/MS-244-13-WEEKLY-EN.mp4

I just downloaded the macro and ran it and it's 1 week ahead of what I'd like. And if I do my math correctly, there are only 13 weeks per quarter, and the url that macro gives me is 14 (which doesn't exist). https://cdns.adventistmission.org/MS/MS-244-14-WEEKLY-EN.mp4

That's the url I need to download this week to display this coming weekend.

This coming Saturday, I want to download https://cdns.adventistmission.org/MS/MS-251-1-WEEKLY-EN.mp4 this video.

Hope that makes sense. :blush: I appreciate all your assistance and suggestions! Thank you!

Yet there can be 12, 13, or 14 Saturdays in a (3-month) quarter. Check the Tuesdays in this quarter (Oct/Nov/Dec 2024) for another example.

Date maths is hard, often depends on what you mean by various terms, and I'll freely admit I had trouble following along with your script. For example (and I may be mis-reading things) you've the check:

if [ $WeekOfQuarter -lt 1 ]

...but that follows on from:

WeekOfQuarter=$(( ($DayOfYear - $StartOfQuarter) / 7 + 1 ))

...which, if I understand correctly, can never be less than 1.

Here's your bash script from above, Forum-formatted, with a few echos to check the variables:

#!/bin/bash

# Adjust the current date by adding 7 days
AdjustedDate=$(date -j -v+7d "+%Y-%m-%d")

# Extract the year, month, and day from the adjusted date
Year=$(date -j -f "%Y-%m-%d" "$AdjustedDate" "+%y")
Month=$(date -j -f "%Y-%m-%d" "$AdjustedDate" "+%m")
DayOfYear=$(date -j -f "%Y-%m-%d" "$AdjustedDate" "+%j")

# Calculate the quarter based on the month
Quarter=$(( (10#$Month - 1) / 3 + 1 ))

# Calculate the start day of the current quarter
StartOfQuarter=$(( ($Quarter - 1) * 91 + 1 ))

# Calculate the week of the quarter
WeekOfQuarter=$(( ($DayOfYear - $StartOfQuarter) / 7 + 1 ))

# Ensure the week is valid
if [ $WeekOfQuarter -lt 1 ]; then
WeekOfQuarter=1
fi

echo "Year: $Year"
echo "Month: $Month"
echo "DayOfYear: $DayOfYear"
echo "Quarter: $Quarter"
echo "StartOfQuarter: $StartOfQuarter"
echo "WeekOfQuarter: $WeekOfQuarter"

And when I run it today (2024-12-24) the output is:

Year: 24
Month: 12
DayOfYear: 366
Quarter: 4
StartOfQuarter: 274
WeekOfQuarter: 14

...matching the macro's output.

This line has me worried, too:

StartOfQuarter=$(( ($Quarter - 1) * 91 + 1 ))

Compare "quarter starts" to the numbers on NASA's "Day of Year" calendar -- it's right for Q1, 2 and 3 for this (leap) year but wrong for Q4, but most years it'll be right for Q1 and 4 but wrong for Q2 and 3.

All of which is a long way of saying -- if you want to use date maths you'll need to find out how the originator is generating the dates and follow along with their method. But I think that you'll be better of doing this some other way -- if the video you want is always the latest posted, for example, then just grab the latest!

2 Likes

haha Your reply definitely got me thinking. I just started scripting this recently and it's been working for me the last several weeks. I ran it Saturday and I got the correct video, but you are indeed correct–it was wrong when I tried to run it today! Fascinating for sure. I did some thinking and research regarding the amount of days each leap year regarding quarters etc and modified things and I think I have it working again. Definitely trial and error. Calculating dates gets really complicated fast!

Download Mission Spotlight Macro (v11.0.3)

Download Mission Spotlight.kmmacros (3.6 KB)

Keyboard Maestro Export

Unfortunately, there isn't a most "recent" video to download. Here is the page that the videos are published to: https://am.adventistmission.org/mission-spotlight

Thank you again so much for your insight! Definitely is making me look at things differently now.

Ah, but there is -- if you approach things in a different way. You know the year and quarter you want, it's the week that is the problem. But (I think) you'll only ever be out by one. So you could brute force it by trying to download the Local_w + 1 file, and reduce the number by 1 every time you fail then try again...

Weekly Changing URL v2.kmmacros (10.8 KB)

Image

This does, of course, rely on them not adding files weeks in advance and only linking them on the web page when they should be made available!