Last active
March 5, 2024 16:33
-
-
Save gruber/3151b98f83b16c66e918691b84be55b6 to your computer and use it in GitHub Desktop.
Given an Apple News URL, prints the original URL for the story.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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