Skip to content

Instantly share code, notes, and snippets.

@gruber
Last active March 5, 2024 16:33
Show Gist options
  • Save gruber/3151b98f83b16c66e918691b84be55b6 to your computer and use it in GitHub Desktop.
Save gruber/3151b98f83b16c66e918691b84be55b6 to your computer and use it in GitHub Desktop.
Given an Apple News URL, prints the original URL for the story.
#!/usr/bin/perl
# Given an Apple News URL, prints the original URL for the story.
#
# Usage:
# % AppleNewsURLRedirect.pl 'https://apple.news/Ae78KIEmMOHCVXD4VA7ysKw'
# or
# % echo 'https://apple.news/Ae78KIEmMOHCVXD4VA7ysKw' | AppleNewsURLRedirect.pl
#
# Author: John Gruber <https://daringfireball.net/>
#
# 13 Nov 2018
# Initial version.
#
# Todo: Should probably take a list of Apple News URLs, not just one.
#
use strict;
use warnings;
use LWP::Simple;
# Try ARGV first
my $apple_news_url = $ARGV[0];
# Try STDIN next (trying STDIN first won't work, will take input from terminal)
unless (defined $apple_news_url) {
$apple_news_url = <STDIN>;
}
unless( defined $apple_news_url && $apple_news_url =~ m{https://apple.news/\S+}) {
print "Usage: AppleNewsURLRedirect.pl 'https://apple.news/article_id'";
exit;
}
my $content = get($apple_news_url);
die "Couldn't get '$apple_news_url'" unless defined $content;
# $content is an HTML page. Pretty easy to scrape the redirect URL
# from the JavaScript, but this could easily break in the future.
$content =~ m{redirectToUrl\("(.+)"\);};
my $redirect_url = $1;
print $redirect_url;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment