Skip to content

Instantly share code, notes, and snippets.

@gruber
Last active March 5, 2024 16:33

Revisions

  1. gruber revised this gist Nov 14, 2018. No changes.
  2. gruber revised this gist Nov 14, 2018. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions AppleNewsURLRedirect.pl
    Original file line number Diff line number Diff line change
    @@ -7,9 +7,7 @@
    # or
    # % echo 'https://apple.news/Ae78KIEmMOHCVXD4VA7ysKw' | AppleNewsURLRedirect.pl
    #
    # Author: John Gruber
    # https://daringfireball.net/
    #
    # Author: John Gruber <https://daringfireball.net/>
    #
    # 13 Nov 2018
    # Initial version.
  3. gruber created this gist Nov 14, 2018.
    44 changes: 44 additions & 0 deletions AppleNewsURLRedirect.pl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    #!/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;