cormanaz has asked for the wisdom of the Perl Monks concerning the following question:

Can anyone point me toward resources for unshortening t.co links in Perl? WWW::Shorten doesn't include it. I was trying to do it with LWP::UserAgent like so:
use strict; use feature ':5.10'; use LWP::UserAgent; my $uri = 'https://t.co/O4qjsxuCsV'; my $browser = new LWP::UserAgent(); $browser->max_redirect(5); $browser->agent("Mozilla/5.0 (Windows NT 6.1)"); my $response = $browser->get($uri); my $request = $response->request(); my @redirects = $response->redirects(); foreach my $res (@redirects) { print $res->request->uri }
But that just plays back the input URI.

Replies are listed 'Best First'.
Re: Unshortening t.co links
by NERDVANA (Priest) on Sep 07, 2023 at 23:40 UTC
    If you replace the tail of your code with
    use DDP; &p([ $request, map [ $_, $_->request ], @redirects, ])

    you can see that $response->request is listing the final request (whose ->uri you probably wanted?), and each of $response->redirects->@* give you ->request that created that response.

    I'm actually not sure if you wanted the first redirect URL or the final redirect URL, but you can get them both from what is there.

Re: Unshortening t.co links
by marto (Cardinal) on Sep 08, 2023 at 06:34 UTC

    "Can anyone point me toward resources for unshortening t.co links in Perl? WWW::Shorten doesn't include it.

    It does, the makealongerlink method, illustrated in the Synopsis

    Update: disregard the above.

    #!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use feature 'say'; my $url = 'https://t.co/O4qjsxuCsV'; # pretend to be a proper browser my $uaname = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 ( + KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36'; my $ua = LWP::UserAgent->new; $ua->agent( $uaname ); my $resp = $ua->get($url); say $resp->header('title');

    returns:

    https://www.malaysia-chronicle.com/?p=225241
Re: Unshortening t.co links
by Bod (Parson) on Sep 07, 2023 at 23:03 UTC
    Can anyone point me toward resources for unshortening t.co links in Perl

    My approach would be to look at WWW::Shorten::TinyURL which is what does the actual work in WWW::Shorten for TinyURL addresses. Then create a package based on that. This new package will be able to be used with WWW::Shorten.

    At first glance it appears expanding the links will be very similar for t.co as it is for tinyurl.com

      "My approach would be to look at WWW::Shorten::TinyURL"

      Pre coffee I made the same mistake myself :)>

      "At first glance it appears expanding the links will be very similar for t.co as it is for tinyurl.com"

      This isn't the case.